From 63990998cb637bea275a3d6379f151fb01a72ae7 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 31 Oct 2024 17:34:39 +0100 Subject: [PATCH] Separate votes into module, updates todo and silences some warnings --- TODO.md | 6 +- src/date/date_parse.rs | 130 +++++++++++++++++++++++++---------------- src/date/date_tools.rs | 8 +-- src/votes.rs | 2 +- 4 files changed, 86 insertions(+), 60 deletions(-) diff --git a/TODO.md b/TODO.md index 5fbbd60..00c5d5f 100644 --- a/TODO.md +++ b/TODO.md @@ -1,13 +1,17 @@ +# Important +[ ] Get rid of the #deprecated +[ ] Get rid of the #dead_code + # Test Driven [ ] Config Load and persistance [ ] Config recovery [ ] Error testing framework - # Tasks ## Dan [ ] Finish testing to reach 100% coverage [ ] Implement Resilience & Recovery +[*] Parse date mini language ## Vky [ ] Write documentation for all functions in IO.rs diff --git a/src/date/date_parse.rs b/src/date/date_parse.rs index 686a619..bb3d114 100644 --- a/src/date/date_parse.rs +++ b/src/date/date_parse.rs @@ -15,7 +15,7 @@ use nom::{ character::complete::{char, digit1, space0}, combinator::{map, value}, multi::separated_list1, - sequence::{delimited, preceded, separated_pair, tuple}, + sequence::{delimited, separated_pair, tuple}, IResult, ParseTo, }; @@ -73,46 +73,42 @@ pub type DateList = Vec; /// /// Ignores all trailing and leading spaces fn parse_month(input: &str) -> IResult<&str, Month> { - delimited( - space0, + alt(( alt(( - alt(( - value(Month::January, tag_no_case("jan")), - value(Month::February, tag_no_case("fev")), - value(Month::February, tag_no_case("feb")), - value(Month::March, tag_no_case("mar")), - value(Month::March, tag_no_case("avr")), - value(Month::April, tag_no_case("apr")), - value(Month::May, tag_no_case("mai")), - value(Month::May, tag_no_case("may")), - value(Month::June, tag_no_case("juin")), - value(Month::June, tag_no_case("jun")), - value(Month::July, tag_no_case("juil")), - value(Month::July, tag_no_case("jul")), - value(Month::August, tag_no_case("aou")), - value(Month::August, tag_no_case("aug")), - value(Month::September, tag_no_case("sep")), - value(Month::October, tag_no_case("oct")), - value(Month::November, tag_no_case("nov")), - value(Month::December, tag_no_case("dec")), - )), - alt(( - value(Month::January, tag_no_case("jr")), - value(Month::February, tag_no_case("fr")), - value(Month::March, tag_no_case("ms")), - value(Month::April, tag_no_case("al")), - value(Month::May, tag_no_case("mi")), - value(Month::June, tag_no_case("jn")), - value(Month::July, tag_no_case("jt")), - value(Month::August, tag_no_case("at")), - value(Month::September, tag_no_case("se")), - value(Month::October, tag_no_case("oe")), - value(Month::November, tag_no_case("ne")), - value(Month::December, tag_no_case("de")), - )), + value(Month::January, tag_no_case("jan")), + value(Month::February, tag_no_case("fev")), + value(Month::February, tag_no_case("feb")), + value(Month::March, tag_no_case("mar")), + value(Month::March, tag_no_case("avr")), + value(Month::April, tag_no_case("apr")), + value(Month::May, tag_no_case("mai")), + value(Month::May, tag_no_case("may")), + value(Month::June, tag_no_case("juin")), + value(Month::June, tag_no_case("jun")), + value(Month::July, tag_no_case("juil")), + value(Month::July, tag_no_case("jul")), + value(Month::August, tag_no_case("aou")), + value(Month::August, tag_no_case("aug")), + value(Month::September, tag_no_case("sep")), + value(Month::October, tag_no_case("oct")), + value(Month::November, tag_no_case("nov")), + value(Month::December, tag_no_case("dec")), )), - space0, - )(input) + alt(( + value(Month::January, tag_no_case("jr")), + value(Month::February, tag_no_case("fr")), + value(Month::March, tag_no_case("ms")), + value(Month::April, tag_no_case("al")), + value(Month::May, tag_no_case("mi")), + value(Month::June, tag_no_case("jn")), + value(Month::July, tag_no_case("jt")), + value(Month::August, tag_no_case("at")), + value(Month::September, tag_no_case("se")), + value(Month::October, tag_no_case("oe")), + value(Month::November, tag_no_case("ne")), + value(Month::December, tag_no_case("de")), + )), + ))(input) } /// Parse a weekday using either french or english day acronims @@ -145,22 +141,24 @@ fn parse_weekday(input: &str) -> IResult<&str, Weekday> { fn parse_simple_date(input: &str) -> IResult<&str, DatePoint> { alt(( map( - tuple((space0, parse_month, space0, digit1)), - |(_, month, _sep, day)| DatePoint::Date { + tuple((space0, parse_month, space0, digit1, space0)), + |(_, month, _sep, day, _)| DatePoint::Date { day: day.parse_to().unwrap(), month: Some(month), }, ), map( - tuple((space0, digit1, space0, parse_month)), - |(_, day, _, month)| DatePoint::Date { + tuple((space0, digit1, space0, parse_month, space0)), + |(_, day, _, month, _)| DatePoint::Date { day: day.parse_to().unwrap(), month: Some(month), }, ), - map(preceded(space0, digit1), |day: &str| DatePoint::Date { - day: day.parse_to().unwrap(), - month: None, + map(delimited(space0, digit1, space0), |day: &str| { + DatePoint::Date { + day: day.parse_to().unwrap(), + month: None, + } }), map(parse_weekday, DatePoint::Weekday), ))(input) @@ -192,12 +190,37 @@ mod tests { #[test] fn test_parsing() { - let (leftover_input, output) = parse_month(" jan €rré").unwrap(); + let (leftover_input, output) = parse_month("jan €rré jan feb v").unwrap(); + assert_eq!(leftover_input, " €rré jan feb v"); assert_eq!(output, Month::January); - assert_eq!(leftover_input, "€rré"); + + let (leftover_input, output) = + parse_str_into_date(" 10 jan €rré10 jan 10 jan").unwrap(); + assert_eq!(leftover_input, "€rré10 jan 10 jan"); + let result = [DateObject::Single(DatePoint::Date { + day: 10, + month: Some(Month::January), + })]; + test_dates(&output, &result); + + let (leftover_input, output) = parse_str_into_date(" 10 jan 20").unwrap(); + assert_eq!(leftover_input, "20"); + let result = [DateObject::Single(DatePoint::Date { + day: 10, + month: Some(Month::January), + })]; + test_dates(&output, &result); + + let (leftover_input, output) = parse_str_into_date("feb 10 jan 20").unwrap(); + assert_eq!(leftover_input, "jan 20"); + let result = [DateObject::Single(DatePoint::Date { + day: 10, + month: Some(Month::February), + })]; + test_dates(&output, &result); + let (leftover_input, output) = parse_str_into_date(" 10 jan - 10 jan, tue , 23,mon-tue,").unwrap(); - assert_eq!(leftover_input, ","); let result = [ DateObject::Range(DateRange { @@ -220,13 +243,18 @@ mod tests { stop: DatePoint::Weekday(Weekday::Tue), }), ]; + test_dates(&output, &result); + } + + fn test_dates(output: &Vec, result: &[DateObject]) { println!("{:?}", output); - for (a, b) in output.into_iter().zip(result.iter()) { + for (a, b) in output.iter().zip(result.iter()) { println!("Found: {:?}. Expected: {:?}", a, b); - assert_eq!(&a, b); + assert_eq!(a, b); } } } + // Procedural approach, in case all fails // /// Parse all DatesObjects separated by commas // fn parse_date_input(input: &str) -> Result, IdleError> { diff --git a/src/date/date_tools.rs b/src/date/date_tools.rs index 82aaa5d..1983560 100644 --- a/src/date/date_tools.rs +++ b/src/date/date_tools.rs @@ -1,12 +1,6 @@ //! Utilities to handle date and relative date selection use crate::error_handling::IdleError; -use chrono::Datelike; -use chrono::Duration; -use chrono::Local; -use chrono::Month; -use chrono::Months; -use chrono::NaiveDate; -use chrono::Weekday; +use chrono::{Datelike, Duration, Local, Month, Months, NaiveDate, Weekday}; /// Returns the date corresponding to the implicit Weekday /// diff --git a/src/votes.rs b/src/votes.rs index 04ffe2a..99ab501 100644 --- a/src/votes.rs +++ b/src/votes.rs @@ -7,7 +7,7 @@ //! ListCurrentEvents (struct) //! ListFutureEvents (struct) //! -#![allow(dead_code)] +#![allow(dead_code, deprecated)] use std::{collections::HashMap, sync::LazyLock};