Skip to content

Commit

Permalink
add feature-gated case-insensitive timezone parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Petros Angelatos <petrosagg@gmail.com>
  • Loading branch information
petrosagg committed Sep 6, 2021
1 parent b739bce commit 8d82b2c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ license = "MIT/Apache-2.0"
[dependencies]
chrono = { version = "0.4", default-features = false }
serde = { version = "1", optional = true, default-features = false }
phf = { version = "0.10", default-features = false }
phf = { version = "0.10", default-features = false, features = ["uncased"] }
uncased = { version = "0.9", optional = true, default-features = false }

[features]
default = ["std"]
std = []
filter-by-regex = ["chrono-tz-build/filter-by-regex"]
uncased_09 = ["uncased", "chrono-tz-build/uncased_09"]

[build-dependencies]
chrono-tz-build = { path = "./chrono-tz-build", version = "0.0.1" }
Expand Down
6 changes: 4 additions & 2 deletions chrono-tz-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ documentation = "https://docs.rs/chrono-tz-build"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
filter-by-regex = ["regex"]
uncased_09 = ["uncased"]

[dependencies]
parse-zoneinfo = { version = "0.3" }
regex = { default-features = false, version = "1", optional = true }
phf_codegen = "0.10"
phf = "0.10"
phf_codegen = { version = "0.10", default-features = false }
phf = { version = "0.10", features = ["uncased"] }
uncased = { version = "0.9", optional = true, default-features = false }
36 changes: 34 additions & 2 deletions chrono-tz-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ fn write_timezone_file(timezone_file: &mut File, table: &Table) -> io::Result<()
}
writeln!(timezone_file, "static TIMEZONES: ::phf::Map<&'static str, Tz> = \n{};", map.build())?;

#[cfg(feature = "uncased_09")]
{
writeln!(timezone_file, "use uncased::UncasedStr;\n",)?;
let mut map = phf_codegen::Map::new();
for zone in &zones {
map.entry(uncased::UncasedStr::new(zone), &format!("Tz::{}", convert_bad_chars(zone)));
}
writeln!(
timezone_file,
"static TIMEZONES_UNCASED: ::phf::Map<&'static uncased::UncasedStr, Tz> = \n{};",
// FIXME(petrosagg): remove this once rust-phf/rust-phf#232 is released
map.build().to_string().replace("::std::mem::transmute", "::core::mem::transmute")
)?;
}

writeln!(
timezone_file,
"#[cfg(feature = \"std\")]
Expand Down Expand Up @@ -136,9 +151,26 @@ impl FromStr for Tz {{
writeln!(
timezone_file,
" }}
}}
}}\n"
}}"
)?;

#[cfg(feature = "uncased_09")]
{
writeln!(
timezone_file,
r#"
#[cfg(feature = "uncased_09")]
/// Parses a timezone string in a case-insensitive way
pub fn from_str_insensitive(s: &str) -> Result<Self, ParseError> {{
#[cfg(feature = "std")]
return TIMEZONES_UNCASED.get(s.into()).cloned().ok_or_else(|| format!("'{{}}' is not a valid timezone", s));
#[cfg(not(feature = "std"))]
return TIMEZONES_UNCASED.get(s.into()).cloned().ok_or("received invalid timezone");
}}"#)?;
}

writeln!(timezone_file, "}}")?;

writeln!(
timezone_file,
"impl Debug for Tz {{
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ extern crate std as core;

extern crate chrono;
extern crate phf;
#[cfg(feature = "uncased_09")]
extern crate uncased;

#[cfg(feature = "serde")]
mod serde;
Expand Down

0 comments on commit 8d82b2c

Please sign in to comment.