From: Gustav Eek Date: Sun, 26 Feb 2023 07:41:18 +0000 (+0100) Subject: Src. Refactor. Prepare for storing output modes. X-Git-Tag: v.0.4~3 X-Git-Url: https://git.g-eek.se/?a=commitdiff_plain;h=f0c1dee5ba3dbf1a599c232f85195f19bd773d63;p=ranknauto.git Src. Refactor. Prepare for storing output modes. A configuration struct is added for storing various output modes. Function *winnow* returns this *config*. The *winnow* function is changed to be called by value. --- diff --git a/src/main.rs b/src/main.rs index 255524b..824b7bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,29 +10,35 @@ use std::io::prelude::*; // Bring `std::io::BufRead` in scope use statrs::distribution::{ Exp, LogNormal, ContinuousCDF}; // Continuous needed for pdf -// CLI Parser, replaces need for use `std::env` -use clap::Parser; +use clap::Parser; // CLI Parser, replaces need for use `std::env` +use regex::Regex; const DEBUG: bool = false; -fn winnow(list: &mut Vec) { + +fn winnow(mut list: Vec) -> (Conf, Vec ) { + list.retain(|x| x != ""); // only keep nonempty + + let conf = Conf { + }; + + // Remove patterns and trim for l in &mut *list { *l = l.trim().to_owned(); } - list.retain(|x| x != ""); // only keep nonempty - // Search bullets + list.retain(|x| x != ""); // again + return (conf, list) } -fn input() -> Vec { +fn input() -> (Conf, Vec) { let stdin = io::stdin(); - let mut list: Vec = + let list: Vec = stdin.lock().lines() .map(|x| x.unwrap()) .collect(); - winnow(&mut list); - list + return winnow(list) } -fn output(prio: &Vec, ranked: &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); } @@ -106,6 +112,9 @@ fn lognormal(n: i32, std: f64) -> Vec { normalize(prio) } +struct Conf { +} + #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] @@ -143,7 +152,7 @@ fn main() { if DEBUG { eprintln!("Finally a skewness: {}", skew) } - let ranked: Vec = input(); + let (conf, ranked) = input(); let num = ranked.len() as i32; let prio = if args.distribution == String::from("delta") { @@ -161,7 +170,7 @@ fn main() { eprintln!("\x08\x08."); } - output(&prio, &ranked); + output(&conf, &prio, &ranked); } @@ -170,29 +179,29 @@ fn main() { fn ordinary_input() { let mut arg: Vec = Vec::new(); - let mut res: Vec = Vec::new(); + let mut exp: Vec = Vec::new(); for s in vec!["", "", "Hej ☕ glade", "", "Amatör", "", ""] { arg.push(String::from(s)); } for s in vec!["Hej ☕ glade", "Amatör"] { - res.push(String::from(s)); + exp.push(String::from(s)); } - winnow(&mut arg); - assert_eq!(arg, res); + let (_, res) = winnow(arg); + assert_eq!(exp, res); } #[test] fn whitespace() { - let mut arg: Vec = + let arg: Vec = "\n\nHej du\t\n glade\nta en\n\n" // Convert &str to String .split("\n").map(|x| x.to_owned()) // with `.to_owned()` or .collect(); // `.to_string()` - let res: Vec = + let exp: Vec = "Hej du\nglade\nta en" .split("\n").map(|x| x.to_owned()).collect(); - winnow(&mut arg); - assert_eq!(arg, res); + let (_, res) = winnow(arg); + assert_eq!(exp, res); }