From 1c3d0711e1c028a3ebfb6ffca3cbef79172085cf Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sat, 22 Feb 2025 16:40:23 -0500 Subject: [PATCH] Start reading the bundle --- Cargo.lock | 1 + l10n-db/Cargo.toml | 1 + l10n-db/i18n/OpenSandbox.toml | 6 +++--- l10n-db/src/bin/main.rs | 33 +++++---------------------------- l10n-db/src/bundle.rs | 18 +++++++++++++++--- l10n-db/src/types.rs | 34 ++++++++++++++++++++++++++-------- 6 files changed, 51 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 372fc49..d48d3bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2414,6 +2414,7 @@ dependencies = [ name = "l10n-db" version = "0.1.0" dependencies = [ + "chrono", "clap", "icu_locid", "serde 1.0.218", diff --git a/l10n-db/Cargo.toml b/l10n-db/Cargo.toml index 8dda9ff..977cd68 100644 --- a/l10n-db/Cargo.toml +++ b/l10n-db/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +chrono = { version = "0.4.39", features = ["serde"] } clap = { version = "4.5.30", features = ["derive"] } icu_locid = { version = "1.5.0", features = ["serde"] } serde = { version = "1.0.218", features = ["derive"] } diff --git a/l10n-db/i18n/OpenSandbox.toml b/l10n-db/i18n/OpenSandbox.toml index 1f24302..4c963f6 100644 --- a/l10n-db/i18n/OpenSandbox.toml +++ b/l10n-db/i18n/OpenSandbox.toml @@ -1,7 +1,7 @@ key = "OpenSandbox" -description = "a basic description without any content" +description = "A sandbox vault refers to a stock vault that contains test data and allows the user to make edits and run experiments on test data." [variants.en-US] locale = "en-US" -content = "" -modified = 1740241312 +content = "Open Sandbox vault" +modified = "2025-02-22T21:35:02.032300406Z" diff --git a/l10n-db/src/bin/main.rs b/l10n-db/src/bin/main.rs index a51d432..ecd079f 100644 --- a/l10n-db/src/bin/main.rs +++ b/l10n-db/src/bin/main.rs @@ -31,34 +31,7 @@ enum Commands { fn edit_key(bundle: &mut Bundle, key: String, locale: LanguageIdentifier, editor: &str) { let message = bundle.message(key); - Editor::edit(message, locale, editor); - - println!("final version"); - println!("{}", toml::to_string(&message).unwrap()); - - /* - let mut editor_file = tempfile::NamedTempFile::new().unwrap(); - let _ = editor_file.write(toml::to_string(message).unwrap().as_bytes()); - let _ = editor_file.flush(); - let mut cmd = Command::new(editor).args([editor_file.path()]).spawn().unwrap(); - cmd.wait().unwrap(); - let _ = editor_file.flush(); - let mut reader = BufReader::new(editor_file); - let mut content = Vec::new(); - let _ = reader.read_to_end(&mut content); - - println!("content"); - println!("{}", String::from_utf8(content).unwrap()); - - - 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(); } @@ -72,7 +45,11 @@ fn main() { match &cli.command { Some(Commands::EditKey{ name, locale } ) => edit_key(&mut bundle, name.to_owned(), langid!("en-US"), &editor), - Some(Commands::ListKeys) => todo!(), + Some(Commands::ListKeys) => { + for (key, _) in bundle.message_iter() { + println!("{}", key); + } + }, Some(Commands::Export{..}) => todo!(), None => {}, } diff --git a/l10n-db/src/bundle.rs b/l10n-db/src/bundle.rs index aa0f8ee..3144ea9 100644 --- a/l10n-db/src/bundle.rs +++ b/l10n-db/src/bundle.rs @@ -9,10 +9,22 @@ pub struct Bundle { impl Bundle { pub fn load_from_disk(path: PathBuf) -> Self { - Self { - path, - messages: HashMap::new(), + let mut messages = HashMap::new(); + if path.is_dir() { + for entry in std::fs::read_dir(&path).unwrap() { + let entry = entry.unwrap(); + let path = entry.path().clone(); + let key = path.file_stem().unwrap(); + + let message = Message::from_file(&entry.path()); + messages.insert(key.to_str().unwrap().to_owned(), message); + } } + Self { path, messages } + } + + pub fn message_iter(&self) -> impl Iterator { + self.messages.iter() } pub fn message(&mut self, name: String) -> &mut Message { diff --git a/l10n-db/src/types.rs b/l10n-db/src/types.rs index c6ee3e5..32c4268 100644 --- a/l10n-db/src/types.rs +++ b/l10n-db/src/types.rs @@ -1,7 +1,8 @@ -use std::{collections::HashMap, time::{SystemTime, UNIX_EPOCH}}; +use std::{collections::HashMap, io::{BufReader, Read}, path::Path}; +use chrono::{DateTime, Utc}; use icu_locid::LanguageIdentifier; -use serde::{Deserialize, Serialize, Serializer}; +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; #[derive(Deserialize, Serialize, Debug)] pub struct Message { @@ -19,6 +20,14 @@ impl Message { } } + pub fn from_file(path: &Path) -> Message { + let file = std::fs::File::open(path).unwrap(); + let mut content = Vec::new(); + let mut reader = BufReader::new(file); + let _ = reader.read_to_end(&mut content); + toml::from_str(&String::from_utf8(content).unwrap()).unwrap() + } + pub fn set_description(&mut self, desc: String) { self.description = desc; } @@ -31,7 +40,7 @@ impl Message { self.variants.entry(locale.clone()).or_insert(Variant { locale, content: "".to_owned(), - modified: SystemTime::now(), + modified: Utc::now(), }) } } @@ -40,8 +49,7 @@ impl Message { pub struct Variant { locale: LanguageIdentifier, content: String, - #[serde(serialize_with = "time_to_number")] - modified: SystemTime, + modified: DateTime, } impl Variant { @@ -51,11 +59,21 @@ impl Variant { pub fn set_content(&mut self, content: String) { self.content = content; - self.modified = SystemTime::now(); + self.modified = Utc::now(); } } -fn time_to_number(time: &SystemTime, s: S) -> Result +/* +fn time_to_number(time: &DateTime, s: S) -> Result where S: Serializer { - s.serialize_u64(time.duration_since(UNIX_EPOCH).unwrap().as_secs()) + let seconds: u64 = time.as_secs(); + s.serialize_u64(seconds) } + +fn number_to_time<'de, D>(d: D) -> Result +where D: Deserializer<'de> { + let buf = String::deserialize(d)?; + let num = buf.parse::().unwrap(); + Ok(DateTime::try_from(num).unwrap()) +} +*/