Skip to content

Commit

Permalink
Revert to default style
Browse files Browse the repository at this point in the history
  • Loading branch information
djc authored and pitdicker committed Apr 15, 2024
1 parent d99e9a6 commit 7e87927
Show file tree
Hide file tree
Showing 11 changed files with 528 additions and 148 deletions.
75 changes: 62 additions & 13 deletions chrono-tz-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ fn strip_comments(mut line: String) -> String {
// to this zone, as a string representation of a static slice.
fn format_rest(rest: Vec<(i64, FixedTimespan)>) -> String {
let mut ret = "&[\n".to_string();
for (start, FixedTimespan { utc_offset, dst_offset, name }) in rest {
for (
start,
FixedTimespan {
utc_offset,
dst_offset,
name,
},
) in rest
{
ret.push_str(&format!(
" ({start}, FixedTimespan {{ \
utc_offset: {utc}, dst_offset: {dst}, name: \"{name}\" \
Expand All @@ -53,7 +61,12 @@ fn format_rest(rest: Vec<(i64, FixedTimespan)>) -> String {
fn convert_bad_chars(name: &str) -> String {
let name = name.replace('/', "__").replace('+', "Plus");
if let Some(pos) = name.find('-') {
if name[pos + 1..].chars().next().map(char::is_numeric).unwrap_or(false) {
if name[pos + 1..]
.chars()
.next()
.map(char::is_numeric)
.unwrap_or(false)
{
name.replace('-', "Minus")
} else {
name.replace('-', "")
Expand All @@ -67,8 +80,15 @@ fn convert_bad_chars(name: &str) -> String {
// database. The `Wrap` wrapper in the `timezone_impl` module then implements
// TimeZone for any contained struct that implements `Timespans`.
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}};",)?;
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,
Expand All @@ -83,7 +103,10 @@ fn write_timezone_file(timezone_file: &mut File, table: &Table) -> io::Result<()
/// for details."
)?;
writeln!(timezone_file, "#[derive(Clone, Copy, PartialEq, Eq, Hash)]")?;
writeln!(timezone_file, r#"#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]"#)?;
writeln!(
timezone_file,
r#"#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]"#
)?;
writeln!(timezone_file, "pub enum Tz {{")?;
for zone in &zones {
let zone_name = convert_bad_chars(zone);
Expand All @@ -100,20 +123,29 @@ fn write_timezone_file(timezone_file: &mut File, table: &Table) -> io::Result<()
for zone in &zones {
map.entry(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 str, Tz> = \n{};",
map.build()
)?;

#[cfg(feature = "case-insensitive")]
{
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)));
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")
map.build()
.to_string()
.replace("::std::mem::transmute", "::core::mem::transmute")
)?;
}

Expand Down Expand Up @@ -242,7 +274,11 @@ pub static TZ_VARIANTS: [Tz; {num}] = [
num = zones.len()
)?;
for zone in &zones {
writeln!(timezone_file, " Tz::{zone},", zone = convert_bad_chars(zone))?;
writeln!(
timezone_file,
" Tz::{zone},",
zone = convert_bad_chars(zone)
)?;
}
write!(timezone_file, "];")?;
Ok(())
Expand All @@ -252,7 +288,10 @@ pub static TZ_VARIANTS: [Tz; {num}] = [
// instead of having to use chrono_tz::timezones::Europe__London
fn write_directory_file(directory_file: &mut File, table: &Table, version: &str) -> io::Result<()> {
// expose the underlying IANA TZDB version
writeln!(directory_file, "pub const IANA_TZDB_VERSION : &str = \"{version}\";\n")?;
writeln!(
directory_file,
"pub const IANA_TZDB_VERSION : &str = \"{version}\";\n"
)?;
// add the `loose' zone definitions first
writeln!(directory_file, "use crate::timezones::Tz;\n")?;
let zones = table
Expand All @@ -263,7 +302,11 @@ fn write_directory_file(directory_file: &mut File, table: &Table, version: &str)
.collect::<BTreeSet<_>>();
for zone in zones {
let zone = convert_bad_chars(zone);
writeln!(directory_file, "pub const {name} : Tz = Tz::{name};", name = zone)?;
writeln!(
directory_file,
"pub const {name} : Tz = Tz::{name};",
name = zone
)?;
}
writeln!(directory_file)?;

Expand All @@ -279,7 +322,11 @@ fn write_directory_file(directory_file: &mut File, table: &Table, version: &str)
match child {
Child::Submodule(name) => {
let submodule_name = convert_bad_chars(name);
writeln!(directory_file, " pub mod {name} {{", name = submodule_name)?;
writeln!(
directory_file,
" pub mod {name} {{",
name = submodule_name
)?;
writeln!(directory_file, " use crate::timezones::Tz;\n",)?;
let full_name = entry.name.to_string() + "/" + name;
for entry in table.structure() {
Expand Down Expand Up @@ -428,7 +475,9 @@ mod filter {
}

// Actually do the filtering.
table.links.retain(|k, v| keep.contains(k) || keep.contains(v));
table
.links
.retain(|k, v| keep.contains(k) || keep.contains(v));

table
.zonesets
Expand Down
6 changes: 5 additions & 1 deletion chrono-tz/src/binary_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ fn test_binary_search() {
assert_eq!(binary_search(30, 50, |x| x.cmp(&42)), Ok(42));
assert_eq!(binary_search(300, 500, |x| x.cmp(&42)), Err(300));
assert_eq!(
binary_search(0, 500, |x| if x < 42 { Ordering::Less } else { Ordering::Greater }),
binary_search(0, 500, |x| if x < 42 {
Ordering::Less
} else {
Ordering::Greater
}),
Err(42)
);
}
Loading

0 comments on commit 7e87927

Please sign in to comment.