diff --git a/l10n-db/src/bin/main.rs b/l10n-db/src/bin/main.rs index 9df9dde..d02b30e 100644 --- a/l10n-db/src/bin/main.rs +++ b/l10n-db/src/bin/main.rs @@ -1,6 +1,4 @@ -use std::{ - fmt, io::{BufReader, Read, Write}, path::PathBuf, process::Command -}; +use std::{fmt, path::PathBuf}; use clap::{Parser, Subcommand}; @@ -24,9 +22,9 @@ enum Commands { /// Edit, potentially creating, a key EditKey { #[arg(short, long)] - name: String, + key: String, #[arg(short, long)] - locale: String, + locale: Option, }, /// List al keys in the database ListKeys, @@ -41,6 +39,8 @@ enum Commands { #[arg(short, long)] format: String, #[arg(short, long)] + file: String, + #[arg(short, long)] locale: Option, }, Report, @@ -50,7 +50,6 @@ enum Commands { struct Config { db_path: PathBuf, base_locale: LanguageIdentifier, - locales: Vec, } fn edit_key(bundle: &mut Bundle, key: String, locale: LanguageIdentifier, editor: &str) { @@ -62,28 +61,26 @@ fn edit_key(bundle: &mut Bundle, key: String, locale: LanguageIdentifier, editor #[derive(Clone, Debug, Default)] struct Report { keys: Vec, - source_deleted: Vec, + // source_deleted: Vec, out_of_date: Vec, } impl fmt::Display for Report { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Out of date messages\n")?; + writeln!(f, "{} messages in bundle", self.keys.len())?; + writeln!(f, "Out of date messages")?; for key in self.out_of_date.iter() { - write!(f, "\t{}\n", key)?; + writeln!(f, "\t{}", key)?; } Ok(()) } } -fn generate_report( - bundle: &Bundle, - base_locale: &LanguageIdentifier, - locales: Vec, -) -> Report { +fn generate_report(bundle: &Bundle, base_locale: &LanguageIdentifier) -> Report { let mut report: Report = Default::default(); for (key, message) in bundle.message_iter() { - if message.variants_out_of_date(base_locale).len() > 0 { + report.keys.push(key.to_owned()); + if !message.variants_out_of_date(base_locale).is_empty() { report.out_of_date.push(key.to_owned()) } } @@ -104,9 +101,11 @@ fn main() { let mut bundle = Bundle::load_from_disk(PathBuf::from(&config.db_path)); match &cli.command { - Some(Commands::EditKey { name, locale }) => { - let identifier = locale.parse::().unwrap(); - edit_key(&mut bundle, name.to_owned(), identifier, &editor) + Some(Commands::EditKey { key, locale }) => { + let identifier = locale.as_ref() + .map(|l| l.parse::().unwrap()) + .unwrap_or(config.base_locale); + edit_key(&mut bundle, key.to_owned(), identifier, &editor) } Some(Commands::ListKeys) => { for (key, _) in bundle.message_iter() { @@ -117,22 +116,26 @@ fn main() { import_file(&mut bundle, &PathBuf::from(file)).unwrap(); bundle.save(); } - Some(Commands::Export { format, locale }) => { + Some(Commands::Export { + format, + file, + locale, + }) => { let locale = locale .as_ref() .map(|l| l.clone().parse::().unwrap()) .unwrap_or(langid!("en")); match format.as_ref() { - "js" => js::export_file(&bundle, locale, &PathBuf::from("output.json")).unwrap(), + "js" => js::export_file(&bundle, locale, &PathBuf::from(file)).unwrap(), "xliff" => { - xliff::export_file(&bundle, locale, &PathBuf::from("output.xliff")).unwrap() + xliff::export_file(&bundle, locale, &PathBuf::from(file)).unwrap() } _ => todo!(), } } Some(Commands::Report) => { - let report = generate_report(&bundle, &config.base_locale, config.locales); + let report = generate_report(&bundle, &config.base_locale); println!("{}", report); } None => {} diff --git a/l10n-db/src/bundle.rs b/l10n-db/src/bundle.rs index 72af3e3..1b55392 100644 --- a/l10n-db/src/bundle.rs +++ b/l10n-db/src/bundle.rs @@ -47,7 +47,7 @@ impl Bundle { fn save_file(path: &PathBuf, s: &[u8]) { let mut f = File::create(path).unwrap(); - f.write(s).unwrap(); + let _ = f.write(s).unwrap(); } #[derive(Deserialize, Serialize, Debug)] diff --git a/l10n-db/src/editor.rs b/l10n-db/src/editor.rs index 039ea9e..b6d9a9f 100644 --- a/l10n-db/src/editor.rs +++ b/l10n-db/src/editor.rs @@ -1,11 +1,10 @@ -use std::{io::{BufReader, Read, Write}, path::Path, process::Command}; +use std::{io::Write, process::Command}; use icu_locid::{langid, LanguageIdentifier}; use serde::{Deserialize, Serialize}; use crate::{read_fh, Message}; - #[derive(Serialize, Deserialize, Debug, Clone)] struct EditorMessage { description: String, @@ -13,19 +12,7 @@ struct EditorMessage { content: String, } -/* -impl EditorMessage { - fn from_variant(description: String, variant: &Variant) -> Self { - Self { - description, - content: variant.content().to_string() - } - } -} -*/ - -pub struct Editor { -} +pub struct Editor {} impl Editor { pub fn edit(msg: &mut Message, locale: LanguageIdentifier, editor: &str) { @@ -47,10 +34,10 @@ impl Editor { let file = file.reopen().unwrap(); let content = read_fh(&file).unwrap(); - let new_variant: EditorMessage = toml::from_str(&String::from_utf8(content).unwrap()).unwrap(); + let new_variant: EditorMessage = + toml::from_str(&String::from_utf8(content).unwrap()).unwrap(); variant.set_content(new_variant.content); msg.set_description(new_variant.description); } } - diff --git a/l10n-db/src/formats/js.rs b/l10n-db/src/formats/js.rs index 79d68cb..6aa94b2 100644 --- a/l10n-db/src/formats/js.rs +++ b/l10n-db/src/formats/js.rs @@ -16,7 +16,7 @@ pub fn export_fh(bundle: &Bundle, locale: LanguageIdentifier, fh: &mut File) -> }).collect::>(); let messages: BTreeMap = messages.into_iter().collect(); - fh.write(serde_json::to_string_pretty(&messages).unwrap().as_bytes()).unwrap(); + let _ = fh.write(serde_json::to_string_pretty(&messages).unwrap().as_bytes()).unwrap(); Ok(()) } diff --git a/l10n-db/src/formats/xliff.rs b/l10n-db/src/formats/xliff.rs index cbde7a3..d05ecaa 100644 --- a/l10n-db/src/formats/xliff.rs +++ b/l10n-db/src/formats/xliff.rs @@ -1,25 +1,14 @@ use std::{ - collections::HashMap, fs::File, - io::{self, BufReader, Read, Write}, + io::{BufReader, Read, Write}, path::Path, }; -use chrono::{DateTime, Utc}; use icu_locid::{langid, LanguageIdentifier}; use xml::{attribute::OwnedAttribute, reader, writer, EmitterConfig, EventReader, EventWriter}; use crate::{Bundle, Message, ReadError, WriteError}; -struct PartialMessage { - variants: HashMap, -} - -struct PartialVariant { - content: Option, - modified: Option>, -} - pub fn export_file( bundle: &Bundle, locale: LanguageIdentifier, @@ -133,12 +122,12 @@ fn write_message( .into(), writer::XmlEvent::start_element("notes").into(), writer::XmlEvent::start_element("note").into(), - writer::XmlEvent::characters(message.description()).into(), + writer::XmlEvent::characters(message.description()), writer::XmlEvent::end_element().into(), writer::XmlEvent::end_element().into(), writer::XmlEvent::start_element("segment").into(), writer::XmlEvent::start_element("source").into(), - writer::XmlEvent::characters(message.variant(locale).unwrap().content()).into(), + writer::XmlEvent::characters(message.variant(locale).unwrap().content()), writer::XmlEvent::end_element().into(), writer::XmlEvent::end_element().into(), writer::XmlEvent::end_element().into(),