From 4609d910da6d231d958605df23ba5b15963c21f4 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Fri, 29 Nov 2024 17:47:56 +0100 Subject: [PATCH] Adds a single helper function --- src/date/date_parse.rs | 5 ++++- src/date/date_tools.rs | 7 ++++++- src/date/mod.rs | 2 ++ src/io.rs | 8 ++++---- src/modules.rs | 2 +- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/date/date_parse.rs b/src/date/date_parse.rs index ad45b36..ab97465 100644 --- a/src/date/date_parse.rs +++ b/src/date/date_parse.rs @@ -11,6 +11,9 @@ //! //! Please note that while an invalid input will return an error, a partially valid input will yield partial results. //! +//! # Functions +//! The following functions are used to parse user data: +//! * [parse_simple_date] //! ### Usage //! ``` //! use idlefox::date::parse_str_into_date; @@ -249,7 +252,7 @@ fn parse_weekday(input: &str) -> IResult<&str, Weekday> { /// The function uses the [nom::branch::alt] combinator to handle the different date formats. /// Each format is represented by a separate [nom::combinator::map] combinator that extracts /// the necessary date information and constructs the DatePoint object. -fn parse_simple_date(input: &str) -> IResult<&str, DatePoint> { +pub fn parse_simple_date(input: &str) -> IResult<&str, DatePoint> { alt(( map( tuple((space0, parse_month, space0, digit1, space0)), diff --git a/src/date/date_tools.rs b/src/date/date_tools.rs index 3f81999..32c04c2 100644 --- a/src/date/date_tools.rs +++ b/src/date/date_tools.rs @@ -1,6 +1,6 @@ //! Utilities to handle date and relative date selection -use crate::error_handling::IdleError; +use crate::{date::date_parse::parse_simple_date, error_handling::IdleError}; use chrono::{Datelike, Duration, Local, Month, Months, NaiveDate, Weekday}; use serde::{Deserialize, Serialize}; @@ -316,6 +316,11 @@ pub fn parse_date_list( NaiveDateList::from_context(objects, context, leftover) } +pub fn parse_single_date(input: &str, context: Option) -> Result { + // As we simply ignore the leftover, we can directly call the to_naive_date method + parse_simple_date(input)?.1.to_naive_date(context) +} + #[cfg(test)] pub(crate) mod tests { use super::*; diff --git a/src/date/mod.rs b/src/date/mod.rs index 6e481f4..77538f0 100644 --- a/src/date/mod.rs +++ b/src/date/mod.rs @@ -17,6 +17,7 @@ //! left after processing the input, they can be retrieved using the `unparsed` method on the returned `Result`. //! //! The function uses the context provided to resolve any relative dates within the input string. +//! - [parse_single_date] //! //! # Examples //! @@ -54,6 +55,7 @@ pub mod date_tools; // Republishing important functions pub use date_parse::parse_str_into_date; pub use date_tools::parse_date_list; +pub use date_tools::parse_single_date; pub use date_tools::NaiveDateList; pub use date_tools::NaiveDateObject; diff --git a/src/io.rs b/src/io.rs index 9ae169e..349c75e 100644 --- a/src/io.rs +++ b/src/io.rs @@ -145,8 +145,8 @@ impl Default for WeeklyVoteModal { title: "The base test vote".into(), description: "Dummy vote".into(), group: Some("Base group".into()), - hours: Some("21h - 23".into()), - short_name: Some("Base vote".into()), + start_date: todo!(), + non_days: todo!(), } } } @@ -168,8 +168,8 @@ pub async fn vote_modal( underlying_data.group, underlying_data.title, underlying_data.description, - underlying_data.short_name, - underlying_data.hours + underlying_data.title, + underlying_data.non_days ); // This whole shenennigan only exists to find a group as specified by text and if so apply it diff --git a/src/modules.rs b/src/modules.rs index 85af193..67e7997 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -22,7 +22,7 @@ pub fn handler(vote_container: VoteContainer) { // TODO: Give vky the tools to parse dates and time // Needs to be parsed as start and end hours - let short_name = vote_container.short_name; + // let short_name = vote_container.short_name; // Needs to be parsed as single date or set to today's date if None let mut non_days = vote_container.non_days; // Needs to be parsed as a list of days spanning one week after start_date