Skip to content
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

Use pre-built timezone/directory maps by default. #193

Open
wants to merge 4 commits into
base: main
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions chrono-tz-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::BTreeSet;
use std::env;
use std::fs::File;
use std::io::{self, BufRead, BufReader, Write};
use std::path::Path;
use std::path::{Path, PathBuf};

use parse_zoneinfo::line::{Line, LineParser};
use parse_zoneinfo::structure::{Child, Structure};
Expand All @@ -15,6 +15,7 @@ use parse_zoneinfo::transitions::FixedTimespan;
use parse_zoneinfo::transitions::TableTransitions;

/// The name of the environment variable which possibly holds the filter regex.
#[cfg(feature = "filter-by-regex")]
const FILTER_ENV_VAR_NAME: &str = "CHRONO_TZ_TIMEZONE_FILTER";

// This function is needed until zoneinfo_parse handles comments correctly.
Expand Down Expand Up @@ -135,6 +136,7 @@ fn write_timezone_file(timezone_file: &mut File, table: &Table) -> io::Result<()

#[cfg(feature = "case-insensitive")]
{
writeln!(timezone_file, r#"#[cfg(feature = "case-insensitive")]"#)?;
writeln!(timezone_file, "use uncased::UncasedStr;\n",)?;
let mut map = phf_codegen::Map::new();
for zone in &zones {
Expand All @@ -143,6 +145,7 @@ fn write_timezone_file(timezone_file: &mut File, table: &Table) -> io::Result<()
&format!("Tz::{}", convert_bad_chars(zone)),
);
}
writeln!(timezone_file, r#"#[cfg(feature = "case-insensitive")]"#)?;
writeln!(
timezone_file,
"static TIMEZONES_UNCASED: ::phf::Map<&'static uncased::UncasedStr, Tz> = \n{};",
Expand Down Expand Up @@ -511,9 +514,15 @@ fn detect_iana_db_version() -> String {
unreachable!("no version found")
}

pub fn main() {
println!("cargo:rerun-if-env-changed={}", FILTER_ENV_VAR_NAME);
fn write_source_files(path: &Path, table: &Table, iana_db_version: &str) {
std::fs::create_dir_all(path).unwrap();
let mut timezone_file = File::create(path.join("timezones.rs")).unwrap();
write_timezone_file(&mut timezone_file, &table).unwrap();
let mut directory_file = File::create(path.join("directory.rs")).unwrap();
write_directory_file(&mut directory_file, &table, &iana_db_version).unwrap();
}

pub fn main() {
let parser = LineParser::default();
let mut table = TableBuilder::new();

Expand Down Expand Up @@ -555,13 +564,13 @@ pub fn main() {

let mut table = table.build();
filter::maybe_filter_timezone_table(&mut table);
let iana_db_version = detect_iana_db_version();

let timezone_path = Path::new(&env::var("OUT_DIR").unwrap()).join("timezones.rs");
let mut timezone_file = File::create(timezone_path).unwrap();
write_timezone_file(&mut timezone_file, &table).unwrap();
if env::var("CHRONO_TZ_UPDATE_PREBUILT").as_deref() == Ok("1") {
let prebuilt_path = env::current_dir().unwrap().join("src").join("prebuilt");
write_source_files(&prebuilt_path, &table, &iana_db_version);
}

let directory_path = Path::new(&env::var("OUT_DIR").unwrap()).join("directory.rs");
let mut directory_file = File::create(directory_path).unwrap();
let version = detect_iana_db_version();
write_directory_file(&mut directory_file, &table, &version).unwrap();
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
write_source_files(&out_path, &table, &iana_db_version);
}
4 changes: 2 additions & 2 deletions chrono-tz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ default = ["std"]
std = []
serde = ["dep:serde"]
filter-by-regex = ["chrono-tz-build/filter-by-regex"]
case-insensitive = ["dep:uncased", "chrono-tz-build/case-insensitive", "phf/uncased"]
case-insensitive = ["dep:uncased", "chrono-tz-build?/case-insensitive", "phf/uncased"]

[build-dependencies]
chrono-tz-build = { path = "../chrono-tz-build", version = "0.4" }
chrono-tz-build = { path = "../chrono-tz-build", version = "0.4", optional = true }

[dev-dependencies]
serde_test = "1"
Expand Down
10 changes: 10 additions & 0 deletions chrono-tz/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
fn main() {
println!("cargo:rerun-if-env-changed=CHRONO_TZ_TIMEZONE_FILTER");

#[cfg(feature = "filter-by-regex")]
chrono_tz_build::main();

#[cfg(not(feature = "filter-by-regex"))]
{
if std::env::var("CHRONO_TZ_TIMEZONE_FILTER").is_ok() {
println!("cargo::error=CHRONO_TZ_TIMEZONE_FILTER set without enabling filter-by-regex feature")
}
}
}
5 changes: 5 additions & 0 deletions chrono-tz/src/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(dead_code)]

#[cfg(feature = "filter-by-regex")]
include!(concat!(env!("OUT_DIR"), "/directory.rs"));

#[cfg(not(feature = "filter-by-regex"))]
include!("prebuilt/directory.rs");
Loading