Made analyze_string public
All checks were successful
continuous-integration/drone Build is passing

This commit is contained in:
Justine Pelletreau 2023-01-13 13:26:16 +01:00
parent a723a89e9f
commit 5bde8b5535

View File

@ -85,7 +85,7 @@ pub mod flair {
for line in reader { for line in reader {
let mut myline = line.unwrap(); let mut myline = line.unwrap();
myline = analyze_line(&mut myline, &vars); myline = analyze_string(&mut myline, &vars);
_ = &mut analyzed_lines.push(myline); _ = &mut analyzed_lines.push(myline);
} }
return Ok(analyzed_lines.join("\n")); 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 ///Runs a bash command in a thread, returns the result
///(Stdout, Stderr) ///(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"). ///Used to replace individual placeholders ("words").
fn analyze_word(word: &str) -> Placeholder { fn analyze_word(word: &str) -> Placeholder {
@ -162,7 +162,7 @@ pub mod flair {
"This is a test line from %%name%% \n\ "This is a test line from %%name%% \n\
%%cmd: echo Sayonara%%" %%cmd: echo Sayonara%%"
); );
let myresult = analyze_line(&mut myline, &vars); let myresult = analyze_string(&mut myline, &vars);
let expected = String::from( let expected = String::from(
"This is a test line from RustGirl \n\ "This is a test line from RustGirl \n\
Sayonara" Sayonara"
@ -173,6 +173,5 @@ pub mod flair {
assert_eq!(myresult, expected); assert_eq!(myresult, expected);
} }
} }
} }