Skip to content

Commit 8a7b626

Browse files
authored
Merge pull request #11 from AD4MANTIS/main
Replace include macro with normal per country module files
2 parents e2b65e9 + 56ecf8a commit 8a7b626

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+115632
-109095
lines changed

gen.py

+51-27
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ class Country:
116116
years = range(2000, 2031)
117117

118118
country = """
119+
use crate::Error;
120+
119121
/// Two-letter country codes defined in ISO 3166-1 alpha-2 .
120122
#[allow(dead_code)]
121123
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, PartialOrd, Ord)]
@@ -127,15 +129,10 @@ class Country:
127129
{%- endfor %}
128130
}
129131
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())
137135
}
138-
}
139136
}
140137
141138
impl AsRef<str> for Country {
@@ -153,21 +150,25 @@ class Country:
153150
type Err = Error;
154151
155152
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
156-
match s {
153+
Ok(match s {
157154
{%- for country in countries %}
158155
#[cfg(feature = "{{country.code}}")]
159-
"{{country.code}}" => Ok(Country::{{country.code}}),
156+
"{{country.code}}" => Country::{{country.code}},
160157
{%- endfor %}
161-
_ => Err(Error::CountryNotAvailable),
162-
}
158+
_ => return Err(Error::CountryNotAvailable),
159+
})
163160
}
164161
}
165162
166163
"""
167164

168165
build = """
166+
use std::collections::HashSet;
167+
168+
use crate::{data::*, prelude::*, HolidayMap, Result, Year};
169+
169170
/// 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> {
171172
let mut map = HolidayMap::new();
172173
{% for country in countries %}
173174
#[cfg(feature = "{{country.code}}")]
@@ -180,10 +181,24 @@ class Country:
180181
181182
"""
182183

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+
183200
build_country = """
184-
/// {{country}}.
185-
#[cfg(feature = "{{code}}")]
186-
pub mod {{code|escape}} {
201+
//! {{country}}
187202
use super::*;
188203
189204
/// Generate holiday map for {{country}}.
@@ -192,21 +207,24 @@ class Country:
192207
let mut map = HashMap::new();
193208
194209
{%- 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![
198215
{% 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}}"),
201217
{%- endfor %}
202-
map.insert({{year}}, m);
203-
}
218+
],
219+
&mut map,
220+
Country::{{code}},
221+
"{{country}}",
222+
);
204223
{%- endif %}
205224
{%- endfor %}
206225
207226
Ok(map)
208227
}
209-
}
210228
"""
211229

212230

@@ -234,14 +252,20 @@ def empty_holiday(**kwargs):
234252
env.filters["day"] = lambda d: d.day
235253
env.filters["escape"] = escape
236254
env.filters["lower"] = lower
237-
with open("src/data.rs", "w") as f:
255+
with open("src/country.rs", "w") as f:
238256
rendered = env.from_string(country).render(countries=countries)
239257
f.write(rendered)
240258

259+
with open("src/build.rs", "w") as f:
241260
rendered = env.from_string(build).render(countries=countries)
242261
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:
245269
holiday = getattr(holidays, country.code, None)
246270
rendered = env.from_string(build_country).render(
247271
code=country.code,

0 commit comments

Comments
 (0)