From b589b2fdab70c4e40fd4c38a69d2fe68c50dedf8 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Sat, 20 Feb 2016 11:26:29 +0100 Subject: [PATCH] Move the code that tries to exctract values from the toml table into separate functions to allow for early returns and overal more sane code --- src/config/mod.rs | 67 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 65ff91cd..49828435 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -53,12 +53,15 @@ extern crate toml; use std::path::{Path, PathBuf}; use std::error::Error; +use utils; + pub struct Config { title: String, description: String, authors: Vec, + root: PathBuf, source: PathBuf, outputs: Vec, @@ -100,6 +103,7 @@ impl Config { authors: vec![], + root: PathBuf::new(), source: PathBuf::new(), outputs: vec![], @@ -111,8 +115,16 @@ impl Config { } } - pub fn read_config(&mut self) -> Result<(), Box> { - unimplemented!() + pub fn read_config(&mut self, path: &Path) -> Result<(), Box> { + let config_content = try!(utils::fs::file_to_string(path)); + try!(self.fill_config(&config_content)); + + // When all the rest succeeded, set the root path + self.root = path.parent() + .expect("How can an existing file not have a parent directory?") + .to_owned(); + + Ok(()) } fn fill_config(&mut self, toml: &str) -> Result<(), Box> { @@ -141,19 +153,14 @@ impl Config { }, }; - // Retrieve toml value - if let Some(value) = config.get("title") { - if let Some(title) = value.as_str() { - self.title = String::from(title) - } - } - if let Some(value) = config.get("description") { - if let Some(description) = value.as_str() { - self.description = String::from(description) - } - } + // Retrieve toml values + self.title = title_from_toml(&config) + .unwrap_or(String::from("Book")); + + self.description = description_from_toml(&config) + .unwrap_or(String::new()); Ok(()) } @@ -310,6 +317,27 @@ impl Plugin { } +// Helper functions to extract values from toml +fn title_from_toml(toml: &toml::Table) -> Option { + if let Some(value) = toml.get("title") { + if let Some(title) = value.as_str() { + return Some(String::from(title)) + } + } + + None +} + +fn description_from_toml(toml: &toml::Table) -> Option { + if let Some(value) = toml.get("description") { + if let Some(description) = value.as_str() { + return Some(String::from(description)) + } + } + + None +} + #[cfg(test)] mod tests { @@ -372,4 +400,17 @@ rust-playpen = { enabled = true } assert_eq!(config.title(), "mdBook"); assert_eq!(config.description(), "This is a command line utility to generate books from markdown files"); } + + + #[test] + fn fill_config_empty() { + let mut config = Config::new(); + + let toml = r#""#; + + config.fill_config(toml); + + assert_eq!(config.title(), "Book"); + assert_eq!(config.description(), ""); + } }