From 8356f6f1e0197ce702fe19829288d416a25565bf Mon Sep 17 00:00:00 2001 From: Gustav Eek Date: Sun, 26 Feb 2023 08:46:28 +0100 Subject: [PATCH] Src. Handle bullet lists Support for bullet list input is implemented with regular expressions in *winnow*. The configuration struct stores bullet format if found. Function *output* implements the printing. Testing is completed for all above. --- src/main.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 824b7bb..20eaf7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,11 +18,19 @@ const DEBUG: bool = false; fn winnow(mut list: Vec) -> (Conf, Vec ) { list.retain(|x| x != ""); // only keep nonempty + // Search bullets + let bullet = Regex::new(r"^(\s*[\*-]\s*)").unwrap(); + let conf = Conf { + bullet: match bullet.captures(&list[0]) { + None => "".to_string(), + Some(x) => x.get(1).unwrap().as_str().to_string(), + }, }; // Remove patterns and trim for l in &mut *list { + *l = bullet.replace_all(&l, "").to_string(); *l = l.trim().to_owned(); } list.retain(|x| x != ""); // again @@ -40,7 +48,11 @@ fn input() -> (Conf, Vec) { fn output(conf: &Conf, prio: &Vec, ranked: &Vec) { for (p, l) in prio.iter().zip(ranked.iter()) { - println!("{:2.0} %\t{}", p * 100.0, l); + println!( + "{}{:2.0} %\t{}", + &conf.bullet, + p * 100.0, + l); } } @@ -113,6 +125,7 @@ fn lognormal(n: i32, std: f64) -> Vec { } struct Conf { + bullet: String, } #[derive(Parser, Debug)] @@ -205,3 +218,31 @@ fn whitespace() { assert_eq!(exp, res); } + +#[test] + +fn bullets() { + + let arg: Vec = + "\n * Hej du\t\n - glade\n-\tta en\n\n" // Convert &str to String + .split("\n").map(|x| x.to_owned()) // with `.to_owned()` or + .collect(); // `.to_string()` + let exp: Vec = + "Hej du\nglade\nta en" + .split("\n").map(|x| x.to_owned()).collect(); + let (conf, res ) = winnow(arg); + assert_eq!(exp, res); + assert_eq!(conf.bullet, " * ".to_owned()); + +} + +#[test] + +fn bullout() { + let conf = Conf {bullet: " * ".to_owned()}; + let prio = vec![0.6, 0.3, 0.1]; + let ranked: Vec = + "Hej du\nglade\nta en" + .split("\n").map(|x| x.to_owned()).collect(); + output(&conf, &prio, &ranked); +} -- 2.39.2