Corrects a horrid mistake #41
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
use crate::{date::date_parse::parse_simple_date, error_handling::IdleError};
|
||||
use chrono::{Datelike, Duration, Local, Month, Months, NaiveDate, Weekday};
|
||||
use log::error;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{
|
||||
|
|
@ -72,32 +71,22 @@ pub fn relative_date_to_date(
|
|||
context: Option<NaiveDate>,
|
||||
) -> Result<NaiveDate, IdleError> {
|
||||
let context = context.unwrap_or(Local::now().date_naive());
|
||||
|
||||
// Change the base date depending on the context
|
||||
match match month {
|
||||
Some(m) if m.number_from_month() >= context.month() => context
|
||||
.with_month(m.number_from_month())
|
||||
.ok_or(IdleError::InvalidDate),
|
||||
Some(m) => context
|
||||
.checked_add_months(Months::new(12))
|
||||
// TODO: THIS IS AN INDICATOR OF A BIGGER MISTAKE
|
||||
.and_then(|d| d.with_day(day))
|
||||
.and_then(|d| d.with_month(m.number_from_month()))
|
||||
.ok_or(IdleError::InvalidDate),
|
||||
None if day >= context.day() => Ok(context),
|
||||
None => context
|
||||
.checked_add_months(Months::new(1))
|
||||
.ok_or(IdleError::InvalidDate),
|
||||
} {
|
||||
Ok(new_base) => new_base
|
||||
.with_day(day)
|
||||
.ok_or(IdleError::InvalidDate(day, month)),
|
||||
Err(_) => {
|
||||
error!("Logged an error");
|
||||
println!("EH");
|
||||
Err(IdleError::InvalidDate(day, month))
|
||||
}
|
||||
let mut new_dat = NaiveDate::from_ymd_opt(
|
||||
context.year_ce().1 as i32,
|
||||
match month {
|
||||
Some(m) => m.number_from_month(),
|
||||
None => context.month(),
|
||||
},
|
||||
day,
|
||||
)
|
||||
.ok_or(IdleError::InvalidDate(day, month))?;
|
||||
if month.is_none() && day < context.day() {
|
||||
new_dat = new_dat.checked_add_months(Months::new(1)).unwrap();
|
||||
}
|
||||
if new_dat.month() < context.month() {
|
||||
new_dat = new_dat.checked_add_months(Months::new(12)).unwrap();
|
||||
}
|
||||
Ok(new_dat)
|
||||
}
|
||||
|
||||
impl DatePoint {
|
||||
|
|
@ -321,6 +310,17 @@ pub fn parse_date_list(
|
|||
NaiveDateList::from_context(objects, context, leftover)
|
||||
}
|
||||
|
||||
/// This function parses a given input string representing a single date and returns a `NaiveDate`.
|
||||
/// The function uses the context provided to resolve any relative dates within the input string.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `input` - A string slice containing the date information to be parsed. This string may include various date formats,
|
||||
/// and possibly unparsed text.
|
||||
/// * `context` - An optional NaiveDate that provides a context for resolving relative dates within the input. If no context
|
||||
/// is provided, the function will attempt to use today's date as a fallback.
|
||||
///
|
||||
/// # Returns
|
||||
/// A Result containing a `NaiveDate` on success or an `IdleError` if parsing fails.
|
||||
pub fn parse_single_date(input: &str, context: Option<NaiveDate>) -> Result<NaiveDate, IdleError> {
|
||||
// As we simply ignore the leftover, we can directly call the to_naive_date method
|
||||
parse_simple_date(input)?.1.to_naive_date(context)
|
||||
|
|
@ -387,6 +387,15 @@ pub(crate) mod tests {
|
|||
NaiveDate::from_ymd_opt(2020, 1, 30)
|
||||
)
|
||||
.is_err());
|
||||
assert_eq!(
|
||||
relative_date_to_date(
|
||||
12,
|
||||
Some(Month::February),
|
||||
NaiveDate::from_ymd_opt(2020, 1, 30)
|
||||
)
|
||||
.unwrap(),
|
||||
NaiveDate::from_ymd_opt(2020, 2, 12).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -15,9 +15,11 @@
|
|||
//! - [parse_date_list]: This function parses a given input string representing date information and returns a `NaiveDateList`,
|
||||
//! which is a collection of sequential or random accessible date objects. If there are any unparsed parts
|
||||
//! 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]
|
||||
//!
|
||||
//! - [`parse_single_date`]: This function parses a given input string representing a single date and returns a `NaiveDate`.
|
||||
//! If there are any unparsed parts 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.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
|
|
|
|||
|
|
@ -141,8 +141,8 @@ impl Default for WeeklyVoteModal {
|
|||
title: "The base test vote".into(),
|
||||
description: "Dummy vote".into(),
|
||||
group: Some("Base group".into()),
|
||||
start_date: todo!(),
|
||||
non_days: todo!(),
|
||||
start_date: Some("10 fev".to_owned()),
|
||||
non_days: Some("10".to_owned()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -333,9 +333,9 @@ pub async fn long_vote(
|
|||
ctx: Context<'_>,
|
||||
#[description = "Titre"] title: String,
|
||||
#[description = "Description"] description: String,
|
||||
#[description = "Date de début du vote"] start_date: Option<String>,
|
||||
#[description = "Date de début de la période de l'événement"] date_begin: String,
|
||||
#[description = "Date de fin de la période de l'événement"] date_end: String,
|
||||
#[description = "Date (optionelle) de début du vote"] start_date: Option<String>,
|
||||
) -> Result<(), GenericError> {
|
||||
handler(VoteContainer {
|
||||
title,
|
||||
|
|
|
|||
Loading…
Reference in New Issue