From 3aa64366796bcd0b289bf9df52b0c677c7092226 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Mon, 16 Oct 2017 20:47:22 +0800 Subject: [PATCH] Added in things from @Phaiax's review --- src/config.rs | 34 ++++++++------ src/renderer/html_handlebars/hbs_renderer.rs | 4 +- tests/config.rs | 48 -------------------- 3 files changed, 23 insertions(+), 63 deletions(-) delete mode 100644 tests/config.rs diff --git a/src/config.rs b/src/config.rs index 802c60cd..848e8873 100644 --- a/src/config.rs +++ b/src/config.rs @@ -32,10 +32,9 @@ impl Config { /// Load the configuration file from disk. pub fn from_disk>(config_file: P) -> Result { let mut buffer = String::new(); - File::open(config_file) - .chain_err(|| "Unable to open the configuration file")? - .read_to_string(&mut buffer) - .chain_err(|| "Couldn't read the file")?; + File::open(config_file).chain_err(|| "Unable to open the configuration file")? + .read_to_string(&mut buffer) + .chain_err(|| "Couldn't read the file")?; Config::from_str(&buffer) } @@ -57,25 +56,34 @@ impl Config { /// Try to get the configuration for a preprocessor, deserializing it as a /// `T`. - pub fn try_get_preprocessor<'de, T: Deserialize<'de>, S: AsRef>(&self, name: S) -> Result { + pub fn try_get_preprocessor<'de, T: Deserialize<'de>, S: AsRef>(&self, + name: S) + -> Result { get_deserialized(name, &self.preprocess) } /// Try to get the configuration for a postprocessor, deserializing it as a /// `T`. - pub fn try_get_postprocessor<'de, T: Deserialize<'de>, S: AsRef>(&self, name: S) -> Result { + pub fn try_get_postprocessor<'de, T: Deserialize<'de>, S: AsRef>(&self, + name: S) + -> Result { get_deserialized(name, &self.postprocess) } } /// Convenience function to load a value from some table then deserialize it. -fn get_deserialized<'de, T: Deserialize<'de>, S: AsRef>(name: S, table: &BTreeMap) -> Result { - match table.get(name.as_ref()) { - Some(output) => output - .clone() - .try_into() - .chain_err(|| "Couldn't deserialize the value"), - None => bail!("Key Not Found"), +fn get_deserialized<'de, T: Deserialize<'de>, S: AsRef>(name: S, + table: &BTreeMap) + -> Result { + let name = name.as_ref(); + + match table.get(name) { + Some(output) => { + output.clone() + .try_into() + .chain_err(|| "Couldn't deserialize the value") + } + None => bail!("Key Not Found, {}", name), } } diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 344942d5..f8186948 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -296,7 +296,7 @@ impl Renderer for HtmlHandlebars { let rendered = self.post_process(rendered, "print.html", - &book.config.html_config().unwrap_or_default().playpen); + &html_config.playpen); book.write_file(Path::new("print").with_extension("html"), &rendered.into_bytes())?; @@ -329,7 +329,7 @@ fn make_data(book: &MDBook, config: &Config) -> Result