From a07ecae04a20e13474ee2c4fbd0b050703910751 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Mon, 24 Feb 2025 22:54:39 -0500 Subject: [PATCH] Mvoe teh message and variant types into the bundle --- l10n-db/src/bundle.rs | 95 ++++++++++++++++++++++++++++++++++- l10n-db/src/editor.rs | 2 +- l10n-db/src/lib.rs | 6 +-- l10n-db/src/types.rs | 112 ------------------------------------------ 4 files changed, 95 insertions(+), 120 deletions(-) delete mode 100644 l10n-db/src/types.rs diff --git a/l10n-db/src/bundle.rs b/l10n-db/src/bundle.rs index df1e6b1..72af3e3 100644 --- a/l10n-db/src/bundle.rs +++ b/l10n-db/src/bundle.rs @@ -1,6 +1,8 @@ -use std::{collections::HashMap, fs::File, io::Write, path::PathBuf}; +use std::{collections::HashMap, fs::File, io::{BufReader, Read, Write}, path::{Path, PathBuf}}; -use crate::{Message, WriteError}; +use chrono::{DateTime, Utc}; +use icu_locid::LanguageIdentifier; +use serde::{Deserialize, Serialize}; pub struct Bundle { path: PathBuf, @@ -48,5 +50,94 @@ fn save_file(path: &PathBuf, s: &[u8]) { f.write(s).unwrap(); } +#[derive(Deserialize, Serialize, Debug)] +pub struct Message { + key: String, + description: String, + variants: HashMap, +} + +impl Message { + pub fn new(key: String) -> Self { + Self { + key, + description: "".to_owned(), + variants: HashMap::new(), + } + } + + 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; + } + + pub fn description(&self) -> &str { + &self.description + } + + pub fn variant(&self, locale: &LanguageIdentifier) -> Option<&Variant> { + self.variants.get(locale) + } + + pub fn variant_mut(&mut self, locale: LanguageIdentifier) -> &mut Variant { + self.variants.entry(locale.clone()).or_insert(Variant { + locale, + content: "".to_owned(), + modified: Utc::now(), + }) + } + + pub fn variants_out_of_date( + &self, + base_locale: &LanguageIdentifier, + ) -> Vec { + match self + .variants + .get(base_locale) + .map(|variant| variant.modified()) + { + Some(base_date) => self + .variants + .iter() + .filter(|(_, value)| base_date > value.modified()) + .map(|(locale, _)| locale.clone()) + .collect(), + None => vec![], + } + } + + // pub fn missing_variants(&self, locals: Vec) -> Vec {} +} + +#[derive(Deserialize, Serialize, Debug, Clone)] +pub struct Variant { + locale: LanguageIdentifier, + content: String, + modified: DateTime, +} + +impl Variant { + pub fn content(&self) -> &str { + &self.content + } + + pub fn set_content(&mut self, content: String) { + self.content = content; + self.modified = Utc::now(); + } + + pub fn modified(&self) -> DateTime { + self.modified + } +} + + #[cfg(test)] mod test {} diff --git a/l10n-db/src/editor.rs b/l10n-db/src/editor.rs index c51d580..039ea9e 100644 --- a/l10n-db/src/editor.rs +++ b/l10n-db/src/editor.rs @@ -3,7 +3,7 @@ use std::{io::{BufReader, Read, Write}, path::Path, process::Command}; use icu_locid::{langid, LanguageIdentifier}; use serde::{Deserialize, Serialize}; -use crate::{read_fh, Message, Variant}; +use crate::{read_fh, Message}; #[derive(Serialize, Deserialize, Debug, Clone)] diff --git a/l10n-db/src/lib.rs b/l10n-db/src/lib.rs index d826d47..2268a86 100644 --- a/l10n-db/src/lib.rs +++ b/l10n-db/src/lib.rs @@ -1,5 +1,5 @@ mod bundle; -pub use bundle::Bundle; +pub use bundle::{Bundle, Message, Variant}; mod editor; pub use editor::Editor; @@ -7,9 +7,5 @@ pub use editor::Editor; mod formats; pub use formats::{js, xliff}; -mod types; -pub use types::{Message, Variant}; - mod utils; pub use utils::*; - diff --git a/l10n-db/src/types.rs b/l10n-db/src/types.rs deleted file mode 100644 index 7b878d8..0000000 --- a/l10n-db/src/types.rs +++ /dev/null @@ -1,112 +0,0 @@ -use std::{ - collections::HashMap, - io::{BufReader, Read}, - path::Path, -}; - -use chrono::{DateTime, Utc}; -use icu_locid::LanguageIdentifier; -use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; - -#[derive(Deserialize, Serialize, Debug)] -pub struct Message { - key: String, - description: String, - variants: HashMap, -} - -impl Message { - pub fn new(key: String) -> Self { - Self { - key, - description: "".to_owned(), - variants: HashMap::new(), - } - } - - 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; - } - - pub fn description(&self) -> &str { - &self.description - } - - pub fn variant(&self, locale: &LanguageIdentifier) -> Option<&Variant> { - self.variants.get(locale) - } - - pub fn variant_mut(&mut self, locale: LanguageIdentifier) -> &mut Variant { - self.variants.entry(locale.clone()).or_insert(Variant { - locale, - content: "".to_owned(), - modified: Utc::now(), - }) - } - - pub fn variants_out_of_date( - &self, - base_locale: &LanguageIdentifier, - ) -> Vec { - match self - .variants - .get(base_locale) - .map(|variant| variant.modified()) - { - Some(base_date) => self - .variants - .iter() - .filter(|(_, value)| base_date > value.modified()) - .map(|(locale, _)| locale.clone()) - .collect(), - None => vec![], - } - } - - // pub fn missing_variants(&self, locals: Vec) -> Vec {} -} - -#[derive(Deserialize, Serialize, Debug, Clone)] -pub struct Variant { - locale: LanguageIdentifier, - content: String, - modified: DateTime, -} - -impl Variant { - pub fn content(&self) -> &str { - &self.content - } - - pub fn set_content(&mut self, content: String) { - self.content = content; - self.modified = Utc::now(); - } - - pub fn modified(&self) -> DateTime { - self.modified - } -} - -/* -fn time_to_number(time: &DateTime, s: S) -> Result -where S: Serializer { - 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()) -} -*/