diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-10 15:44:27 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-10 15:44:27 +0100 |
commit | c7cc34f3e956173eb9e03decaf08e0ea6ea28525 (patch) | |
tree | c3941e8a3a2fa6732423e74a8ae79ab4116dc820 | |
parent | b083ea5e110314bb02530e3a660e5a3d5d72d1e0 (diff) | |
download | askama-c7cc34f3e956173eb9e03decaf08e0ea6ea28525.tar.gz askama-c7cc34f3e956173eb9e03decaf08e0ea6ea28525.tar.bz2 askama-c7cc34f3e956173eb9e03decaf08e0ea6ea28525.zip |
Move Config struct to askama_shared crate root
-rw-r--r-- | askama_shared/src/lib.rs | 41 | ||||
-rw-r--r-- | askama_shared/src/path.rs | 40 |
2 files changed, 42 insertions, 39 deletions
diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index 64d8d25..f940021 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -7,10 +7,47 @@ extern crate serde_derive; extern crate serde_json; extern crate toml; +use std::env; +use std::fs; +use std::path::PathBuf; + +mod escaping; +mod error; + pub use error::{Error, Result}; pub use escaping::MarkupDisplay; -mod error; pub mod filters; pub mod path; -mod escaping; +struct Config { + dirs: Vec<PathBuf>, +} + +impl Config { + fn new() -> Config { + let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + let filename = root.join(CONFIG_FILE_NAME); + + let default = vec![root.join("templates")]; + let dirs = if filename.exists() { + let config_str = fs::read_to_string(&filename) + .expect(&format!("unable to read {}", filename.to_str().unwrap())); + let raw: RawConfig = toml::from_str(&config_str) + .expect(&format!("invalid TOML in {}", filename.to_str().unwrap())); + raw.dirs + .map(|dirs| dirs.into_iter().map(|dir| root.join(dir)).collect()) + .unwrap_or_else(|| default) + } else { + default + }; + + Config { dirs } + } +} + +#[derive(Deserialize)] +struct RawConfig { + dirs: Option<Vec<String>>, +} + +static CONFIG_FILE_NAME: &str = "askama.toml"; diff --git a/askama_shared/src/path.rs b/askama_shared/src/path.rs index bc329e5..b1a1e6a 100644 --- a/askama_shared/src/path.rs +++ b/askama_shared/src/path.rs @@ -1,10 +1,9 @@ -use std::env; -use std::fs::{self, File}; +use super::Config; + +use std::fs::File; use std::io::Read; use std::path::{Path, PathBuf}; -use toml; - pub fn get_template_source(tpl_path: &Path) -> String { let mut f = match File::open(tpl_path) { Err(_) => panic!("unable to open template file '{}'", tpl_path.to_str().unwrap()), @@ -42,39 +41,6 @@ pub fn template_dirs() -> Vec<PathBuf> { Config::new().dirs } -struct Config { - dirs: Vec<PathBuf>, -} - -impl Config { - fn new() -> Config { - let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); - let filename = root.join(CONFIG_FILE_NAME); - - let default = vec![root.join("templates")]; - let dirs = if filename.exists() { - let config_str = fs::read_to_string(&filename) - .expect(&format!("unable to read {}", filename.to_str().unwrap())); - let raw: RawConfig = toml::from_str(&config_str) - .expect(&format!("invalid TOML in {}", filename.to_str().unwrap())); - raw.dirs - .map(|dirs| dirs.into_iter().map(|dir| root.join(dir)).collect()) - .unwrap_or_else(|| default) - } else { - default - }; - - Config { dirs } - } -} - -#[derive(Deserialize)] -struct RawConfig { - dirs: Option<Vec<String>>, -} - -static CONFIG_FILE_NAME: &str = "askama.toml"; - #[cfg(test)] mod tests { use super::{find_template_from_path, get_template_source}; |