-
Notifications
You must be signed in to change notification settings - Fork 3
/
gen.py
275 lines (240 loc) · 7.19 KB
/
gen.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import holidays
from dataclasses import dataclass
from jinja2 import Environment
@dataclass
class Country:
code: str
name: str
countries = [
Country("AO", "Angola"),
Country("AR", "Argentina"),
Country("AM", "Armenia"),
Country("AW", "Aruba"),
Country("AU", "Australia"),
Country("AT", "Austria"),
Country("AZ", "Azerbaijan"),
Country("BD", "Bangladesh"),
Country("BY", "Belarus"),
Country("BE", "Belgium"),
Country("BO", "Bolivia"),
Country("BA", "Bosnia and Herzegovina"),
Country("BW", "Botswana"),
Country("BR", "Brazil"),
Country("BG", "Bulgaria"),
Country("BI", "Burundi"),
Country("CA", "Canada"),
Country("CL", "Chile"),
Country("CN", "China"),
Country("CO", "Colombia"),
Country("HR", "Croatia"),
Country("CU", "Cuba"),
Country("CW", "Curaçao"),
Country("CY", "Cyprus"),
Country("CZ", "Czechia"),
Country("DK", "Denmark"),
Country("DJ", "Djibouti"),
Country("DO", "Dominican Republic"),
Country("EG", "Egypt"),
Country("EE", "Estonia"),
Country("ET", "Ethiopia"),
Country("FI", "Finland"),
Country("FR", "France"),
Country("GE", "Georgia"),
Country("DE", "Germany"),
Country("GR", "Greece"),
Country("HN", "Honduras"),
Country("HK", "Hong Kong"),
Country("HU", "Hungary"),
Country("IS", "Iceland"),
Country("IN", "India"),
Country("ID", "Indonesia"),
Country("IE", "Ireland"),
Country("IM", "Isle of Man"),
Country("IL", "Israel"),
Country("IT", "Italy"),
Country("JM", "Jamaica"),
Country("JP", "Japan"),
Country("KZ", "Kazakhstan"),
Country("KE", "Kenya"),
Country("LV", "Latvia"),
Country("LS", "Lesotho"),
Country("LI", "Liechtenstein"),
Country("LT", "Lithuania"),
Country("LU", "Luxembourg"),
Country("MG", "Madagascar"),
Country("MY", "Malaysia"),
Country("MW", "Malawi"),
Country("MT", "Malta"),
Country("MX", "Mexico"),
Country("MD", "Moldova"),
Country("MA", "Morocco"),
Country("MZ", "Mozambique"),
Country("NL", "Netherlands"),
Country("NA", "Namibia"),
Country("NZ", "New Zealand"),
Country("NI", "Nicaragua"),
Country("NG", "Nigeria"),
Country("MK", "North Macedonia"),
Country("NO", "Norway"),
Country("PK", "Pakistan"),
Country("PY", "Paraguay"),
Country("PE", "Peru"),
Country("PL", "Poland"),
Country("PT", "Portugal"),
Country("RO", "Romania"),
Country("RU", "Russia"),
Country("SA", "Saudi Arabia"),
Country("RS", "Serbia"),
Country("SG", "Singapore"),
Country("SK", "Slovakia"),
Country("SI", "Slovenia"),
Country("ZA", "South Africa"),
Country("KR", "South Korea"),
Country("ES", "Spain"),
Country("SZ", "Swaziland"),
Country("SE", "Sweden"),
Country("CH", "Switzerland"),
Country("TW", "Taiwan"),
Country("TR", "Turkey"),
Country("TN", "Tunisia"),
Country("UA", "Ukraine"),
Country("AE", "United Arab Emirates"),
Country("GB", "United Kingdom"),
Country("US", "United States"),
Country("UY", "Uruguay"),
Country("UZ", "Uzbekistan"),
Country("VE", "Venezuela"),
Country("VN", "Vietnam"),
Country("ZM", "Zambia"),
Country("ZW", "Zimbabwe"),
]
years = range(2000, 2031)
country = """
use crate::Error;
/// Two-letter country codes defined in ISO 3166-1 alpha-2 .
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub enum Country {
{%- for country in countries %}
#[cfg(feature = "{{country.code}}")]
/// {{country.name}}
{{country.code}},
{%- endfor %}
}
impl std::fmt::Display for Country {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_ref())
}
}
impl AsRef<str> for Country {
fn as_ref(&self) -> &str {
match self {
{%- for country in countries %}
#[cfg(feature = "{{country.code}}")]
Country::{{country.code}} => "{{country.code}}",
{%- endfor %}
}
}
}
impl std::str::FromStr for Country {
type Err = Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Ok(match s {
{%- for country in countries %}
#[cfg(feature = "{{country.code}}")]
"{{country.code}}" => Country::{{country.code}},
{%- endfor %}
_ => return Err(Error::CountryNotAvailable),
})
}
}
"""
build = """
use std::collections::HashSet;
use crate::{data::*, prelude::*, HolidayMap, Result, Year};
/// Generate holiday map for the specified countries and years.
pub fn build(countries: Option<&HashSet<Country>>, years: Option<&std::ops::Range<Year>>) -> Result<HolidayMap> {
let mut map = HolidayMap::new();
{% for country in countries %}
#[cfg(feature = "{{country.code}}")]
if countries.is_none() || countries.unwrap().contains(&Country::{{country.code}}) {
map.insert(Country::{{country.code}}, {{country.code|escape}}::build(&years)?);
}
{% endfor %}
Ok(map)
}
"""
country_mod = """
mod helper;
use crate::{prelude::*, Holiday, NaiveDateExt, Result, Year};
use helper::build_year;
use chrono::NaiveDate;
use std::collections::BTreeMap;
use std::collections::HashMap;
{% for country in countries %}
#[cfg(feature = "{{country.code}}")]
pub mod {{country.code|escape}};
{% endfor %}
"""
build_country = """
//! {{country}}
use super::*;
/// Generate holiday map for {{country}}.
#[allow(unused_mut, unused_variables)]
pub fn build(years: &Option<&std::ops::Range<Year>>) -> Result<HashMap<Year, BTreeMap<NaiveDate, Holiday>>> {
let mut map = HashMap::new();
{%- for year in years %}
{% if holiday(years=year) %}
build_year(
years,
{{year}},
vec![
{% for date, name in holiday(years=year).items() %}
(NaiveDate::from_ymd_res({{date|year}}, {{date|month}}, {{date|day}})?, "{{name}}"),
{%- endfor %}
],
&mut map,
Country::{{code}},
"{{country}}",
);
{%- endif %}
{%- endfor %}
Ok(map)
}
"""
def lower(code: str) -> str:
return code.lower()
def escape(code: str) -> str:
rust_keywords = ["as", "in", "do"]
lower = code.lower()
if lower in rust_keywords:
return "r#" + lower
else:
return lower
def empty_holiday(**kwargs):
return {}
if __name__ == "__main__":
env = Environment()
env.filters["year"] = lambda d: d.year
env.filters["month"] = lambda d: d.month
env.filters["day"] = lambda d: d.day
env.filters["escape"] = escape
env.filters["lower"] = lower
with open("src/country.rs", "w") as f:
rendered = env.from_string(country).render(countries=countries)
f.write(rendered)
with open("src/build.rs", "w") as f:
rendered = env.from_string(build).render(countries=countries)
f.write(rendered)
with open("src/data/mod.rs", "w") as f:
rendered = env.from_string(country_mod).render(countries=countries)
f.write(rendered)
for country in countries:
with open("src/data/{}.rs".format(country.code.lower()), "w") as f:
holiday = getattr(holidays, country.code, None)
rendered = env.from_string(build_country).render(
code=country.code,
country=country.name,
years=years,
holiday=holiday or empty_holiday)
f.write(rendered)