|
| 1 | +//! A compact, zero copy TZif file. |
| 2 | +//! |
| 3 | +//! NOTE: This representation does not follow the TZif specification |
| 4 | +//! to full detail, but instead attempts to compress TZif data into |
| 5 | +//! a functional, data driven equivalent. |
| 6 | +
|
| 7 | +use parse_zoneinfo::table::Table; |
| 8 | +use std::{ |
| 9 | + borrow::Cow, |
| 10 | + collections::{BTreeMap, BTreeSet}, |
| 11 | + path::Path, |
| 12 | +}; |
| 13 | +use zerotrie::{ZeroTrieBuildError, ZeroTrieSimpleAscii}; |
| 14 | +use zerovec::{VarZeroVec, ZeroVec}; |
| 15 | + |
| 16 | +use crate::tzdb::TzdbDataProvider; |
| 17 | + |
| 18 | +#[derive(Debug, Clone, yoke::Yokeable, databake::Bake, serde::Serialize)] |
| 19 | +#[databake(path = temporal_provider::tzif)] |
| 20 | +pub struct ZeroZoneInfo<'data> { |
| 21 | + // Why u16? It would suck to have to refactor because there are > 256 TZifs |
| 22 | + ids: ZeroTrieSimpleAscii<ZeroVec<'data, u8>>, |
| 23 | + |
| 24 | + tzifs: VarZeroVec<'data, ZeroTzifULE>, |
| 25 | +} |
| 26 | + |
| 27 | +#[zerovec::make_varule(ZeroTzifULE)] |
| 28 | +#[derive(PartialEq, Debug, Clone, yoke::Yokeable, serde::Serialize, databake::Bake)] |
| 29 | +#[zerovec::skip_derive(Ord)] |
| 30 | +#[zerovec::derive(Debug, Serialize)] |
| 31 | +#[databake(path = temporal_provider::tzif)] |
| 32 | +pub struct ZeroTzif<'data> { |
| 33 | + transitions: ZeroVec<'data, i64>, |
| 34 | + // NOTE: zoneinfo64 does a fun little bitmap str |
| 35 | + transition_types: ZeroVec<'data, u32>, |
| 36 | + types: ZeroVec<'data, i64>, |
| 37 | + posix: Cow<'data, str>, |
| 38 | +} |
| 39 | + |
| 40 | +pub enum ZoneInfoDataError { |
| 41 | + Build(ZeroTrieBuildError), |
| 42 | +} |
| 43 | + |
| 44 | +impl ZeroZoneInfo<'_> { |
| 45 | + pub fn build(tzdata: &Path) -> Result<Self, ZoneInfoDataError> { |
| 46 | + let provider = TzdbDataProvider::new(tzdata).unwrap(); |
| 47 | + let mut identifiers = BTreeMap::default(); |
| 48 | + let mut zones_set = BTreeSet::default(); |
| 49 | + for zoneset_id in provider.data.zonesets.keys() { |
| 50 | + let _ = zones_set.insert(zoneset_id.clone()); |
| 51 | + identifiers.insert(zoneset_id.clone(), zoneset_id.clone()); |
| 52 | + } |
| 53 | + for (link, zone) in provider.data.links.iter() { |
| 54 | + let _ = zones_set.insert(zone.clone()); |
| 55 | + identifiers.insert(link.clone(), zone.clone()); |
| 56 | + } |
| 57 | + |
| 58 | + let zones: Vec<String> = zones_set.iter().cloned().collect(); |
| 59 | + |
| 60 | + let identier_map: BTreeMap<Vec<u8>, usize> = identifiers |
| 61 | + .iter() |
| 62 | + .map(|(id, zoneid)| (id.as_bytes().to_vec(), zones.binary_search(zoneid).unwrap())) |
| 63 | + .collect(); |
| 64 | + |
| 65 | + let tzifs: Vec<ZeroTzif<'_>> = zones |
| 66 | + .iter() |
| 67 | + .map(|id| ZeroTzif::build(&provider.data, id)) |
| 68 | + .collect(); |
| 69 | + |
| 70 | + let tzifs_zerovec: VarZeroVec<'static, ZeroTzifULE> = tzifs.as_slice().into(); |
| 71 | + |
| 72 | + let ids = ZeroTrieSimpleAscii::try_from(&identier_map) |
| 73 | + .map_err(ZoneInfoDataError::Build)? |
| 74 | + .convert_store(); |
| 75 | + |
| 76 | + Ok(ZeroZoneInfo { |
| 77 | + ids, |
| 78 | + tzifs: tzifs_zerovec, |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl ZeroTzif<'_> { |
| 84 | + fn build(data: &Table, id: &str) -> Self { |
| 85 | + todo!() |
| 86 | + } |
| 87 | +} |
0 commit comments