Use system tzinfo instead of compiling it into the binary

This commit is contained in:
Joscha 2021-11-22 00:42:55 +01:00
parent 36aa6abcb9
commit d7df47eba4
6 changed files with 46 additions and 207 deletions

View file

@ -2,7 +2,8 @@ use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::{fs, io, result};
use chrono_tz::Tz;
use chrono::{DateTime, Utc};
use tzfile::Tz;
use self::commands::File;
@ -13,7 +14,7 @@ mod parse;
#[derive(Debug)]
pub struct Files {
files: HashMap<PathBuf, File>,
timezone: Option<Tz>,
timezone: Tz,
}
#[derive(Debug, thiserror::Error)]
@ -25,9 +26,9 @@ pub enum Error {
#[error("{file1} has time zone {tz1} but {file2} has time zone {tz2}")]
TzConflict {
file1: PathBuf,
tz1: Tz,
tz1: String,
file2: PathBuf,
tz2: Tz,
tz2: String,
},
}
@ -35,20 +36,15 @@ pub type Result<T> = result::Result<T, Error>;
impl Files {
pub fn load(path: &Path) -> Result<Self> {
let mut new = Self {
files: HashMap::new(),
timezone: None,
};
new.load_file(path)?;
new.determine_timezone()?;
Ok(new)
let mut files = HashMap::new();
Self::load_file(&mut files, path)?;
let timezone = Self::determine_timezone(&files)?;
Ok(Self { files, timezone })
}
fn load_file(&mut self, path: &Path) -> Result<()> {
fn load_file(files: &mut HashMap<PathBuf, File>, path: &Path) -> Result<()> {
let canon_path = path.canonicalize()?;
if self.files.contains_key(&canon_path) {
if files.contains_key(&canon_path) {
// We've already loaded this exact file.
return Ok(());
}
@ -57,39 +53,43 @@ impl Files {
let file = parse::parse(path, &content)?;
let includes = file.includes.clone();
self.files.insert(canon_path, file);
files.insert(canon_path, file);
for include in includes {
self.load_file(&include)?;
Self::load_file(files, &include)?;
}
Ok(())
}
fn determine_timezone(&mut self) -> Result<()> {
let mut found: Option<(PathBuf, Tz)> = None;
fn determine_timezone(files: &HashMap<PathBuf, File>) -> Result<Tz> {
let mut found: Option<(PathBuf, String)> = None;
for file in self.files.values() {
if let Some(file_tz) = file.timezone {
for file in files.values() {
if let Some(file_tz) = &file.timezone {
if let Some((found_name, found_tz)) = &found {
if *found_tz != file_tz {
if found_tz != file_tz {
return Err(Error::TzConflict {
file1: found_name.clone(),
tz1: *found_tz,
tz1: found_tz.clone(),
file2: file.name.clone(),
tz2: file_tz,
tz2: file_tz.clone(),
});
}
} else {
found = Some((file.name.clone(), file_tz));
found = Some((file.name.clone(), file_tz.clone()));
}
}
}
if let Some((_, tz)) = found {
self.timezone = Some(tz);
}
Ok(if let Some((_, tz)) = found {
Tz::named(&tz)?
} else {
Tz::local()?
})
}
Ok(())
pub fn now(&self) -> DateTime<&Tz> {
Utc::now().with_timezone(&&self.timezone)
}
}

View file

@ -1,7 +1,6 @@
use std::path::PathBuf;
use chrono::NaiveDate;
use chrono_tz::Tz;
#[derive(Debug)]
pub struct Time {
@ -331,6 +330,6 @@ pub enum Command {
pub struct File {
pub name: PathBuf,
pub includes: Vec<PathBuf>,
pub timezone: Option<Tz>,
pub timezone: Option<String>,
pub commands: Vec<Command>,
}

View file

@ -2,7 +2,6 @@ use std::path::{Path, PathBuf};
use std::result;
use chrono::NaiveDate;
use chrono_tz::Tz;
use pest::error::ErrorVariant;
use pest::iterators::Pair;
use pest::prec_climber::{Assoc, Operator, PrecClimber};
@ -38,15 +37,9 @@ fn parse_include(p: Pair<Rule>) -> String {
p.into_inner().next().unwrap().as_str().to_string()
}
fn parse_timezone(p: Pair<Rule>) -> Result<Tz> {
fn parse_timezone(p: Pair<Rule>) -> String {
assert_eq!(p.as_rule(), Rule::timezone);
let span = p.as_span();
p.into_inner()
.next()
.unwrap()
.as_str()
.parse()
.map_err(|_| error(span, "invalid timezone"))
p.into_inner().next().unwrap().as_str().trim().to_string()
}
fn parse_number(p: Pair<Rule>) -> i32 {
@ -720,7 +713,7 @@ fn parse_command(p: Pair<Rule>, file: &mut File) -> Result<()> {
file.includes.push(parent.join(parse_include(p)));
}
Rule::timezone => match file.timezone {
None => file.timezone = Some(parse_timezone(p)?),
None => file.timezone = Some(parse_timezone(p)),
Some(_) => fail(p.as_span(), "cannot set timezone multiple times")?,
},
Rule::task => file.commands.push(Command::Task(parse_task(p)?)),

View file

@ -16,6 +16,6 @@ pub struct Opt {
fn main() -> anyhow::Result<()> {
let opt = Opt::from_args();
let files = Files::load(&opt.file)?;
println!("{:#?}", files);
println!("{}", files.now().format("%F %T %Z"));
Ok(())
}