list.retain(|x| x != ""); // only keep nonempty
// Search patterns
+
let bullet = Regex::new(r"^(\s*[\*-]\s*)").unwrap();
let number = Regex::new(r"^(\s*)([0-9]+)([).]\s*)").unwrap();
let prio = Regex::new(r"^\s*[0-9]+\s*%\s*").unwrap();
- // Use index of the longest second group match to create conf template
- if DEBUG {
- eprintln!(
- "Match: '{}', '{}', '{}', '{}'",
- match number.captures(&list[0]){None => "", Some(x) => x.get(0).unwrap().as_str()},
- match number.captures(&list[0]){None => "", Some(x) => x.get(1).unwrap().as_str()},
- match number.captures(&list[0]){None => "", Some(x) => x.get(2).unwrap().as_str()},
- match number.captures(&list[0]){None => "", Some(x) => x.get(3).unwrap().as_str()},
- );
- }
+ if DEBUG { eprintln!( // debug patterns
+ "Matches first item: '{}', '{}', '{}'",
+ match bullet.captures(&list[0]){
+ None => "",
+ Some(x) => x.get(0).unwrap().as_str()},
+ match number.captures(&list[0]){
+ None => "",
+ Some(x) => x.get(0).unwrap().as_str()},
+ match prio.captures(&list[0]){
+ None => "",
+ Some(x) => x.get(0).unwrap().as_str()});}
+
+ // Use index of the longest relevant group match to create conf
+ // template. Call this "the rule of index of the longest match".
+
+ let maxi_bullet = list.iter().map(
+ |y| match bullet.captures(y) {
+ None => 0,
+ Some(x) => x.get(1).unwrap().as_str().len(),
+ }).enumerate().fold(
+ (0, 0), |max, (ind, val)| if val > max.1 {(ind, val)} else {max}).0;
- let lens: Vec<usize> = list.iter().map(
+ let maxi_number = list.iter().map(
|y| match number.captures(y) {
None => 0,
Some(x) => x.get(2).unwrap().as_str().len(),
- }).collect();
-
- let maxi = lens.iter().enumerate().fold(
- (0, 0),
- |max, (ind, &val)| if val > max.1 {(ind, val)} else {max});
-
- let tmpl = match number.captures(&list[maxi.0]) {
- None => "".to_string(),
- Some(x) => {
- if DEBUG {
- eprintln!("Test matches '{}', '{}', '{}'",
- x.get(1).unwrap().as_str(),
- x.get(2).unwrap().as_str(),
- x.get(3).unwrap().as_str());
- }
- format!(
- "{}{{}}{}",
- x.get(1).unwrap().as_str(),
- x.get(3).unwrap().as_str())
- },
- };
-
- if DEBUG {
- eprint!("Lens index {}, {}: ", maxi.0, maxi.1);
- for n in lens {eprint!("{}, ", n);}
- eprintln!("\x08\x08.");
- eprintln!("Pattern: '{}'", tmpl);
- }
+ }).enumerate().fold(
+ (0, 0), |max, (ind, val)| if val > max.1 {(ind, val)} else {max}).0;
+
+ if DEBUG { eprintln!( // debug longest match
+ "Longest match: {}, '{}'; {}, '{}'",
+ maxi_bullet,
+ match bullet.captures(&list[maxi_bullet]) {
+ None => "",
+ Some(x) => x.get(1).unwrap().as_str()},
+ maxi_number,
+ match number.captures(&list[maxi_number]) {
+ None => "",
+ Some(x) => x.get(2).unwrap().as_str()})};
let conf = Conf {
- bullet: match bullet.captures(&list[0]) {
+ bullet: match bullet.captures(&list[maxi_bullet]) {
None => "".to_string(),
Some(x) => x.get(1).unwrap().as_str().to_string(),
},
- number: tmpl,
+ number: match number.captures(&list[maxi_number]) {
+ None => "".to_string(),
+ Some(x) => format!(
+ "{}{{}}{}",
+ x.get(1).unwrap().as_str(),
+ x.get(3).unwrap().as_str())
+ },
};
if DEBUG {
}
// Remove patterns and trim
+
for l in &mut *list {
*l = bullet.replace_all(&l, "").to_string();
*l = number.replace_all(&l, "").to_string();
.split("\n").map(|x| x.to_owned()).collect();
let (conf, res) = winnow(arg);
assert_eq!(exp, res);
- assert_eq!(conf.bullet, " * ".to_owned());
+ assert_eq!(conf.bullet, " - ".to_owned());
assert_eq!(conf.number, "".to_owned());
}
fn numbers() {
let arg: Vec<String> =
- "1. Hej du\n 459. glade\n 2)ta en\n"
+ "1) Hej du\n 459. glade\n 2)ta en\n"
.split("\n").map(|x| x.to_owned())
.collect();
let exp: Vec<String> =