@@ -116,6 +116,8 @@ class Country:
116
116
years = range (2000 , 2031 )
117
117
118
118
country = """
119
+ use crate::Error;
120
+
119
121
/// Two-letter country codes defined in ISO 3166-1 alpha-2 .
120
122
#[allow(dead_code)]
121
123
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, PartialOrd, Ord)]
@@ -127,15 +129,10 @@ class Country:
127
129
{%- endfor %}
128
130
}
129
131
130
- impl ToString for Country {
131
- fn to_string(&self) -> String {
132
- match self {
133
- {%- for country in countries %}
134
- #[cfg(feature = "{{country.code}}")]
135
- Country::{{country.code}} => "{{country.code}}".into(),
136
- {%- endfor %}
132
+ impl std::fmt::Display for Country {
133
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
134
+ write!(f, "{}", self.as_ref())
137
135
}
138
- }
139
136
}
140
137
141
138
impl AsRef<str> for Country {
@@ -153,21 +150,25 @@ class Country:
153
150
type Err = Error;
154
151
155
152
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
156
- match s {
153
+ Ok( match s {
157
154
{%- for country in countries %}
158
155
#[cfg(feature = "{{country.code}}")]
159
- "{{country.code}}" => Ok( Country::{{country.code}}),
156
+ "{{country.code}}" => Country::{{country.code}},
160
157
{%- endfor %}
161
- _ => Err(Error::CountryNotAvailable),
162
- }
158
+ _ => return Err(Error::CountryNotAvailable),
159
+ })
163
160
}
164
161
}
165
162
166
163
"""
167
164
168
165
build = """
166
+ use std::collections::HashSet;
167
+
168
+ use crate::{data::*, prelude::*, HolidayMap, Result, Year};
169
+
169
170
/// Generate holiday map for the specified countries and years.
170
- fn build(countries: Option<&HashSet<Country>>, years: Option<&std::ops::Range<Year>>) -> Result<HolidayMap> {
171
+ pub fn build(countries: Option<&HashSet<Country>>, years: Option<&std::ops::Range<Year>>) -> Result<HolidayMap> {
171
172
let mut map = HolidayMap::new();
172
173
{% for country in countries %}
173
174
#[cfg(feature = "{{country.code}}")]
@@ -180,10 +181,24 @@ class Country:
180
181
181
182
"""
182
183
184
+ country_mod = """
185
+ mod helper;
186
+
187
+ use crate::{prelude::*, Holiday, NaiveDateExt, Result, Year};
188
+ use helper::build_year;
189
+
190
+ use chrono::NaiveDate;
191
+ use std::collections::BTreeMap;
192
+ use std::collections::HashMap;
193
+
194
+ {% for country in countries %}
195
+ #[cfg(feature = "{{country.code}}")]
196
+ pub mod {{country.code|escape}};
197
+ {% endfor %}
198
+ """
199
+
183
200
build_country = """
184
- /// {{country}}.
185
- #[cfg(feature = "{{code}}")]
186
- pub mod {{code|escape}} {
201
+ //! {{country}}
187
202
use super::*;
188
203
189
204
/// Generate holiday map for {{country}}.
@@ -192,21 +207,24 @@ class Country:
192
207
let mut map = HashMap::new();
193
208
194
209
{%- for year in years %}
195
- {% if holiday(years=year) %}
196
- if years.is_none() || years.unwrap().contains(&{{year}}) {
197
- let mut m = BTreeMap::new();
210
+ {% if holiday(years=year) %}
211
+ build_year(
212
+ years,
213
+ {{year}},
214
+ vec![
198
215
{% for date, name in holiday(years=year).items() %}
199
- let date = NaiveDate::from_ymd_res({{date|year}}, {{date|month}}, {{date|day}})?;
200
- m.insert(date, Holiday::new(Country::{{code}}, "{{country}}", date, "{{name}}"));
216
+ (NaiveDate::from_ymd_res({{date|year}}, {{date|month}}, {{date|day}})?, "{{name}}"),
201
217
{%- endfor %}
202
- map.insert({{year}}, m);
203
- }
218
+ ],
219
+ &mut map,
220
+ Country::{{code}},
221
+ "{{country}}",
222
+ );
204
223
{%- endif %}
205
224
{%- endfor %}
206
225
207
226
Ok(map)
208
227
}
209
- }
210
228
"""
211
229
212
230
@@ -234,14 +252,20 @@ def empty_holiday(**kwargs):
234
252
env .filters ["day" ] = lambda d : d .day
235
253
env .filters ["escape" ] = escape
236
254
env .filters ["lower" ] = lower
237
- with open ("src/data .rs" , "w" ) as f :
255
+ with open ("src/country .rs" , "w" ) as f :
238
256
rendered = env .from_string (country ).render (countries = countries )
239
257
f .write (rendered )
240
258
259
+ with open ("src/build.rs" , "w" ) as f :
241
260
rendered = env .from_string (build ).render (countries = countries )
242
261
f .write (rendered )
243
-
244
- for country in countries :
262
+
263
+ with open ("src/data/mod.rs" , "w" ) as f :
264
+ rendered = env .from_string (country_mod ).render (countries = countries )
265
+ f .write (rendered )
266
+
267
+ for country in countries :
268
+ with open ("src/data/{}.rs" .format (country .code .lower ()), "w" ) as f :
245
269
holiday = getattr (holidays , country .code , None )
246
270
rendered = env .from_string (build_country ).render (
247
271
code = country .code ,
0 commit comments