Added in things from @Phaiax's review

This commit is contained in:
Michael Bryan 2017-10-16 20:47:22 +08:00
parent d37821c194
commit 3aa6436679
No known key found for this signature in database
GPG Key ID: E9C602B0D9A998DC
3 changed files with 23 additions and 63 deletions

View File

@ -32,8 +32,7 @@ impl Config {
/// Load the configuration file from disk. /// Load the configuration file from disk.
pub fn from_disk<P: AsRef<Path>>(config_file: P) -> Result<Config> { pub fn from_disk<P: AsRef<Path>>(config_file: P) -> Result<Config> {
let mut buffer = String::new(); let mut buffer = String::new();
File::open(config_file) File::open(config_file).chain_err(|| "Unable to open the configuration file")?
.chain_err(|| "Unable to open the configuration file")?
.read_to_string(&mut buffer) .read_to_string(&mut buffer)
.chain_err(|| "Couldn't read the file")?; .chain_err(|| "Couldn't read the file")?;
@ -57,25 +56,34 @@ impl Config {
/// Try to get the configuration for a preprocessor, deserializing it as a /// Try to get the configuration for a preprocessor, deserializing it as a
/// `T`. /// `T`.
pub fn try_get_preprocessor<'de, T: Deserialize<'de>, S: AsRef<str>>(&self, name: S) -> Result<T> { pub fn try_get_preprocessor<'de, T: Deserialize<'de>, S: AsRef<str>>(&self,
name: S)
-> Result<T> {
get_deserialized(name, &self.preprocess) get_deserialized(name, &self.preprocess)
} }
/// Try to get the configuration for a postprocessor, deserializing it as a /// Try to get the configuration for a postprocessor, deserializing it as a
/// `T`. /// `T`.
pub fn try_get_postprocessor<'de, T: Deserialize<'de>, S: AsRef<str>>(&self, name: S) -> Result<T> { pub fn try_get_postprocessor<'de, T: Deserialize<'de>, S: AsRef<str>>(&self,
name: S)
-> Result<T> {
get_deserialized(name, &self.postprocess) get_deserialized(name, &self.postprocess)
} }
} }
/// Convenience function to load a value from some table then deserialize it. /// Convenience function to load a value from some table then deserialize it.
fn get_deserialized<'de, T: Deserialize<'de>, S: AsRef<str>>(name: S, table: &BTreeMap<String, Value>) -> Result<T> { fn get_deserialized<'de, T: Deserialize<'de>, S: AsRef<str>>(name: S,
match table.get(name.as_ref()) { table: &BTreeMap<String, Value>)
Some(output) => output -> Result<T> {
.clone() let name = name.as_ref();
match table.get(name) {
Some(output) => {
output.clone()
.try_into() .try_into()
.chain_err(|| "Couldn't deserialize the value"), .chain_err(|| "Couldn't deserialize the value")
None => bail!("Key Not Found"), }
None => bail!("Key Not Found, {}", name),
} }
} }

View File

@ -296,7 +296,7 @@ impl Renderer for HtmlHandlebars {
let rendered = self.post_process(rendered, let rendered = self.post_process(rendered,
"print.html", "print.html",
&book.config.html_config().unwrap_or_default().playpen); &html_config.playpen);
book.write_file(Path::new("print").with_extension("html"), book.write_file(Path::new("print").with_extension("html"),
&rendered.into_bytes())?; &rendered.into_bytes())?;
@ -329,7 +329,7 @@ fn make_data(book: &MDBook, config: &Config) -> Result<serde_json::Map<String, s
} }
// Add google analytics tag // Add google analytics tag
if let Some(ref ga) = book.config.html_config().and_then(|html| html.google_analytics) { if let Some(ref ga) = config.html_config().and_then(|html| html.google_analytics) {
data.insert("google_analytics".to_owned(), json!(ga)); data.insert("google_analytics".to_owned(), json!(ga));
} }

View File

@ -1,48 +0,0 @@
extern crate mdbook;
extern crate tempdir;
use std::fs::File;
use std::io::Write;
use mdbook::MDBook;
use tempdir::TempDir;
// Tests that config values unspecified in the configuration file do not
// overwrite
// values specified earlier.
// #[test]
// fn do_not_overwrite_unspecified_config_values() {
// let dir = TempDir::new("mdbook").expect("Could not create a temp dir");
// let book = MDBook::new(dir.path())
// .with_source("bar")
// .with_destination("baz")
// .with_mathjax_support(true);
// assert_eq!(book.get_root(), dir.path());
// assert_eq!(book.get_source(), dir.path().join("bar"));
// assert_eq!(book.get_destination(), dir.path().join("baz"));
// // Test when trying to read a config file that does not exist
// let book = book.read_config().expect("Error reading the config file");
// assert_eq!(book.get_root(), dir.path());
// assert_eq!(book.get_source(), dir.path().join("bar"));
// assert_eq!(book.get_destination(), dir.path().join("baz"));
// assert_eq!(book.get_mathjax_support(), true);
// // Try with a partial config file
// let file_path = dir.path().join("book.toml");
// let mut f = File::create(file_path).expect("Could not create config
// file");
// f.write_all(br#"source = "barbaz""#).expect("Could not write to config
// file");
// f.sync_all().expect("Could not sync the file");
// let book = book.read_config().expect("Error reading the config file");
// assert_eq!(book.get_root(), dir.path());
// assert_eq!(book.get_source(), dir.path().join("barbaz"));
// assert_eq!(book.get_destination(), dir.path().join("baz"));
// assert_eq!(book.get_mathjax_support(), true);
// }