62 lines
1.3 KiB
Rust
62 lines
1.3 KiB
Rust
use std::collections::HashMap;
|
|
use std::path::{Path, PathBuf};
|
|
use std::{fs, io, result};
|
|
|
|
use chrono_tz::Tz;
|
|
|
|
use self::commands::File;
|
|
|
|
pub mod commands;
|
|
mod format;
|
|
mod parse;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Files {
|
|
files: HashMap<PathBuf, File>,
|
|
timezone: Option<Tz>,
|
|
}
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum Error {
|
|
#[error("{0}")]
|
|
IoError(#[from] io::Error),
|
|
#[error("{0}")]
|
|
ParseError(#[from] parse::Error),
|
|
}
|
|
|
|
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.to_owned())?;
|
|
new.determine_timezone()?;
|
|
|
|
Ok(new)
|
|
}
|
|
|
|
fn load_file(&mut self, path: PathBuf) -> Result<()> {
|
|
let canon_path = path.canonicalize()?;
|
|
if self.files.contains_key(&canon_path) {
|
|
// We've already loaded this exact file.
|
|
return Ok(());
|
|
}
|
|
|
|
let content = fs::read_to_string(&path)?;
|
|
let file = parse::parse(path, &content)?;
|
|
self.files.insert(canon_path, file);
|
|
|
|
// TODO Also load all included files
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn determine_timezone(&mut self) -> Result<()> {
|
|
// TODO Implement once files can specify time zones
|
|
Ok(())
|
|
}
|
|
}
|