Skip to content

Commit

Permalink
Return an iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Oct 20, 2023
1 parent a32e66d commit b00fd3e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 121 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions provider/datagen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ num-bigint = { version = "0.4.4", default-features = false }
clap = { version = "4", optional = true, features = ["derive"] }
eyre = { version = "0.6", optional = true }
simple_logger = { version = "4.1.0", default-features = false, optional = true }
either = "1.9.0"

[dev-dependencies]
crlify = { path = "../../utils/crlify" }
Expand Down
34 changes: 19 additions & 15 deletions provider/datagen/src/transform/cldr/calendar/japanese.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,23 @@ impl IterableDataProvider<JapaneseExtendedErasV1Marker> for crate::DatagenProvid
}
}

/// Computes the japanese era code map
/// Somewhat expensive, prefer caching
pub fn compute_era_code_map() -> BTreeMap<String, TinyStr16> {
let snapshot: JapaneseErasV1 = serde_json::from_str(JAPANEXT_FILE)
.expect("Failed to parse the precached golden. This is a bug.");
let mut map: BTreeMap<_, _> = snapshot
.dates_to_eras
.iter()
.enumerate()
.map(|(i, (_, value))| (i.to_string(), value))
.collect();
// Splice in details about gregorian eras for pre-meiji dates
map.insert("-2".to_string(), tinystr!(16, "bce"));
map.insert("-1".to_string(), tinystr!(16, "ce"));
map
/// Computes the japanese era code map or loads from static cache
pub fn get_era_code_map() -> &'static BTreeMap<String, TinyStr16> {
static MAP: once_cell::sync::OnceCell<BTreeMap<String, TinyStr16>> =
once_cell::sync::OnceCell::new();

MAP.get_or_init(|| {
let snapshot: JapaneseErasV1 = serde_json::from_str(JAPANEXT_FILE)
.expect("Failed to parse the precached golden. This is a bug.");
let mut map: BTreeMap<_, _> = snapshot
.dates_to_eras
.iter()
.enumerate()
.map(|(i, (_, value))| (i.to_string(), value))
.collect();
// Splice in details about gregorian eras for pre-meiji dates
map.insert("-2".to_string(), tinystr!(16, "bce"));
map.insert("-1".to_string(), tinystr!(16, "ce"));
map
})
}
146 changes: 40 additions & 106 deletions provider/datagen/src/transform/cldr/datetime/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ fn convert_eras(eras: &cldr_serde::ca::Eras, calendar: &str) -> Eras<'static> {
let mut out_eras = Eras::default();

for (cldr, code) in map {
if let Some(name) = eras.names.get(cldr) {
if let Some(name) = eras.names.get(&*cldr) {
out_eras.names.insert(code.as_str().into(), name);
}
if let Some(abbr) = eras.abbr.get(cldr) {
if let Some(abbr) = eras.abbr.get(&*cldr) {
out_eras.abbr.insert(code.as_str().into(), abbr);
}
if let Some(narrow) = eras.narrow.get(cldr) {
if let Some(narrow) = eras.narrow.get(&*cldr) {
out_eras.narrow.insert(code.as_str().into(), narrow);
}
}
Expand Down Expand Up @@ -86,109 +86,43 @@ fn get_month_code_map(calendar: &str) -> &'static [TinyStr4] {
}
}

fn get_era_code_map(calendar: &str) -> &'static BTreeMap<String, TinyStr16> {
static MAP: once_cell::sync::OnceCell<BTreeMap<String, BTreeMap<String, TinyStr16>>> =
once_cell::sync::OnceCell::new();
let map = MAP.get_or_init(|| {
let japanese = crate::transform::cldr::calendar::japanese::compute_era_code_map();
[
(
"gregory".to_string(),
[
("0".to_string(), tinystr!(16, "bce")),
("1".to_string(), tinystr!(16, "ce")),
]
.into_iter()
.collect(),
),
(
"buddhist".to_string(),
[("0".to_string(), tinystr!(16, "be"))]
.into_iter()
.collect(),
),
("chinese".to_string(), [].into_iter().collect()),
("japanese".to_string(), japanese.clone()),
("japanext".to_string(), japanese),
(
"coptic".to_string(),
[
// Before Diocletian
("0".to_string(), tinystr!(16, "bd")),
// Anno Diocletian/After Diocletian
("1".to_string(), tinystr!(16, "ad")),
]
.into_iter()
.collect(),
),
("dangi".to_string(), [].into_iter().collect()),
(
"indian".to_string(),
[("0".to_string(), tinystr!(16, "saka"))]
.into_iter()
.collect(),
),
(
"islamic".to_string(),
[("0".to_string(), tinystr!(16, "islamic"))]
.into_iter()
.collect(),
),
(
"islamicc".to_string(),
[("0".to_string(), tinystr!(16, "islamic"))]
.into_iter()
.collect(),
),
(
"umalqura".to_string(),
[("0".to_string(), tinystr!(16, "islamic"))]
.into_iter()
.collect(),
),
(
"tbla".to_string(),
[("0".to_string(), tinystr!(16, "islamic"))]
.into_iter()
.collect(),
),
(
"persian".to_string(),
[("0".to_string(), tinystr!(16, "ah"))]
.into_iter()
.collect(),
),
(
"hebrew".to_string(),
[("0".to_string(), tinystr!(16, "hebrew"))]
.into_iter()
.collect(),
),
(
"ethiopic".to_string(),
[
("0".to_string(), tinystr!(16, "incar")),
("1".to_string(), tinystr!(16, "pre-incar")),
("2".to_string(), tinystr!(16, "mundi")),
]
.into_iter()
.collect(),
),
(
"roc".to_string(),
[
("0".to_string(), tinystr!(16, "roc-inverse")),
("1".to_string(), tinystr!(16, "roc")),
]
.into_iter()
.collect(),
),
]
.into_iter()
.collect()
});
map.get(calendar)
.unwrap_or_else(|| panic!("Era map unknown for {}", calendar))
fn get_era_code_map(calendar: &str) -> impl Iterator<Item = (&str, TinyStr16)> {
use either::Either;

let array: &[_] = match calendar {
"gregory" => &[("0", tinystr!(16, "bce")), ("1", tinystr!(16, "ce"))],
"buddhist" => &[("0", tinystr!(16, "be"))],
"japanese" | "japanext" => {
return Either::Right(
crate::transform::cldr::calendar::japanese::get_era_code_map()
.iter()
.map(|(k, v)| (&**k, *v)),
)
}
"coptic" => &[
// Before Diocletian
("0", tinystr!(16, "bd")),
// Anno Diocletian/After Diocletian
("1", tinystr!(16, "ad")),
],
"dangi" | "chinese" => &[],
"indian" => &[("0", tinystr!(16, "saka"))],
"islamic" | "islamicc" | "umalqura" | "tbla" => &[("0", tinystr!(16, "islamic"))],
"persian" => &[("0", tinystr!(16, "ah"))],
"hebrew" => &[("0", tinystr!(16, "hebrew"))],
"ethiopic" => &[
("0", tinystr!(16, "incar")),
("1", tinystr!(16, "pre-incar")),
("2", tinystr!(16, "mundi")),
],
"roc" => &[
("0", tinystr!(16, "roc-inverse")),
("1", tinystr!(16, "roc")),
],
_ => panic!("Era map unknown for {calendar}"),
};

Either::Left(array.iter().copied())
}

macro_rules! symbols_from {
Expand Down

0 comments on commit b00fd3e

Please sign in to comment.