From 5bde8b5535932c73aec55e946f89425dd8433cff Mon Sep 17 00:00:00 2001 From: Justine Pelletreau Date: Fri, 13 Jan 2023 13:26:16 +0100 Subject: [PATCH] Made analyze_string public --- src/lib.rs | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cbf466b..c2bd861 100644 --- a/src/lib.rs +++ b/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 { + 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 { - 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); } - } }