refactor: remove month_from_n function

This commit is contained in:
ThetaDev 2023-04-20 22:01:24 +02:00
parent ffa1e51a2b
commit ea80f8463d
3 changed files with 5 additions and 22 deletions

View file

@ -18,7 +18,7 @@
use std::ops::Mul;
use serde::{Deserialize, Serialize};
use time::{Date, Duration, OffsetDateTime};
use time::{Date, Duration, Month, OffsetDateTime};
use crate::{
param::Language,
@ -261,7 +261,8 @@ pub fn parse_textual_date(lang: Language, textual_date: &str) -> Option<ParsedDa
}
match (y, m, d) {
(Some(y), Some(m), Some(d)) => util::month_from_n(m as u8)
(Some(y), Some(m), Some(d)) => Month::try_from(m as u8)
.ok()
.and_then(|m| Date::from_calendar_date(y.into(), m, d as u8).ok())
.map(ParsedDate::Absolute),
_ => None,

View file

@ -1,23 +1,5 @@
use time::{Date, Month, OffsetDateTime};
pub const fn month_from_n(n: u8) -> Option<Month> {
match n {
1 => Some(Month::January),
2 => Some(Month::February),
3 => Some(Month::March),
4 => Some(Month::April),
5 => Some(Month::May),
6 => Some(Month::June),
7 => Some(Month::July),
8 => Some(Month::August),
9 => Some(Month::September),
10 => Some(Month::October),
11 => Some(Month::November),
12 => Some(Month::December),
_ => None,
}
}
/// Shift a date by the given number of months.
/// Ambiguous month-ends are shifted backwards as necessary.
pub fn shift_months(date: Date, months: i32) -> Date {
@ -30,7 +12,7 @@ pub fn shift_months(date: Date, months: i32) -> Date {
month += 12;
}
let month = month_from_n(month as u8).unwrap();
let month = Month::try_from(month as u8).unwrap();
let month_days = time::util::days_in_year_month(year, month);
day = day.min(month_days);

View file

@ -3,7 +3,7 @@ mod protobuf;
pub mod dictionary;
pub use date::{month_from_n, now_sec, shift_months, shift_years};
pub use date::{now_sec, shift_months, shift_years};
pub use protobuf::{string_from_pb, ProtoBuilder};
use std::{