use toml; use std::path::PathBuf; use std::fs::File; use std::io::{Error, ErrorKind, Read}; #[derive(Deserialize, Clone, Debug)] pub struct GlobalConfigToml { pub website: WebsiteConfig, } #[derive(Deserialize, Clone, Debug)] pub struct WebsiteConfig { pub website_name: String, pub built_pages: Vec, pub use_default_css: bool, pub javascript: Vec, pub css: Vec, pub favicon: Option, pub twitter_author: Option, pub google_robots: Option, pub google_site_verification: Option, } impl GlobalConfigToml { pub fn get_config() -> Result { let mut file = File::open("config.toml")?; let mut contents = String::new(); file.read_to_string(&mut contents)?; match toml::from_str(&contents) { Ok(config) => Ok(config), Err(err) => { if let Some((line, col)) = err.line_col() { Err(Error::new( ErrorKind::Other, format!("Erronous config.toml at {}:{}", line, col), )) } else { Err(Error::new( ErrorKind::Other, format!("Failed to parse config.toml correctly. Check variable names!"), )) } } } } } #[derive(Deserialize, Clone, Debug)] pub struct PageConfigToml { pub page: PageConfig, } impl PageConfigToml { pub fn get_from(path: &PathBuf) -> Result { let path_str = match path.to_str() { Some(path_str) => path_str, None => "", }; let mut file = File::open(path.as_path())?; let mut contents = String::new(); file.read_to_string(&mut contents)?; match toml::from_str(&contents) { Ok(config) => Ok(config), Err(err) => { if let Some((line, col)) = err.line_col() { Err(Error::new( ErrorKind::Other, format!("Erronous toml: {} at {}:{}", path_str, line, col), )) } else { Err(Error::new( ErrorKind::Other, format!( "Failed to parse toml correctly: {}. Check variable names!", path_str ), )) } } } } } #[derive(Deserialize, Clone, Debug)] pub struct PageConfig { pub title: String, pub description: String, pub content_path: String, pub favicon: Option, pub javascript: Option>, pub css: Option>, }