Skip to content

Generate the baked zone info data for ZoneInfoProvider #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: impl-zoneinfo-support
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 7 additions & 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 bakeddata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ publish = false
databake = "0.2.0"
serde_json = "1.0.140"
temporal_provider = { workspace = true }
rustc-hash = "2.1.1"
65 changes: 52 additions & 13 deletions bakeddata/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use databake::{quote, Bake};
use rustc_hash::FxHasher;
use std::{
collections::{BTreeMap, BTreeSet, HashMap},
fs::{self, File},
hash::{Hash, Hasher},
io::{self, BufWriter, Write},
path::Path,
};
use temporal_provider::{tzif::ZoneInfoProvider, IanaIdentifierNormalizer};
use temporal_provider::{
tzif::{ZeroTzifULE, ZoneInfoProvider},
IanaIdentifierNormalizer,
};

trait BakedDataProvider {
fn write_data(&self, data_path: &Path) -> io::Result<()>;
Expand All @@ -20,7 +26,7 @@ impl BakedDataProvider for ZoneInfoProvider<'_> {

let baked_macro = quote! {
#[macro_export]
macro_rules! zone_info_provider {
macro_rules! zone_info_provider_baked {
() => {
pub const ZONE_INFO_PROVIDER: &'static temporal_provider::ZoneInfoProvider = &#baked;
}
Expand All @@ -41,26 +47,59 @@ impl BakedDataProvider for ZoneInfoProvider<'_> {
// Recreate directory.
fs::create_dir_all(zoneinfo_debug_path.clone())?;

let map_file = zoneinfo_debug_path.join("map.json");

// Create id sets for the tzifs
let mut tzif_ids: HashMap<usize, BTreeSet<String>> = HashMap::new();
for (identifier, index) in self.ids.to_btreemap().iter() {
let (directory, filename) = if identifier.contains('/') {
let (directory, filename) = identifier.rsplit_once('/').expect("'/' must exist");
let identifier_dir = zoneinfo_debug_path.join(directory);
fs::create_dir_all(identifier_dir.clone())?;
(identifier_dir, filename)
if let Some(id_set) = tzif_ids.get_mut(index) {
id_set.insert(identifier.clone());
} else {
(zoneinfo_debug_path.clone(), identifier.as_str())
};
let mut filepath = directory.join(filename);
filepath.set_extension("json");
let json = serde_json::to_string_pretty(&self.tzifs[*index])?;
fs::write(filepath, json)?;
tzif_ids.insert(*index, BTreeSet::from([identifier.clone()]));
}
}

let tzif_dir_path = zoneinfo_debug_path.join("tzifs");
fs::create_dir_all(tzif_dir_path.clone())?;

let mut id_map: BTreeMap<String, String> = BTreeMap::new();
for (id, tzif) in self.tzifs.iter().enumerate() {
let mut tzif_data = serde_json::Map::new();
let id_set = tzif_ids.get(&id).unwrap();
tzif_data.insert("ids".into(), serde_json::to_value(id_set)?);
tzif_data.insert("tzif".into(), serde_json::to_value(&tzif)?);
let filename = format!("tzif-{}-{}.json", hash_ids(id_set), hash_tzif(&tzif));
let filepath = tzif_dir_path.join(filename.clone());
for id in id_set {
id_map.insert(id.clone(), filename.clone());
}
fs::write(filepath, serde_json::to_string_pretty(&tzif_data)?)?;
}

fs::write(
map_file,
format!("{}\n", serde_json::to_string_pretty(&id_map)?),
)?;

// TODO: Add version
Ok(())
}
}

fn hash_ids(set: &BTreeSet<String>) -> String {
let mut hasher = FxHasher::default();
set.hash(&mut hasher);
format!("{:x}", hasher.finish())
}

fn hash_tzif(tzif: &ZeroTzifULE) -> String {
let mut hasher = FxHasher::default();
tzif.transitions().as_bytes().hash(&mut hasher);
tzif.types().as_bytes().hash(&mut hasher);
tzif.posix().as_bytes().hash(&mut hasher);
format!("{:x}", hasher.finish())
}

impl BakedDataProvider for IanaIdentifierNormalizer<'_> {
fn write_data(&self, data_path: &Path) -> io::Result<()> {
fs::create_dir_all(data_path)?;
Expand Down
Loading