diff --git a/Cargo.toml b/Cargo.toml index 005b3ac..5b0668c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "chrono-locale" version = "0.1.0" authors = ["Alessandro Pellizzari "] -# build = "build.rs" +build = "build.rs" [dependencies] chrono = "0.4" diff --git a/build.rs b/build.rs index a9c7dc5..7f342ac 100644 --- a/build.rs +++ b/build.rs @@ -1,62 +1,174 @@ extern crate walkdir; +extern crate serde; +#[macro_use] +extern crate serde_derive; +extern crate serde_json; use std::env; use std::fs::File; -use std::io::Write; +use std::io::{Write, Read, Error as IoError}; use std::path::Path; +use serde_json::{Error as JsonError}; -use walkdir::WalkDir; +use walkdir::{WalkDir, DirEntry}; +#[derive(Deserialize)] pub struct Locale { - short_months: Vec, - long_months: Vec, - short_weekdays: Vec, - long_weekdays: Vec, + short_months: Option>, + long_months: Option>, + short_weekdays: Option>, + long_weekdays: Option>, } - fn main() { let out_dir = env::var("OUT_DIR").unwrap(); let dest_path = Path::new(&out_dir).join("locales.rs"); let mut f = File::create(&dest_path).unwrap(); - f.write_all(r###" - use std::collections::HashMap; + let _ = f.write_all(r#####"lazy_static! { + pub static ref LOCALES: Locales = { + let mut res = Locales { + short_months: HashMap::new(), + long_months: HashMap::new(), + short_weekdays: HashMap::new(), + long_weekdays: HashMap::new(), + }; - pub type Locales = HashMap; + res.short_months.insert( + "C".into(), + vec!["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], + ); - pub struct Locale { - short_months: Vec, - long_months: Vec, - short_weekdays: Vec, - long_weekdays: Vec, - } + res.long_months.insert( + "C".into(), + vec![ + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December", + ], + ); - lazy_static! { - static ref LOCALES: Locales = { - let mut res = HashMap::new(); -"###.as_bytes()).unwrap(); + res.short_weekdays + .insert("C".into(), vec!["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]); + res.long_weekdays.insert( + "C".into(), + vec!["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], + ); + "#####.as_bytes()); println!("Building..."); for entry in WalkDir::new("locales") { let entry = entry.unwrap(); - println!("{}", entry.path().display()); + println!("Found {}", entry.path().display()); if entry.path().extension().map(|e| e != "json").unwrap_or(false) { println!("Not a json file"); continue } + let locale_name = entry.path().file_stem().map(|n| n.to_string_lossy()); + if locale_name.is_none() { + continue; + } + let locale_name = locale_name.unwrap().to_string(); + if let Ok(locale_data) = load_locale(&entry) { + if let Some(long_months) = locale_data.long_months { + if long_months.len() == 12 { + let _ = f.write_all(format!( + "res.long_months.insert(\"{}\".into(), vec![{}]);\n", + locale_name, + long_months + .iter() + .map(|s| format!("\"{}\"", s)) + .collect::>() + .join(",") + ).as_bytes()).unwrap(); + } + } + + if let Some(short_months) = locale_data.short_months { + if short_months.len() == 12 { + let _ = f.write_all(format!( + "res.short_months.insert(\"{}\".into(), vec![{}]);\n", + locale_name, + short_months + .iter() + .map(|s| format!("\"{}\"", s)) + .collect::>() + .join(",") + ).as_bytes()).unwrap(); + } + } + + if let Some(long_weekdays) = locale_data.long_weekdays { + if long_weekdays.len() == 7 { + let _ = f.write_all(format!( + "res.long_weekdays.insert(\"{}\".into(), vec![{}]);\n", + locale_name, + long_weekdays + .iter() + .map(|s| format!("\"{}\"", s)) + .collect::>() + .join(",") + ).as_bytes()).unwrap(); + } + } + + if let Some(short_weekdays) = locale_data.short_weekdays { + if short_weekdays.len() == 7 { + let _ = f.write_all(format!( + "res.short_weekdays.insert(\"{}\".into(), vec![{}]);\n", + locale_name, + short_weekdays + .iter() + .map(|s| format!("\"{}\"", s)) + .collect::>() + .join(",") + ).as_bytes()).unwrap(); + } + } + } } - - f.write_all(r###" - - res + let _ = f.write_all(r####" res }; } - "###.as_bytes()).unwrap(); + "####.as_bytes()); +} + +fn load_locale(entry: &DirEntry) -> Result { + let mut locale_data = String::new(); + let mut f = File::open(entry.path())?; + f.read_to_string(&mut locale_data)?; + let locale = serde_json::from_str::(&locale_data)?; + Ok(locale) +} + +enum BuildError { + Io(IoError), + Json(JsonError), +} + +impl From for BuildError { + fn from(e: IoError) -> Self { + BuildError::Io(e) + } +} + +impl From for BuildError { + fn from(e: JsonError) -> Self { + BuildError::Json(e) + } } diff --git a/src/locales-old.rs b/src/locales-old.rs new file mode 100644 index 0000000..0952cba --- /dev/null +++ b/src/locales-old.rs @@ -0,0 +1,55 @@ +// This file will be overwritten by build.rs during compilation + +use std::collections::HashMap; + +#[derive(Debug)] +pub struct Locales { + pub short_months: HashMap>, + pub long_months: HashMap>, + pub short_weekdays: HashMap>, + pub long_weekdays: HashMap>, +} + +lazy_static! { + pub static ref LOCALES: Locales = { + let mut res = Locales { + short_months: HashMap::new(), + long_months: HashMap::new(), + short_weekdays: HashMap::new(), + long_weekdays: HashMap::new(), + }; + + res.short_months.insert( + "C".into(), + vec!["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], + ); + + res.long_months.insert( + "C".into(), + vec![ + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December", + ], + ); + + res.short_weekdays + .insert("C".into(), vec!["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]); + + res.long_weekdays.insert( + "C".into(), + vec!["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], + ); + + res + }; +} diff --git a/src/locales.rs b/src/locales.rs index e34e5a7..fee07fa 100644 --- a/src/locales.rs +++ b/src/locales.rs @@ -1,7 +1,6 @@ -// This file will be overwritten by build.rs during compilation - use std::collections::HashMap; +#[derive(Debug)] pub struct Locales { pub short_months: HashMap>, pub long_months: HashMap>, @@ -9,46 +8,5 @@ pub struct Locales { pub long_weekdays: HashMap>, } -lazy_static! { - pub static ref LOCALES: Locales = { - let mut res = Locales { - short_months: HashMap::new(), - long_months: HashMap::new(), - short_weekdays: HashMap::new(), - long_weekdays: HashMap::new(), - }; +include!(concat!(env!("OUT_DIR"), "/locales.rs")); - res.short_months.insert( - "C".into(), - vec!["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], - ); - - res.long_months.insert( - "C".into(), - vec![ - "January", - "February", - "March", - "April", - "May", - "June", - "July", - "August", - "September", - "October", - "November", - "December", - ], - ); - - res.short_weekdays - .insert("C".into(), vec!["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]); - - res.long_weekdays.insert( - "C".into(), - vec!["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], - ); - - res - }; -}