Code reformat and deleted unused file

This commit is contained in:
Alessandro Pellizzari 2018-12-06 09:23:09 +00:00
parent d4c00f1892
commit 91d300149c
3 changed files with 55 additions and 111 deletions

110
build.rs
View file

@ -1,16 +1,16 @@
extern crate walkdir;
extern crate serde; extern crate serde;
extern crate walkdir;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
extern crate serde_json; extern crate serde_json;
use serde_json::Error as JsonError;
use std::env; use std::env;
use std::fs::File; use std::fs::File;
use std::io::{Write, Read, Error as IoError}; use std::io::{Error as IoError, Read, Write};
use std::path::Path; use std::path::Path;
use serde_json::{Error as JsonError};
use walkdir::{WalkDir, DirEntry}; use walkdir::{DirEntry, WalkDir};
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Locale { pub struct Locale {
@ -26,7 +26,8 @@ fn main() {
let dest_path = Path::new(&out_dir).join("locales.rs"); let dest_path = Path::new(&out_dir).join("locales.rs");
let mut f = File::create(&dest_path).unwrap(); let mut f = File::create(&dest_path).unwrap();
let _ = f.write_all(r#####"lazy_static! { let _ = f.write_all(
r#####"lazy_static! {
pub static ref LOCALES: Locales = { pub static ref LOCALES: Locales = {
let mut res = Locales { let mut res = Locales {
short_months: HashMap::new(), short_months: HashMap::new(),
@ -35,7 +36,9 @@ fn main() {
long_weekdays: HashMap::new(), long_weekdays: HashMap::new(),
ampm: HashMap::new(), ampm: HashMap::new(),
}; };
"#####.as_bytes()); "#####
.as_bytes(),
);
println!("Building..."); println!("Building...");
for entry in WalkDir::new("locales") { for entry in WalkDir::new("locales") {
@ -44,7 +47,7 @@ fn main() {
if entry.path().extension().map(|e| e != "json").unwrap_or(false) { if entry.path().extension().map(|e| e != "json").unwrap_or(false) {
println!("Not a json file"); println!("Not a json file");
continue continue;
} }
let locale_name = entry.path().file_stem().map(|n| n.to_string_lossy()); let locale_name = entry.path().file_stem().map(|n| n.to_string_lossy());
@ -52,85 +55,82 @@ fn main() {
continue; continue;
} }
let locale_name = locale_name.unwrap().to_string(); let locale_name = locale_name.unwrap().to_string();
if let Ok(locale_data) = load_locale(&entry) { if let Ok(locale_data) = load_locale(&entry) {
if let Some(long_months) = locale_data.long_months { if let Some(long_months) = locale_data.long_months {
if long_months.len() == 12 { if long_months.len() == 12 {
let _ = f.write_all(format!( let _ = f
"res.long_months.insert(\"{}\".into(), vec![{}]);\n", .write_all(
locale_name, format!(
long_months "res.long_months.insert(\"{}\".into(), vec![{}]);\n",
.iter() locale_name,
.map(|s| format!("\"{}\"", s)) long_months.iter().map(|s| format!("\"{}\"", s)).collect::<Vec<String>>().join(",")
.collect::<Vec<String>>() ).as_bytes(),
.join(",") ).unwrap();
).as_bytes()).unwrap();
} }
} }
if let Some(short_months) = locale_data.short_months { if let Some(short_months) = locale_data.short_months {
if short_months.len() == 12 { if short_months.len() == 12 {
let _ = f.write_all(format!( let _ = f
"res.short_months.insert(\"{}\".into(), vec![{}]);\n", .write_all(
locale_name, format!(
short_months "res.short_months.insert(\"{}\".into(), vec![{}]);\n",
.iter() locale_name,
.map(|s| format!("\"{}\"", s)) short_months.iter().map(|s| format!("\"{}\"", s)).collect::<Vec<String>>().join(",")
.collect::<Vec<String>>() ).as_bytes(),
.join(",") ).unwrap();
).as_bytes()).unwrap();
} }
} }
if let Some(long_weekdays) = locale_data.long_weekdays { if let Some(long_weekdays) = locale_data.long_weekdays {
if long_weekdays.len() == 7 { if long_weekdays.len() == 7 {
let _ = f.write_all(format!( let _ = f
"res.long_weekdays.insert(\"{}\".into(), vec![{}]);\n", .write_all(
locale_name, format!(
long_weekdays "res.long_weekdays.insert(\"{}\".into(), vec![{}]);\n",
.iter() locale_name,
.map(|s| format!("\"{}\"", s)) long_weekdays.iter().map(|s| format!("\"{}\"", s)).collect::<Vec<String>>().join(",")
.collect::<Vec<String>>() ).as_bytes(),
.join(",") ).unwrap();
).as_bytes()).unwrap();
} }
} }
if let Some(short_weekdays) = locale_data.short_weekdays { if let Some(short_weekdays) = locale_data.short_weekdays {
if short_weekdays.len() == 7 { if short_weekdays.len() == 7 {
let _ = f.write_all(format!( let _ = f
"res.short_weekdays.insert(\"{}\".into(), vec![{}]);\n", .write_all(
locale_name, format!(
short_weekdays "res.short_weekdays.insert(\"{}\".into(), vec![{}]);\n",
.iter() locale_name,
.map(|s| format!("\"{}\"", s)) short_weekdays.iter().map(|s| format!("\"{}\"", s)).collect::<Vec<String>>().join(",")
.collect::<Vec<String>>() ).as_bytes(),
.join(",") ).unwrap();
).as_bytes()).unwrap();
} }
} }
if let Some(ampm) = locale_data.ampm { if let Some(ampm) = locale_data.ampm {
if ampm.len() == 2 { if ampm.len() == 2 {
let _ = f.write_all(format!( let _ = f
"res.ampm.insert(\"{}\".into(), vec![{}]);\n", .write_all(
locale_name, format!(
ampm "res.ampm.insert(\"{}\".into(), vec![{}]);\n",
.iter() locale_name,
.map(|s| format!("\"{}\"", s)) ampm.iter().map(|s| format!("\"{}\"", s)).collect::<Vec<String>>().join(",")
.collect::<Vec<String>>() ).as_bytes(),
.join(",") ).unwrap();
).as_bytes()).unwrap();
} }
} }
} }
} }
let _ = f.write_all(r####" res let _ = f.write_all(
r####" res
}; };
} }
"####.as_bytes()); "####
.as_bytes(),
);
} }
fn load_locale(entry: &DirEntry) -> Result<Locale, BuildError> { fn load_locale(entry: &DirEntry) -> Result<Locale, BuildError> {

View file

@ -1,55 +0,0 @@
// This file will be overwritten by build.rs during compilation
use std::collections::HashMap;
#[derive(Debug)]
pub struct Locales {
pub short_months: HashMap<String, Vec<&'static str>>,
pub long_months: HashMap<String, Vec<&'static str>>,
pub short_weekdays: HashMap<String, Vec<&'static str>>,
pub long_weekdays: HashMap<String, Vec<&'static str>>,
}
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
};
}

View file

@ -10,4 +10,3 @@ pub struct Locales {
} }
include!(concat!(env!("OUT_DIR"), "/locales.rs")); include!(concat!(env!("OUT_DIR"), "/locales.rs"));