-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perform interpolation on template parameters.
- Loading branch information
1 parent
9df3887
commit 1c9a8a0
Showing
7 changed files
with
169 additions
and
59 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,6 @@ version = "0.1.0" | |
|
||
[dependencies] | ||
clap = "2.1.1" | ||
lazy_static = "0.1.15" | ||
regex = "0.1.54" | ||
yaml-rust = "0.3.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,79 @@ | ||
use yaml::Yaml; | ||
use yaml::yaml::{Array, Hash}; | ||
use regex::{Captures, Regex}; | ||
|
||
use parameter::ParamMap; | ||
|
||
pub type ProcessorResult = Result<(), String>; | ||
pub type ProcessorResult = Result<Option<Yaml>, String>; | ||
|
||
pub fn process_yaml(yaml: &mut Yaml, parameters: &ParamMap) -> ProcessorResult { | ||
match yaml { | ||
&mut Yaml::Array(ref mut array) => try!(process_array(array, parameters)), | ||
&mut Yaml::Hash(ref mut hash) => try!(process_hash(hash, parameters)), | ||
&mut Yaml::String(ref mut string) => try!(process_string(string, parameters)), | ||
_ => {}, | ||
&mut Yaml::Array(ref mut array) => process_array(array, parameters), | ||
&mut Yaml::Hash(ref mut hash) => process_hash(hash, parameters), | ||
&mut Yaml::String(ref mut string) => process_string(string, parameters), | ||
_ => Ok(None), | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn process_array(array: &mut Array, parameters: &ParamMap) -> ProcessorResult { | ||
for value in array { | ||
try!(process_yaml(value, parameters)); | ||
match process_yaml(value, parameters) { | ||
Ok(Some(new_value)) => *value = new_value, | ||
Err(error) => return Err(error), | ||
_ => {}, | ||
} | ||
} | ||
|
||
Ok(()) | ||
Ok(None) | ||
} | ||
|
||
fn process_hash(hash: &mut Hash, parameters: &ParamMap) -> ProcessorResult { | ||
for (_, value) in hash { | ||
try!(process_yaml(value, parameters)); | ||
match process_yaml(value, parameters) { | ||
Ok(Some(new_value)) => *value = new_value, | ||
Err(error) => return Err(error), | ||
_ => {}, | ||
} | ||
} | ||
|
||
Ok(()) | ||
Ok(None) | ||
} | ||
|
||
fn process_string(string: &mut String, parameters: &ParamMap) -> ProcessorResult { | ||
// TODO: Interpolate any parameters. | ||
lazy_static! { | ||
static ref LITERAL_INTERPOLATION: Regex = Regex::new( | ||
r"\$\({2}(.*)\){2}" | ||
).expect("Failed to compile regex."); | ||
} | ||
|
||
lazy_static! { | ||
static ref STRING_INTERPOLATION: Regex = Regex::new( | ||
r"\$\((.*)\)" | ||
).expect("Failed to compile regex."); | ||
} | ||
|
||
let interpolate = |captures: &Captures| -> String { | ||
let key = captures.at(1).expect("Failed to extract regex capture group."); | ||
|
||
match parameters.get(key) { | ||
Some(parameter) => parameter.value.clone().unwrap_or("~".to_owned()), | ||
None => captures.at(0).expect("Failed to extract regex match.").to_owned(), | ||
} | ||
}; | ||
|
||
Ok(()) | ||
let replacement = LITERAL_INTERPOLATION.replace_all(string, &interpolate); | ||
|
||
let contains_literal_replacement = &replacement != string; | ||
|
||
let final_replacement = STRING_INTERPOLATION.replace_all(&replacement, &interpolate); | ||
|
||
let contains_string_replacement = &final_replacement != &replacement; | ||
|
||
if !contains_literal_replacement && !contains_string_replacement { | ||
Ok(None) | ||
} else if contains_literal_replacement && !contains_string_replacement { | ||
Ok(Some(Yaml::from_str(&final_replacement))) | ||
} else { | ||
Ok(Some(Yaml::String(final_replacement))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters