From 76de75210f027e9f17a7abf86936f0d7c34faff1 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Mon, 24 Feb 2025 22:50:06 -0500 Subject: [PATCH] Improve the report format --- l10n-db/src/bin/main.rs | 36 ++++++++++++++---------------------- l10n-db/src/types.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/l10n-db/src/bin/main.rs b/l10n-db/src/bin/main.rs index 56a8f0a..9df9dde 100644 --- a/l10n-db/src/bin/main.rs +++ b/l10n-db/src/bin/main.rs @@ -1,7 +1,5 @@ use std::{ - io::{BufReader, Read, Write}, - path::PathBuf, - process::Command, + fmt, io::{BufReader, Read, Write}, path::PathBuf, process::Command }; use clap::{Parser, Subcommand}; @@ -68,32 +66,26 @@ struct Report { 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")?; + for key in self.out_of_date.iter() { + write!(f, "\t{}\n", key)?; + } + Ok(()) + } +} + fn generate_report( bundle: &Bundle, base_locale: &LanguageIdentifier, locales: Vec, ) -> Report { let mut report: Report = Default::default(); - let locales: Vec = - locales.into_iter().filter(|a| a != base_locale).collect(); for (key, message) in bundle.message_iter() { - match message.variant(base_locale) { - Some(ref base_variant) => { - for locale in locales.iter() { - match message.variant(locale) { - Some(v) if v.modified() < base_variant.modified() => { - report.out_of_date.push(key.to_owned()) - } - Some(_) => {} - None => report.out_of_date.push(key.to_owned()), - } - } - } - None => { - report.source_deleted.push(key.clone()); - } + if message.variants_out_of_date(base_locale).len() > 0 { + report.out_of_date.push(key.to_owned()) } - let base_variant = message.variant(base_locale).clone(); } report @@ -141,7 +133,7 @@ fn main() { } Some(Commands::Report) => { let report = generate_report(&bundle, &config.base_locale, config.locales); - println!("{:?}", report); + println!("{}", report); } None => {} } diff --git a/l10n-db/src/types.rs b/l10n-db/src/types.rs index 5b8c9ee..7b878d8 100644 --- a/l10n-db/src/types.rs +++ b/l10n-db/src/types.rs @@ -1,4 +1,8 @@ -use std::{collections::HashMap, io::{BufReader, Read}, path::Path}; +use std::{ + collections::HashMap, + io::{BufReader, Read}, + path::Path, +}; use chrono::{DateTime, Utc}; use icu_locid::LanguageIdentifier; @@ -47,6 +51,27 @@ impl Message { 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)]