Write a single phrase to disk

This commit is contained in:
Savanni D'Gerinel 2025-02-22 10:23:46 -05:00
parent 359ab96779
commit e0392a4150
4 changed files with 73 additions and 18 deletions

View File

@ -0,0 +1,7 @@
key = "OpenSandbox"
description = "Open a sandbox"
[variants.en-US]
locale = "en-US"
content = "Open a sandbox vault"
modified = 1740198524

View File

@ -1,6 +1,9 @@
use std::path::PathBuf;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use l10n_db; use icu_locid::{langid, LanguageIdentifier};
use l10n_db::{self, Bundle};
#[derive(Parser)] #[derive(Parser)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
@ -11,9 +14,9 @@ struct Cli {
#[derive(Subcommand)] #[derive(Subcommand)]
enum Commands { enum Commands {
// Edit, potentially creating, a key /// Edit, potentially creating, a key
// EditKey { }, EditKey { name: String, locale: String },
// List al keys in the database /// List al keys in the database
ListKeys, ListKeys,
// Search the database // Search the database
// Search { }, // Search { },
@ -26,10 +29,28 @@ enum Commands {
}, },
} }
fn edit_key(bundle: &mut Bundle, key: String, editor: &str) {
let message = bundle.message(key);
println!("message: {:?}", message);
message.set_description("Open a sandbox".to_owned());
let variant = message.variant_mut(langid!("en-US"));
variant.set_content("Open a sandbox vault".to_owned());
bundle.save();
}
fn main() { fn main() {
let editor = std::env::var("EDITOR").unwrap();
let db_path = std::env::var("DB_PATH").unwrap();
let cli = Cli::parse(); let cli = Cli::parse();
let mut bundle = Bundle::load_from_disk(PathBuf::from(&db_path));
match &cli.command { match &cli.command {
Some(Commands::EditKey{ name, locale } ) => edit_key(&mut bundle, name.to_owned(), &editor),
Some(Commands::ListKeys) => todo!(), Some(Commands::ListKeys) => todo!(),
Some(Commands::Export{..}) => todo!(), Some(Commands::Export{..}) => todo!(),
None => {}, None => {},

View File

@ -1,24 +1,40 @@
use std::{collections::HashMap, path::Path}; use std::{collections::HashMap, fs::File, io::Write, path::PathBuf};
use crate::Message; use crate::Message;
pub struct Bundle { pub struct Bundle {
messages: HashMap<String, Message> path: PathBuf,
messages: HashMap<String, Message>,
} }
impl Bundle { impl Bundle {
pub fn new() -> Self { pub fn load_from_disk(path: PathBuf) -> Self {
Self{ messages: HashMap::new() }
}
pub fn load_from_disk(path: &Path) -> Self {
Self { Self {
messages: HashMap::new() path,
} messages: HashMap::new(),
} }
} }
pub fn message(&mut self, name: String) -> &mut Message {
self.messages
.entry(name.to_owned())
.or_insert(Message::new(name.to_owned()))
}
pub fn save(&self) {
self.messages.iter().for_each(|(key, value)| {
let mut path = self.path.clone();
path.push(key);
path.set_extension("toml");
save_file(&path, toml::to_string(value).unwrap().as_bytes());
});
}
}
fn save_file(path: &PathBuf, s: &[u8]) {
let mut f = File::create(path).unwrap();
f.write(s).unwrap();
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {}
}

View File

@ -1,9 +1,9 @@
use std::{collections::HashMap, time::SystemTime}; use std::{collections::HashMap, time::{SystemTime, UNIX_EPOCH}};
use icu_locid::LanguageIdentifier; use icu_locid::LanguageIdentifier;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize, Serializer};
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize, Debug)]
pub struct Message { pub struct Message {
key: String, key: String,
description: String, description: String,
@ -36,11 +36,22 @@ impl Message {
} }
} }
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize, Debug)]
pub struct Variant { pub struct Variant {
locale: LanguageIdentifier, locale: LanguageIdentifier,
content: String, content: String,
#[serde(serialize_with = "time_to_number")]
modified: SystemTime, modified: SystemTime,
} }
impl Variant {
pub fn set_content(&mut self, content: String) {
self.content = content;
self.modified = SystemTime::now();
}
}
fn time_to_number<S>(time: &SystemTime, s: S) -> Result<S::Ok, S::Error>
where S: Serializer {
s.serialize_u64(time.duration_since(UNIX_EPOCH).unwrap().as_secs())
}