mdbook/src/config/tomlconfig.rs

61 lines
1.7 KiB
Rust
Raw Normal View History

2017-05-18 19:32:08 +02:00
extern crate toml;
use std::path::PathBuf;
2017-06-24 23:53:08 +08:00
use errors::*;
2017-05-18 19:32:08 +02:00
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct TomlConfig {
pub source: Option<PathBuf>,
pub title: Option<String>,
pub author: Option<String>,
pub authors: Option<Vec<String>>,
pub description: Option<String>,
pub output: Option<TomlOutputConfig>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct TomlOutputConfig {
pub html: Option<TomlHtmlConfig>,
}
#[serde(rename_all = "kebab-case")]
2017-05-18 19:32:08 +02:00
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct TomlHtmlConfig {
pub destination: Option<PathBuf>,
pub theme: Option<PathBuf>,
pub google_analytics: Option<String>,
pub curly_quotes: Option<bool>,
pub mathjax_support: Option<bool>,
pub additional_css: Option<Vec<PathBuf>>,
2017-06-06 20:34:57 +02:00
pub additional_js: Option<Vec<PathBuf>>,
2017-06-29 00:35:20 -04:00
pub playpen: Option<TomlPlaypenConfig>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct TomlPlaypenConfig {
pub editor: Option<PathBuf>,
pub editable: Option<bool>,
2017-05-18 19:32:08 +02:00
}
2017-06-27 09:08:58 +02:00
/// Returns a `TomlConfig` from a TOML string
2017-05-18 19:32:08 +02:00
///
/// ```
/// # use mdbook::config::tomlconfig::TomlConfig;
/// # use std::path::PathBuf;
/// let toml = r#"title="Some title"
/// [output.html]
/// destination = "htmlbook" "#;
///
2017-05-18 19:32:08 +02:00
/// let config = TomlConfig::from_toml(&toml).expect("Should parse correctly");
/// assert_eq!(config.title, Some(String::from("Some title")));
/// assert_eq!(config.output.unwrap().html.unwrap().destination, Some(PathBuf::from("htmlbook")));
/// ```
impl TomlConfig {
2017-06-24 23:53:08 +08:00
pub fn from_toml(input: &str) -> Result<Self> {
let config: TomlConfig = toml::from_str(input).chain_err(|| "Could not parse TOML")?;
2017-06-27 09:08:58 +02:00
Ok(config)
2017-05-18 19:32:08 +02:00
}
}