From e010018c578527efaeb23069b58915f14d86b4f0 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Wed, 22 Nov 2017 16:21:20 +0100 Subject: [PATCH] Make all config options optional and add default config Signed-off-by: Christoph Wurst --- .vscode/settings.json | 5 +++ src/config/app.rs | 76 +++++++++++++++++++++++++++++++++++---- src/packaging/commands.rs | 4 +-- 3 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..68059f36 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "cSpell.words": [ + "krankerl" + ] +} \ No newline at end of file diff --git a/src/config/app.rs b/src/config/app.rs index 00a95f36..3948598d 100644 --- a/src/config/app.rs +++ b/src/config/app.rs @@ -1,3 +1,5 @@ +use std::convert::Into; +use std::default::Default; use std::error::Error; use std::fs::File; use std::io::prelude::*; @@ -8,6 +10,17 @@ use toml; use super::super::error; #[derive(Debug, Deserialize)] +struct ParsedAppConfig { + package: Option, +} + +#[derive(Debug, Deserialize)] +struct ParsedPackageConfig { + before_cmds: Option>, + exclude: Option>, +} + +#[derive(Debug)] pub struct AppConfig { package: PackageConfig, } @@ -18,14 +31,32 @@ impl AppConfig { } } -#[derive(Debug, Deserialize)] +impl Default for AppConfig { + fn default() -> Self { + AppConfig { + package: PackageConfig::default(), + } + } +} + +impl Into for ParsedAppConfig { + fn into(self) -> AppConfig { + AppConfig { + package: self.package + .map(|pc| pc.into()) + .unwrap_or(PackageConfig::default()), + } + } +} + +#[derive(Debug)] pub struct PackageConfig { - before_cmds: Option>, + before_cmds: Vec, exclude: Vec, } impl PackageConfig { - pub fn before_cmds(&self) -> &Option> { + pub fn before_cmds(&self) -> &Vec { &self.before_cmds } pub fn exclude(&self) -> &Vec { @@ -33,6 +64,24 @@ impl PackageConfig { } } +impl Into for ParsedPackageConfig { + fn into(self) -> PackageConfig { + PackageConfig { + before_cmds: self.before_cmds.unwrap_or(vec![]), + exclude: self.exclude.unwrap_or(vec![]), + } + } +} + +impl Default for PackageConfig { + fn default() -> Self { + PackageConfig { + before_cmds: vec![], + exclude: vec![], + } + } +} + pub fn init_config(app_path: &Path) -> Result<(), error::Error> { let mut path_buf = app_path.to_path_buf(); path_buf.push("krankerl.toml"); @@ -67,7 +116,7 @@ fn load_config(path: &Path) -> Result { Ok(contents) } -fn parse_config(config: String) -> Result { +fn parse_config(config: String) -> Result { toml::from_str(&config).map_err(|e| { error::Error::Other(format!( "could not parse krankerl.toml: {}", @@ -78,7 +127,7 @@ fn parse_config(config: String) -> Result { pub fn get_config(path: &Path) -> Result { let config_str = load_config(path)?; - parse_config(config_str) + parse_config(config_str).map(|config| config.into()) } #[cfg(test)] @@ -86,7 +135,16 @@ mod tests { use super::*; #[test] - fn test_parse_minimal_config() { + fn test_parse_empty_config() { + let toml = r#""#; + + let config = parse_config(toml.to_owned()); + + assert!(config.is_ok()); + } + + #[test] + fn test_parse_simple_config() { let toml = r#" [package] exclude = [] @@ -129,6 +187,10 @@ mod tests { assert!(config.is_ok()); let config = config.unwrap(); - assert!(config.package.before_cmds.is_some()); + assert!(config.package.is_some()); + let package_config = config.package.unwrap(); + assert!(package_config.before_cmds.is_some()); + let cmds = package_config.before_cmds.unwrap(); + assert_eq!(3, cmds.len()); } } diff --git a/src/packaging/commands.rs b/src/packaging/commands.rs index 3e7ac9e9..f560e742 100644 --- a/src/packaging/commands.rs +++ b/src/packaging/commands.rs @@ -48,7 +48,7 @@ impl PackageCommands for CommandList { impl<'a> Into for &'a PackageConfig { fn into(self) -> CommandList { CommandList { - cmds: self.before_cmds().clone().unwrap_or(vec![]).to_owned(), + cmds: self.before_cmds().clone(), } } } @@ -56,7 +56,7 @@ impl<'a> Into for &'a PackageConfig { impl Into for PackageConfig { fn into(self) -> CommandList { CommandList { - cmds: self.before_cmds().clone().unwrap_or(vec![]).to_owned(), + cmds: self.before_cmds().clone(), } } }