134 lines
3.9 KiB
Rust
134 lines
3.9 KiB
Rust
use toml;
|
|
|
|
use std::path::PathBuf;
|
|
use std::fs::File;
|
|
use std::io::Read;
|
|
use std::error::Error as STDError;
|
|
use std::collections::HashMap;
|
|
|
|
use logger::LogLevel;
|
|
use error::Error;
|
|
|
|
#[derive(Deserialize, Clone, Debug)]
|
|
pub struct GlobalConfigToml {
|
|
pub website: WebsiteConfig,
|
|
pub navbar: Option<NavbarConfig>,
|
|
}
|
|
|
|
#[derive(Deserialize, Clone, Debug)]
|
|
pub struct WebsiteConfig {
|
|
pub website_name: String,
|
|
pub built_pages: Vec<String>,
|
|
pub use_default_css: bool,
|
|
pub javascript: Vec<String>,
|
|
pub css: Vec<String>,
|
|
pub favicon: Option<String>,
|
|
pub before_navbar_url: Option<String>,
|
|
pub before_content_url: Option<String>,
|
|
pub after_content_url: Option<String>,
|
|
pub twitter_author: Option<String>,
|
|
pub google_robots: Option<String>,
|
|
pub google_site_verification: Option<String>,
|
|
}
|
|
|
|
#[derive(Deserialize, Clone, Debug)]
|
|
pub struct NavbarConfig {
|
|
pub items: Vec<String>,
|
|
#[serde(rename = "item")]
|
|
pub item_map: HashMap<String, NavbarItem>,
|
|
}
|
|
|
|
#[derive(Deserialize, Clone, Debug)]
|
|
pub struct NavbarItem {
|
|
pub title: String,
|
|
pub link: String,
|
|
pub image_url: Option<String>,
|
|
}
|
|
|
|
impl GlobalConfigToml {
|
|
pub fn get_config() -> Result<GlobalConfigToml, Error> {
|
|
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(
|
|
LogLevel::SEVERE,
|
|
format!("Erronous config.toml at {}:{}", line, col),
|
|
))
|
|
} else {
|
|
Err(Error::new(
|
|
LogLevel::SEVERE,
|
|
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<PageConfigToml, Error> {
|
|
let path_str = match path.to_str() {
|
|
Some(path_str) => path_str,
|
|
None => "",
|
|
};
|
|
|
|
let mut file = match File::open(path.as_path()) {
|
|
Ok(file) => file,
|
|
Err(err) => {
|
|
return Err(Error::new(
|
|
LogLevel::SEVERE,
|
|
format!(
|
|
"Failed to open page config: {}, Reason: {}",
|
|
path.to_str().unwrap(),
|
|
err.description().to_owned()
|
|
),
|
|
))
|
|
}
|
|
};
|
|
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(
|
|
LogLevel::SEVERE,
|
|
format!("Erronous toml: {} at {}:{}", path_str, line, col),
|
|
))
|
|
} else {
|
|
Err(Error::new(
|
|
LogLevel::SEVERE,
|
|
format!(
|
|
"Failed to parse toml correctly: {}. Check variable names!",
|
|
path_str
|
|
),
|
|
))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Deserialize, Clone, Debug)]
|
|
pub struct PageConfig {
|
|
pub html_path: String,
|
|
pub title: String,
|
|
pub description: String,
|
|
pub content_path: String,
|
|
pub favicon: Option<String>,
|
|
pub before_navbar_url: Option<String>,
|
|
pub before_content_url: Option<String>,
|
|
pub after_content_url: Option<String>,
|
|
pub javascript: Option<Vec<String>>,
|
|
pub css: Option<Vec<String>>,
|
|
}
|