This commit is contained in:
parent
a723a89e9f
commit
5bde8b5535
33
src/lib.rs
33
src/lib.rs
@ -85,7 +85,7 @@ pub mod flair {
|
||||
|
||||
for line in reader {
|
||||
let mut myline = line.unwrap();
|
||||
myline = analyze_line(&mut myline, &vars);
|
||||
myline = analyze_string(&mut myline, &vars);
|
||||
_ = &mut analyzed_lines.push(myline);
|
||||
}
|
||||
return Ok(analyzed_lines.join("\n"));
|
||||
@ -94,6 +94,20 @@ pub mod flair {
|
||||
};
|
||||
}
|
||||
|
||||
///Analyzes a String, and replaces placeholders
|
||||
///with their actual value.
|
||||
pub fn analyze_string<'a>(line: &'a mut String, vars: &HashMap<String, String>) -> String {
|
||||
let re = Regex::new(r"%%[^%]*%%").unwrap();
|
||||
let mut temp_line = String::from(line.as_str());
|
||||
for occurence in re.captures_iter(line) {
|
||||
//Never goes further than zero
|
||||
//println!("Found : {:?}", &occurence[0]);
|
||||
let my_ph = analyze_word(&occurence[0]);
|
||||
temp_line = temp_line.replace(&occurence[0], &my_ph.replace_to_val(&vars));
|
||||
}
|
||||
return temp_line;
|
||||
|
||||
}
|
||||
|
||||
///Runs a bash command in a thread, returns the result
|
||||
///(Stdout, Stderr)
|
||||
@ -113,20 +127,6 @@ pub mod flair {
|
||||
}
|
||||
|
||||
|
||||
///Analyzes a line, and replaces placeholders
|
||||
///with their actual value.
|
||||
fn analyze_line<'a>(line: &'a mut String, vars: &HashMap<String, String>) -> String {
|
||||
let re = Regex::new(r"%%[^%]*%%").unwrap();
|
||||
let mut temp_line = String::from(line.as_str());
|
||||
for occurence in re.captures_iter(line) {
|
||||
//Never goes further than zero
|
||||
//println!("Found : {:?}", &occurence[0]);
|
||||
let my_ph = analyze_word(&occurence[0]);
|
||||
temp_line = temp_line.replace(&occurence[0], &my_ph.replace_to_val(&vars));
|
||||
}
|
||||
return temp_line;
|
||||
|
||||
}
|
||||
|
||||
///Used to replace individual placeholders ("words").
|
||||
fn analyze_word(word: &str) -> Placeholder {
|
||||
@ -162,7 +162,7 @@ pub mod flair {
|
||||
"This is a test line from %%name%% \n\
|
||||
%%cmd: echo Sayonara%%"
|
||||
);
|
||||
let myresult = analyze_line(&mut myline, &vars);
|
||||
let myresult = analyze_string(&mut myline, &vars);
|
||||
let expected = String::from(
|
||||
"This is a test line from RustGirl \n\
|
||||
Sayonara"
|
||||
@ -173,6 +173,5 @@ pub mod flair {
|
||||
assert_eq!(myresult, expected);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user