-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.rs
167 lines (149 loc) · 3.86 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
extern crate serde;
extern crate walkdir;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
use serde_json::Error as JsonError;
use std::env;
use std::fs::File;
use std::io::{Error as IoError, Read, Write};
use std::path::Path;
use walkdir::{DirEntry, WalkDir};
#[derive(Deserialize)]
pub struct Locale {
short_months: Option<Vec<String>>,
long_months: Option<Vec<String>>,
short_weekdays: Option<Vec<String>>,
long_weekdays: Option<Vec<String>>,
ampm: Option<Vec<String>>,
}
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();
let _ = f.write_all(
r#####"// This file is @generated automatically by chrono_lc. Please don't edit by hand.
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(),
ampm: HashMap::new(),
};
"#####
.as_bytes(),
);
println!("Building...");
for entry in WalkDir::new("locales") {
let entry = entry.unwrap();
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 {
f.write_all(
format!(
"res.long_months.insert(\"{}\".into(), vec![{}]);\n",
locale_name,
long_months.iter().map(|s| format!("\"{}\"", s)).collect::<Vec<String>>().join(",")
)
.as_bytes(),
)
.unwrap();
}
}
if let Some(short_months) = locale_data.short_months {
if short_months.len() == 12 {
f.write_all(
format!(
"res.short_months.insert(\"{}\".into(), vec![{}]);\n",
locale_name,
short_months.iter().map(|s| format!("\"{}\"", s)).collect::<Vec<String>>().join(",")
)
.as_bytes(),
)
.unwrap();
}
}
if let Some(long_weekdays) = locale_data.long_weekdays {
if long_weekdays.len() == 7 {
f.write_all(
format!(
"res.long_weekdays.insert(\"{}\".into(), vec![{}]);\n",
locale_name,
long_weekdays.iter().map(|s| format!("\"{}\"", s)).collect::<Vec<String>>().join(",")
)
.as_bytes(),
)
.unwrap();
}
}
if let Some(short_weekdays) = locale_data.short_weekdays {
if short_weekdays.len() == 7 {
f.write_all(
format!(
"res.short_weekdays.insert(\"{}\".into(), vec![{}]);\n",
locale_name,
short_weekdays.iter().map(|s| format!("\"{}\"", s)).collect::<Vec<String>>().join(",")
)
.as_bytes(),
)
.unwrap();
}
}
if let Some(ampm) = locale_data.ampm {
if ampm.len() == 4 {
f.write_all(
format!(
"res.ampm.insert(\"{}\".into(), vec![{}]);\n",
locale_name,
ampm.iter().map(|s| format!("\"{}\"", s)).collect::<Vec<String>>().join(",")
)
.as_bytes(),
)
.unwrap();
}
}
}
}
let _ = f.write_all(
r####" res
};
}
"####
.as_bytes(),
);
println!("Formatting...");
}
fn load_locale(entry: &DirEntry) -> Result<Locale, BuildError> {
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>(&locale_data)?;
Ok(locale)
}
#[allow(dead_code)]
enum BuildError {
Io(IoError),
Json(JsonError),
}
impl From<IoError> for BuildError {
fn from(e: IoError) -> Self {
BuildError::Io(e)
}
}
impl From<JsonError> for BuildError {
fn from(e: JsonError) -> Self {
BuildError::Json(e)
}
}