From 7168ab8fd10823e59811e9b56e2bce6d7556224a Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Thu, 23 Mar 2017 19:52:16 -0700 Subject: [PATCH] Add functions for loading parameter values from a string or YAML document. --- src/lib.rs | 8 +++++++- src/parameter.rs | 50 +++++++++++++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4f3df66..590f13b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -79,7 +79,13 @@ extern crate regex; extern crate yaml_rust as yaml; pub use template::Template; -pub use parameter::{ParameterValue, ParameterValues, parameter_values_from_file}; +pub use parameter::{ + ParameterValue, + ParameterValues, + parameter_values_from_file, + parameter_values_from_str, + parameter_values_from_yaml, +}; pub use secret::{Secret, Secrets}; mod parameter; diff --git a/src/parameter.rs b/src/parameter.rs index eeda24a..6e04f0f 100644 --- a/src/parameter.rs +++ b/src/parameter.rs @@ -39,42 +39,56 @@ pub type ParamMap = HashMap; /// A map of parameter names to user-supplied values of the parameters. pub type ParameterValues = HashMap; -/// Creates a `ParameterFile` from a file. +/// Loads `ParameterValues` from a file. pub fn parameter_values_from_file(file_path: &str) -> Result { let mut file = File::open(file_path).map_err(|err| err.description().to_owned())?; let mut contents = String::new(); file.read_to_string(&mut contents).map_err(|err| err.description().to_owned())?; + parameter_values_from_str(&contents) +} + +/// Loads `ParameterValues` from the raw contents of a parameter file. +pub fn parameter_values_from_str(contents: &str) -> Result { let docs = YamlLoader::load_from_str(&contents) .map_err(|err| err.description().to_owned())?; let mut parameter_values = ParameterValues::new(); for doc in docs { - match doc { - Yaml::Hash(ref hash) => { - for (key, value) in hash { - match *key { - Yaml::String(ref key_string) => { - match *value { - Yaml::String(ref value_string) => { - parameter_values.insert( - key_string.to_string(), - ParameterValue::Plain(value_string.to_string()), - ); - } - _ => return Err("Parameter values in paramter files must be strings.".to_string()), + parameter_values.extend(parameter_values_from_yaml(doc)?); + } + + Ok(parameter_values) +} + +/// Loads `ParameterValues` from a YAML document in the format of a parameter file. +pub fn parameter_values_from_yaml(yaml: Yaml) -> Result { + let mut parameter_values = ParameterValues::new(); + + match yaml { + Yaml::Hash(ref hash) => { + for (key, value) in hash { + match *key { + Yaml::String(ref key_string) => { + match *value { + Yaml::String(ref value_string) => { + parameter_values.insert( + key_string.to_string(), + ParameterValue::Plain(value_string.to_string()), + ); } + _ => return Err("Parameter values in parameter files must be strings.".to_string()), } - _ => return Err( - "Parameters in parameter files must be strings.".to_string() - ), } + _ => return Err( + "Parameters names in parameter files must be strings.".to_string() + ), } } - _ => return Err("YAML documents in parameter files must be hashes.".to_string()), } + _ => return Err("YAML documents in parameter files must be hashes.".to_string()), } Ok(parameter_values)