Skip to content

Commit

Permalink
Add functions for loading parameter values from a string or YAML docu…
Browse files Browse the repository at this point in the history
…ment.
  • Loading branch information
jimmycuadra committed Mar 24, 2017
1 parent a4dbf7d commit 7168ab8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
50 changes: 32 additions & 18 deletions src/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,42 +39,56 @@ pub type ParamMap = HashMap<String, Parameter>;
/// A map of parameter names to user-supplied values of the parameters.
pub type ParameterValues = HashMap<String, ParameterValue>;

/// Creates a `ParameterFile` from a file.
/// Loads `ParameterValues` from a file.
pub fn parameter_values_from_file(file_path: &str) -> Result<ParameterValues, String> {
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<ParameterValues, String> {
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<ParameterValues, String> {
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)
Expand Down

0 comments on commit 7168ab8

Please sign in to comment.