extern crate lalrpop;
use chrono_tz::TZ_VARIANTS;
use std::{io::Write, path::PathBuf};
const TIMEZONE_DOCS: &str = r#"### Timezones extracted from TZ data
### Never use the actual values, they are going to change.
###
### Usage:
###
### ```tremor
### use std::datetime;
### let date_str = datetime::format(datetime::with_timezone(0, datetime::timezones::EUROPE_BERLIN), datetime::formats::RFC3339);
### ```
"#;
fn create_timezones_tremor(file: &mut Vec<u8>) {
file.write_all(TIMEZONE_DOCS.as_bytes()).unwrap();
for (index, tz) in TZ_VARIANTS.iter().enumerate() {
let name = tz.name();
let ident_name = tz
.name()
.replace("GMT+", "GMT_PLUS_")
.replace("GMT-", "GMT_MINUS_")
.chars()
.map(|c| if c.is_alphanumeric() { c } else { '_' })
.map(|c| c.to_ascii_uppercase())
.collect::<String>();
let docstring = format!("## Timezone name constant for {name}\n");
let line = format!("const {ident_name} = {index};\n");
file.write_all(docstring.as_bytes())
.expect("expected writing to timezones.tremor to succeed");
file.write_all(line.as_bytes())
.expect("expected writing to timezones.tremor to succeed");
}
}
fn maybe_rewrite_file<G>(path: &[&str], generator: G)
where
G: Fn(&mut Vec<u8>),
{
let mut generated = Vec::new();
generator(&mut generated);
let mut stdlib_file = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
for p in path {
stdlib_file.push(p);
}
let stdlib = std::fs::read(&stdlib_file).unwrap_or_else(|_| {
panic!(
"Expected {} to exist in the stdlib",
stdlib_file.to_string_lossy()
)
});
if stdlib != generated {
std::fs::write(&stdlib_file, &generated).unwrap_or_else(|_| {
panic!(
"Expected to be able to write to {}",
stdlib_file.to_string_lossy()
)
});
}
}
fn main() {
lalrpop::Configuration::new()
.use_cargo_dir_conventions()
.process()
.expect("Unable to initialize LALRPOP");
println!("cargo:rustc-cfg=can_join_spans");
println!("cargo:rustc-cfg=can_show_location_of_runtime_parse_error");
maybe_rewrite_file(
&["lib", "std", "datetime", "timezones.tremor"],
create_timezones_tremor,
);
}