Improve the report format

This commit is contained in:
Savanni D'Gerinel 2025-02-24 22:50:06 -05:00
parent e5b3c7e4e1
commit 76de75210f
2 changed files with 40 additions and 23 deletions

View File

@ -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<String>,
}
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<LanguageIdentifier>,
) -> Report {
let mut report: Report = Default::default();
let locales: Vec<LanguageIdentifier> =
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 => {}
}

View File

@ -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<LanguageIdentifier> {
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<LanguageIdentifier>) -> Vec<LanguageIdentifier> {}
}
#[derive(Deserialize, Serialize, Debug, Clone)]