diff --git a/l10n-db/i18n/OpenSandbox.toml b/l10n-db/i18n/OpenSandbox.toml new file mode 100644 index 0000000..7df243d --- /dev/null +++ b/l10n-db/i18n/OpenSandbox.toml @@ -0,0 +1,7 @@ +key = "OpenSandbox" +description = "Open a sandbox" + +[variants.en-US] +locale = "en-US" +content = "Open a sandbox vault" +modified = 1740198524 diff --git a/l10n-db/src/bin/main.rs b/l10n-db/src/bin/main.rs index 0ff2a5c..39602cb 100644 --- a/l10n-db/src/bin/main.rs +++ b/l10n-db/src/bin/main.rs @@ -1,6 +1,9 @@ +use std::path::PathBuf; + use clap::{Parser, Subcommand}; -use l10n_db; +use icu_locid::{langid, LanguageIdentifier}; +use l10n_db::{self, Bundle}; #[derive(Parser)] #[command(version, about, long_about = None)] @@ -11,9 +14,9 @@ struct Cli { #[derive(Subcommand)] enum Commands { - // Edit, potentially creating, a key - // EditKey { }, - // List al keys in the database + /// Edit, potentially creating, a key + EditKey { name: String, locale: String }, + /// List al keys in the database ListKeys, // Search the database // 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() { + let editor = std::env::var("EDITOR").unwrap(); + let db_path = std::env::var("DB_PATH").unwrap(); + let cli = Cli::parse(); + let mut bundle = Bundle::load_from_disk(PathBuf::from(&db_path)); + match &cli.command { + Some(Commands::EditKey{ name, locale } ) => edit_key(&mut bundle, name.to_owned(), &editor), Some(Commands::ListKeys) => todo!(), Some(Commands::Export{..}) => todo!(), None => {}, diff --git a/l10n-db/src/bundle.rs b/l10n-db/src/bundle.rs index 2f7572c..aa0f8ee 100644 --- a/l10n-db/src/bundle.rs +++ b/l10n-db/src/bundle.rs @@ -1,24 +1,40 @@ -use std::{collections::HashMap, path::Path}; +use std::{collections::HashMap, fs::File, io::Write, path::PathBuf}; use crate::Message; pub struct Bundle { - messages: HashMap + path: PathBuf, + messages: HashMap, } impl Bundle { - pub fn new() -> Self { - Self{ messages: HashMap::new() } - } - - pub fn load_from_disk(path: &Path) -> Self { + pub fn load_from_disk(path: PathBuf) -> 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)] -mod test { -} +mod test {} diff --git a/l10n-db/src/types.rs b/l10n-db/src/types.rs index 0e7de32..c6ca0d1 100644 --- a/l10n-db/src/types.rs +++ b/l10n-db/src/types.rs @@ -1,9 +1,9 @@ -use std::{collections::HashMap, time::SystemTime}; +use std::{collections::HashMap, time::{SystemTime, UNIX_EPOCH}}; use icu_locid::LanguageIdentifier; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, Serializer}; -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Serialize, Debug)] pub struct Message { key: String, description: String, @@ -36,11 +36,22 @@ impl Message { } } -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Serialize, Debug)] pub struct Variant { locale: LanguageIdentifier, content: String, + #[serde(serialize_with = "time_to_number")] modified: SystemTime, } +impl Variant { + pub fn set_content(&mut self, content: String) { + self.content = content; + self.modified = SystemTime::now(); + } +} +fn time_to_number(time: &SystemTime, s: S) -> Result +where S: Serializer { + s.serialize_u64(time.duration_since(UNIX_EPOCH).unwrap().as_secs()) +}