Skip to content

Commit

Permalink
make timezone parsing case insensitive
Browse files Browse the repository at this point in the history
Signed-off-by: Petros Angelatos <petrosagg@gmail.com>
  • Loading branch information
petrosagg committed Aug 19, 2021
1 parent 822b939 commit 06b6cd8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ 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", default-features = false }

[features]
default = ["std"]
Expand Down
5 changes: 3 additions & 2 deletions chrono-tz-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ filter-by-regex = ["regex"]
[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 = "0.9"
16 changes: 12 additions & 4 deletions chrono-tz-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use parse_zoneinfo::table::{Table, TableBuilder};
use parse_zoneinfo::transitions::FixedTimespan;
use parse_zoneinfo::transitions::TableTransitions;

use uncased::UncasedStr;

/// The name of the environment variable which possibly holds the filter regex.
const FILTER_ENV_VAR_NAME: &str = "CHRONO_TZ_TIMEZONE_FILTER";

Expand Down Expand Up @@ -70,6 +72,7 @@ fn write_timezone_file(timezone_file: &mut File, table: &Table) -> io::Result<()
let zones = table.zonesets.keys().chain(table.links.keys()).collect::<BTreeSet<_>>();
writeln!(timezone_file, "use core::fmt::{{self, Debug, Display, Formatter}};",)?;
writeln!(timezone_file, "use core::str::FromStr;\n",)?;
writeln!(timezone_file, "use uncased::UncasedStr;\n",)?;
writeln!(
timezone_file,
"use ::timezone_impl::{{TimeSpans, FixedTimespanSet, FixedTimespan}};\n",
Expand All @@ -96,9 +99,14 @@ fn write_timezone_file(timezone_file: &mut File, table: &Table) -> io::Result<()

let mut map = phf_codegen::Map::new();
for zone in &zones {
map.entry(zone, &format!("Tz::{}", convert_bad_chars(zone)));
map.entry(UncasedStr::new(zone), &format!("Tz::{}", convert_bad_chars(zone)));
}
writeln!(timezone_file, "static TIMEZONES: ::phf::Map<&'static str, Tz> = \n{};", map.build())?;
writeln!(
timezone_file,
"static TIMEZONES: ::phf::Map<&'static 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,
Expand All @@ -109,9 +117,9 @@ fn write_timezone_file(timezone_file: &mut File, table: &Table) -> io::Result<()
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {{
#[cfg(feature = \"std\")]
return TIMEZONES.get(s).cloned().ok_or_else(|| format!(\"'{{}}' is not a valid timezone\", s));
return TIMEZONES.get(s.into()).cloned().ok_or_else(|| format!(\"'{{}}' is not a valid timezone\", s));
#[cfg(not(feature = \"std\"))]
return TIMEZONES.get(s).cloned().ok_or(\"received invalid timezone\");
return TIMEZONES.get(s.into()).cloned().ok_or(\"received invalid timezone\");
}}
}}\n"
)?;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ extern crate std as core;

extern crate chrono;
extern crate phf;
extern crate uncased;

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

0 comments on commit 06b6cd8

Please sign in to comment.