From 36e720b26ce9fb5e79a5cb4596a7d66e493f6b10 Mon Sep 17 00:00:00 2001 From: Gustav Eek Date: Mon, 25 Dec 2023 15:52:22 +0100 Subject: [PATCH] Src. Parse the iCalendar (org-mode) priority pattern This implements only the parsing in *lib.winnow* of priority patterns: `(A)`, `(A1)`, `[#A]`, etc. The priority pattern occurse after any bullets, numbers or priorities: ` 8. 45 % [#A] Text`. The operation is the similar to the owne with numbers. The difference is that the iCalendar priority patterns apply only after the other patterns. The *Conf* enum is updated with an *ical* field. Also tests are provided. --- src/lib.rs | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c971262..8cd1b48 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,9 +24,11 @@ fn winnow(mut list: Vec) -> (Conf, Vec ) { 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(); + let ical = Regex::new( + r"^\s*([\[\(#]+)[A-C][0-9]*([\]\)]+)\s*").unwrap(); if DEBUG { eprintln!( // debug patterns - "Matches first item: '{}', '{}', '{}'", + "Matches first item: '{}', '{}', '{}', '{}'", match bullet.captures(&list[0]){ None => "", Some(x) => x.get(0).unwrap().as_str()}, @@ -35,7 +37,14 @@ fn winnow(mut list: Vec) -> (Conf, Vec ) { Some(x) => x.get(0).unwrap().as_str()}, match prio.captures(&list[0]){ None => "", - Some(x) => x.get(0).unwrap().as_str()});} + Some(x) => x.get(0).unwrap().as_str()}, + match ical.captures( + & {let tmp = bullet.replace(&list[0], ""); + let tmp = number.replace(&tmp, ""); + prio.replace(&tmp, "").into_owned()} ){ + 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". @@ -77,6 +86,16 @@ fn winnow(mut list: Vec) -> (Conf, Vec ) { x.get(1).unwrap().as_str(), x.get(3).unwrap().as_str()) }, + ical: match ical.captures( + & {let tmp = bullet.replace(&list[0], ""); + let tmp = number.replace(&tmp, ""); + prio.replace(&tmp, "").into_owned()} ){ + None => "".to_string(), + Some(x) => format!( + "{}{{}}{}", + x.get(1).unwrap().as_str(), + x.get(2).unwrap().as_str()) + }, }; if DEBUG { @@ -89,6 +108,7 @@ fn winnow(mut list: Vec) -> (Conf, Vec ) { *l = bullet.replace_all(&l, "").to_string(); *l = number.replace_all(&l, "").to_string(); *l = prio.replace_all(&l, "").to_string(); + *l = ical.replace_all(&l, "").to_string(); *l = l.trim().to_owned(); } list.retain(|x| x != ""); // again @@ -198,6 +218,7 @@ pub fn lognormal(n: i32, std: f64) -> Vec { pub struct Conf { bullet: String, number: String, + ical: String, } #[derive(Parser, Debug)] @@ -316,11 +337,28 @@ fn pipe() { #[test] +fn icalinput() { + + let arg: Vec = + "(A) Hej du\n(B2) glade (C)\nta en" + .split("\n").map(|x| x.to_owned()).collect(); + let exp: Vec = + "Hej du\nglade (C)\nta en" + .split("\n").map(|x| x.to_owned()).collect(); + let(conf, res) = winnow(arg); + assert_eq!(exp, res); + assert_eq!(conf.ical, "({})"); +} + +#[test] + fn bullout() { let conf = Conf { bullet: " * ".to_owned(), - number: "".to_owned()}; + number: "".to_owned(), + ical: "".to_owned(), + }; let prio = vec![0.6, 0.3, 0.1]; let ranked: Vec = "Hej du\nglade\nta en" -- 2.39.2