diff --git a/leptos_webpage/src/bin/create_default_configs.rs b/leptos_webpage/src/bin/create_default_configs.rs index e2d6da0..01a6ecc 100644 --- a/leptos_webpage/src/bin/create_default_configs.rs +++ b/leptos_webpage/src/bin/create_default_configs.rs @@ -5,28 +5,29 @@ use std::path::Path; use std::{env, fs, io}; fn main() -> Result<(), String> { - let out_dir = env::var_os("TRUNK_STAGING_DIR").unwrap_or("target/default_configs".into()); - fs::create_dir_all(&out_dir).map_err(|e| format!("failed to create target directory: {e}"))?; + let out_dir = env::var_os("TRUNK_STAGING_DIR").unwrap_or("target/default_configs".into()); + fs::create_dir_all(&out_dir).map_err(|e| format!("failed to create target directory: {e}"))?; - create_default_config::("session_config", &out_dir).map_err(|e| format!("Failed to create session_config: {e}"))?; - Ok(()) + create_default_config::("session_config", &out_dir) + .map_err(|e| format!("Failed to create session_config: {e}"))?; + Ok(()) } fn create_default_config(name: &str, out_dir: P) -> io::Result<()> where - T: Serialize + Default, - P: AsRef, + T: Serialize + Default, + P: AsRef, { - create_config(name, out_dir, &T::default()) + create_config(name, out_dir, &T::default()) } fn create_config(name: &str, out_dir: P, default_config: &T) -> io::Result<()> where - T: Serialize, - P: AsRef, + T: Serialize, + P: AsRef, { - let out_path = out_dir.as_ref().join(format!("default_{name}.json")); - let out_file = File::create(&out_path)?; - serde_json::to_writer_pretty(out_file, default_config)?; - Ok(()) + let out_path = out_dir.as_ref().join(format!("default_{name}.json")); + let out_file = File::create(&out_path)?; + serde_json::to_writer_pretty(out_file, default_config)?; + Ok(()) } diff --git a/leptos_webpage/src/lib.rs b/leptos_webpage/src/lib.rs index e78f067..e0f6f66 100644 --- a/leptos_webpage/src/lib.rs +++ b/leptos_webpage/src/lib.rs @@ -4,17 +4,17 @@ use session_iter::day::Day; pub mod webpage; pub fn localize_day(day: &Day) -> String { - format!( - "{}, {}", - match day.weekday() { - Weekday::Mon => "Montag", - Weekday::Tue => "Dienstag", - Weekday::Wed => "Mittwoch", - Weekday::Thu => "Donnerstag", - Weekday::Fri => "Freitag", - Weekday::Sat => "Samstag", - Weekday::Sun => "Sonntag", - }, - day.date().format("%d.%m.%Y") - ) + format!( + "{}, {}", + match day.weekday() { + Weekday::Mon => "Montag", + Weekday::Tue => "Dienstag", + Weekday::Wed => "Mittwoch", + Weekday::Thu => "Donnerstag", + Weekday::Fri => "Freitag", + Weekday::Sat => "Samstag", + Weekday::Sun => "Sonntag", + }, + day.date().format("%d.%m.%Y") + ) } diff --git a/leptos_webpage/src/main.rs b/leptos_webpage/src/main.rs index ed4a94e..08ee84d 100644 --- a/leptos_webpage/src/main.rs +++ b/leptos_webpage/src/main.rs @@ -2,7 +2,7 @@ use jana_sessions_webpage::webpage; use leptos::prelude::*; fn main() { - console_error_panic_hook::set_once(); + console_error_panic_hook::set_once(); - mount_to_body(webpage::App); + mount_to_body(webpage::App); } diff --git a/leptos_webpage/src/webpage.rs b/leptos_webpage/src/webpage.rs index ce6bf47..0a0d2b4 100644 --- a/leptos_webpage/src/webpage.rs +++ b/leptos_webpage/src/webpage.rs @@ -6,34 +6,36 @@ use serde::{Deserialize, Serialize}; use session_iter::day::Day; use session_iter::session::iter::{DatedSession, DatedSessionIter}; use session_iter::session::rule::WeekdayOfMonth; -use session_iter::session::{Alternation, Cancellation, Dated, ExtraSession, RegularSession, Session, WithNote}; +use session_iter::session::{ + Alternation, Cancellation, Dated, ExtraSession, RegularSession, Session, WithNote, +}; #[component] pub fn App() -> impl IntoView { - let session_config = - LocalResource::new(|| async { load_config::("session_config").await }); + let session_config = + LocalResource::new(|| async { load_config::("session_config").await }); - let session_dates = move || { - session_config - .get() - .as_deref() - .cloned() - .map(|config| match config { - Ok(config) => { - let config = config.unwrap_or_default(); - view! { }.into_any() - } - Err(e) => view! { + let session_dates = move || { + session_config + .get() + .as_deref() + .cloned() + .map(|config| match config { + Ok(config) => { + let config = config.unwrap_or_default(); + view! { }.into_any() + } + Err(e) => view! {

"Error"

{e}

} - .into_any(), - }) - }; + .into_any(), + }) + }; - view! { + view! {
{session_dates}
@@ -42,11 +44,11 @@ pub fn App() -> impl IntoView { #[component] fn Sessions(config: SessionConfig) -> impl IntoView { - let mut session_iter = DatedSessionIter::new(Day::default(), config.sessions); - let (dated_sessions, mut_dated_sessions) = - signal(session_iter.by_ref().take(2).collect::>()); + let mut session_iter = DatedSessionIter::new(Day::default(), config.sessions); + let (dated_sessions, mut_dated_sessions) = + signal(session_iter.by_ref().take(2).collect::>()); - view! { + view! {

"Anstehende Proben Termine"

@@ -75,87 +77,87 @@ fn Sessions(config: SessionConfig) -> impl IntoView { #[component] fn DatedSession(session: DatedSession) -> impl IntoView { - match session { - DatedSession::Regular { session, day, .. } => view! { + match session { + DatedSession::Regular { session, day, .. } => view! {

""

{localize_day(&day)}

{session.note}

} - .into_any(), - DatedSession::Extra(session) => view! { + .into_any(), + DatedSession::Extra(session) => view! {

""

{localize_day(&session.day)}

{session.note}

} - .into_any(), - DatedSession::Canceled { regular, day, .. } => view! { + .into_any(), + DatedSession::Canceled { regular, day, .. } => view! {

""

{localize_day(&day)}

{regular.note}

} - .into_any(), - DatedSession::Altered { - regular, - day, - cause, - .. - } => { - let day = move || match cause.new_day { - None => view! { {localize_day(&day)} }.into_any(), - Some(new_day) => view! { + .into_any(), + DatedSession::Altered { + regular, + day, + cause, + .. + } => { + let day = move || match cause.new_day { + None => view! { {localize_day(&day)} }.into_any(), + Some(new_day) => view! { {localize_day(&day)} " " {localize_day(&new_day)} } - .into_any(), - }; + .into_any(), + }; - view! { + view! {

"<Änderung>"

{day}

{cause.note.or(regular.note)}

} - .into_any() - } + .into_any() } + } } async fn load_config(name: &str) -> Result, String> where - T: for<'a> Deserialize<'a>, + T: for<'a> Deserialize<'a>, { - let response = Request::get(&format!("./{name}.json")) - .send() - .await - .map_err(|e| format!("HTTP error: {e}"))?; - if response - .headers() - .get("content-type") - .is_none_or(|content_type| content_type != "application/json") - { - return Ok(None); - } + let response = Request::get(&format!("./{name}.json")) + .send() + .await + .map_err(|e| format!("HTTP error: {e}"))?; + if response + .headers() + .get("content-type") + .is_none_or(|content_type| content_type != "application/json") + { + return Ok(None); + } - let config = response - .json() - .await - .map_err(|e| format!("JSON error: {e}"))?; - Ok(Some(config)) + let config = response + .json() + .await + .map_err(|e| format!("JSON error: {e}"))?; + Ok(Some(config)) } #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(default)] pub struct SessionConfig { - pub motd: Option, - pub sessions: Vec, + pub motd: Option, + pub sessions: Vec, } macro_rules! date { @@ -165,49 +167,54 @@ macro_rules! date { } impl Default for SessionConfig { - fn default() -> Self { - const YEAR: i32 = 2024; - const NEXT_TUE_SESSION: NaiveDate = date!(18, 2, YEAR); - const NEXT_SUN_SESSION: NaiveDate = date!(2, 3, YEAR); - let next_next_sun_session = { - let some_day_next_month = NEXT_SUN_SESSION.checked_add_months(Months::new(1)).unwrap(); - NaiveDate::from_weekday_of_month_opt( - some_day_next_month.year(), - some_day_next_month.month(), - Weekday::Sun, - 1, - ) - .unwrap() - }; + fn default() -> Self { + const YEAR: i32 = 2024; + const NEXT_TUE_SESSION: NaiveDate = date!(18, 2, YEAR); + const NEXT_SUN_SESSION: NaiveDate = date!(2, 3, YEAR); + let next_next_sun_session = { + let some_day_next_month = NEXT_SUN_SESSION.checked_add_months(Months::new(1)).unwrap(); + NaiveDate::from_weekday_of_month_opt( + some_day_next_month.year(), + some_day_next_month.month(), + Weekday::Sun, + 1, + ) + .unwrap() + }; - Self { - motd: Some("Jeder 1. Sonntag und 3. Dienstag im Monat".into()), - sessions: vec![ - RegularSession::from(WeekdayOfMonth::new(1, Weekday::Sun)) - .with_note("10:00 Uhr") - .except( - NEXT_SUN_SESSION, - Alternation { - new_day: Some(NEXT_SUN_SESSION.checked_sub_days(Days::new(1)).unwrap().into()), - ..Default::default() - }, - ) - .except( - next_next_sun_session, - Alternation { - note: Some("11 Uhr".into()), - ..Default::default() - }, - ) - .into(), - RegularSession::from(WeekdayOfMonth::new(3, Weekday::Tue)) - .with_note("18:30 Uhr") - .except(NEXT_TUE_SESSION, Cancellation::new()) - .into(), - ExtraSession::from(NEXT_TUE_SESSION.checked_add_days(Days::new(2)).unwrap()) - .with_note("18 Uhr") - .into(), - ], - } + Self { + motd: Some("Jeder 1. Sonntag und 3. Dienstag im Monat".into()), + sessions: vec![ + RegularSession::from(WeekdayOfMonth::new(1, Weekday::Sun)) + .with_note("10:00 Uhr") + .except( + NEXT_SUN_SESSION, + Alternation { + new_day: Some( + NEXT_SUN_SESSION + .checked_sub_days(Days::new(1)) + .unwrap() + .into(), + ), + ..Default::default() + }, + ) + .except( + next_next_sun_session, + Alternation { + note: Some("11 Uhr".into()), + ..Default::default() + }, + ) + .into(), + RegularSession::from(WeekdayOfMonth::new(3, Weekday::Tue)) + .with_note("18:30 Uhr") + .except(NEXT_TUE_SESSION, Cancellation::new()) + .into(), + ExtraSession::from(NEXT_TUE_SESSION.checked_add_days(Days::new(2)).unwrap()) + .with_note("18 Uhr") + .into(), + ], } + } } diff --git a/session_iter/src/day.rs b/session_iter/src/day.rs index 5f74995..c5480a1 100644 --- a/session_iter/src/day.rs +++ b/session_iter/src/day.rs @@ -5,45 +5,45 @@ use std::ops::Deref; #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] #[repr(transparent)] pub struct Day { - date: NaiveDate, + date: NaiveDate, } impl From for Day { - fn from(date: NaiveDate) -> Self { - Self { date } - } + fn from(date: NaiveDate) -> Self { + Self { date } + } } impl Default for Day { - fn default() -> Self { - Self::from(Local::now().date_naive()) - } + fn default() -> Self { + Self::from(Local::now().date_naive()) + } } impl Day { - pub fn date(&self) -> NaiveDate { - self.date - } + pub fn date(&self) -> NaiveDate { + self.date + } - pub fn weekday(&self) -> Weekday { - self.date.weekday() - } + pub fn weekday(&self) -> Weekday { + self.date.weekday() + } - pub fn weekday_of_month(&self) -> u8 { - self.date.day0() as u8 / 7 + 1 - } + pub fn weekday_of_month(&self) -> u8 { + self.date.day0() as u8 / 7 + 1 + } } impl Deref for Day { - type Target = NaiveDate; + type Target = NaiveDate; - fn deref(&self) -> &Self::Target { - &self.date - } + fn deref(&self) -> &Self::Target { + &self.date + } } impl From for NaiveDate { - fn from(day: Day) -> Self { - day.date - } -} \ No newline at end of file + fn from(day: Day) -> Self { + day.date + } +} diff --git a/session_iter/src/lib.rs b/session_iter/src/lib.rs index e884830..7a926f9 100644 --- a/session_iter/src/lib.rs +++ b/session_iter/src/lib.rs @@ -2,4 +2,3 @@ pub mod day; pub mod session; #[cfg(test)] mod test_util; - diff --git a/session_iter/src/main.rs b/session_iter/src/main.rs index e71fdf5..f328e4d 100644 --- a/session_iter/src/main.rs +++ b/session_iter/src/main.rs @@ -1 +1 @@ -fn main() {} \ No newline at end of file +fn main() {} diff --git a/session_iter/src/session.rs b/session_iter/src/session.rs index 8282334..5276905 100644 --- a/session_iter/src/session.rs +++ b/session_iter/src/session.rs @@ -64,8 +64,8 @@ macro_rules! impl_from { #[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] pub enum Session { - Regular(RegularSession), - Extra(ExtraSession), + Regular(RegularSession), + Extra(ExtraSession), } impl_from!(RegularSession for Session as Regular); @@ -73,42 +73,42 @@ impl_from!(ExtraSession for Session as Extra); #[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] pub struct RegularSession { - pub rule: SessionRule, - pub note: Note, - #[serde(with = "serde_json_any_key::any_key_map")] - pub except: BTreeMap, + pub rule: SessionRule, + pub note: Note, + #[serde(with = "serde_json_any_key::any_key_map")] + pub except: BTreeMap, } impl From for RegularSession { - fn from(rule: SessionRule) -> Self { - Self { - rule, - note: None, - except: Default::default(), - } + fn from(rule: SessionRule) -> Self { + Self { + rule, + note: None, + except: Default::default(), } + } } impl RegularSession { - pub fn except(mut self, day: D, except: E) -> Self - where - D: Into, - E: Into, - { - self.except.insert(day.into(), except.into()); - self - } + pub fn except(mut self, day: D, except: E) -> Self + where + D: Into, + E: Into, + { + self.except.insert(day.into(), except.into()); + self + } - ///gets the next session day, where no except applies. Can possibly return the `current_date`. - pub fn next_regular_session_day(&self, current_date: D) -> Option - where - D: Into, - { - self - .rule - .to_session_day_iter(current_date) - .find(|day| !self.except.contains_key(day)) - } + ///gets the next session day, where no except applies. Can possibly return the `current_date`. + pub fn next_regular_session_day(&self, current_date: D) -> Option + where + D: Into, + { + self + .rule + .to_session_day_iter(current_date) + .find(|day| !self.except.contains_key(day)) + } } impl_from!(WeekdayOfMonth for RegularSession by SessionRule); @@ -117,14 +117,14 @@ impl_with_note!(RegularSession); #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] pub struct ExtraSession { - pub day: Day, - pub note: Note, + pub day: Day, + pub note: Note, } impl From for ExtraSession { - fn from(day: Day) -> Self { - Self { day, note: None } - } + fn from(day: Day) -> Self { + Self { day, note: None } + } } impl_from!(NaiveDate for ExtraSession by Day); @@ -132,15 +132,15 @@ impl_opt_noted!(ExtraSession); impl_with_note!(ExtraSession); impl Dated for ExtraSession { - fn day(&self) -> Day { - self.day - } + fn day(&self) -> Day { + self.day + } } #[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] pub enum Except { - Alternation(Alternation), - Cancellation(Cancellation), + Alternation(Alternation), + Cancellation(Cancellation), } impl_from!(Alternation for Except); @@ -149,18 +149,18 @@ impl_opt_noted!(Except with Alternation, Cancellation); #[derive(Debug, Clone, Eq, PartialEq, Hash, Default, Serialize, Deserialize)] pub struct Alternation { - pub note: Note, - ///the date when the alternation should show up in the calendar, or the original date - pub new_day: Option, + pub note: Note, + ///the date when the alternation should show up in the calendar, or the original date + pub new_day: Option, } impl From for Alternation { - fn from(day: Day) -> Self { - Self { - new_day: Some(day), - ..Default::default() - } + fn from(day: Day) -> Self { + Self { + new_day: Some(day), + ..Default::default() } + } } impl_from!(NaiveDate for Alternation by Day); @@ -170,27 +170,27 @@ impl_with_note!(Alternation); #[derive(Debug, Clone, Eq, PartialEq, Hash, Default, Serialize, Deserialize)] #[serde(default)] pub struct Cancellation { - pub note: Note, + pub note: Note, } impl Cancellation { - pub fn new() -> Self { - Self::default() - } + pub fn new() -> Self { + Self::default() + } } impl_opt_noted!(Cancellation); impl_with_note!(Cancellation); pub trait Dated { - /// The day when this should show up in a calendar - fn day(&self) -> Day; + /// The day when this should show up in a calendar + fn day(&self) -> Day; } pub trait OptNoted { - fn note(&self) -> Option<&str>; + fn note(&self) -> Option<&str>; } pub trait WithNote { - fn with_note(self, note: &str) -> Self; + fn with_note(self, note: &str) -> Self; } diff --git a/session_iter/src/session/iter.rs b/session_iter/src/session/iter.rs index d49e21b..78643a3 100644 --- a/session_iter/src/session/iter.rs +++ b/session_iter/src/session/iter.rs @@ -1,7 +1,7 @@ use crate::day::Day; use crate::session::rule::SessionRuleLike; use crate::session::{ - Alternation, Cancellation, Dated, Except, ExtraSession, OptNoted, RegularSession, Session, + Alternation, Cancellation, Dated, Except, ExtraSession, OptNoted, RegularSession, Session, }; use chrono::Days; use serde::{Deserialize, Serialize}; @@ -10,308 +10,308 @@ use std::hash::Hash; #[derive(Debug, Clone)] pub struct DatedSessionIter { - sessions: BTreeMap>, + sessions: BTreeMap>, } impl DatedSessionIter { - pub fn new(start_date: D, sessions: S) -> DatedSessionIter - where - D: Into, - S: IntoIterator, - { - let current_date = start_date.into(); + pub fn new(start_date: D, sessions: S) -> DatedSessionIter + where + D: Into, + S: IntoIterator, + { + let current_date = start_date.into(); - //map every session and their excepts to a date when they should show up in the calendar - let sessions = - sessions - .into_iter() - .flat_map(|session| match session { - Session::Regular(session) => session - .except - .iter() - .filter(|(&day, _)| session.rule.accepts(day)) - .map(|(&day, except)| match except { - Except::Alternation(alternation) => DatedSession::Altered { - day, - regular: session.clone(), - cause: alternation.clone(), - }, - Except::Cancellation(cancellation) => DatedSession::Canceled { - day, - regular: session.clone(), - cause: cancellation.clone(), - }, - }) - .chain(session.next_regular_session_day(current_date).map(|day| { - DatedSession::Regular { - day, - session: session.clone(), - } - })) - .collect(), - Session::Extra(extra) => vec![extra.into()], - }) - //filter out and entries which would lay in the past - .filter(|dated_session| dated_session.day() >= current_date) - //group sessions on the same day together - .fold( - BTreeMap::<_, VecDeque<_>>::new(), - |mut map, dated_session| { - map - .entry(dated_session.day()) - .or_default() - .push_back(dated_session); - map - }, - ); + //map every session and their excepts to a date when they should show up in the calendar + let sessions = + sessions + .into_iter() + .flat_map(|session| match session { + Session::Regular(session) => session + .except + .iter() + .filter(|(&day, _)| session.rule.accepts(day)) + .map(|(&day, except)| match except { + Except::Alternation(alternation) => DatedSession::Altered { + day, + regular: session.clone(), + cause: alternation.clone(), + }, + Except::Cancellation(cancellation) => DatedSession::Canceled { + day, + regular: session.clone(), + cause: cancellation.clone(), + }, + }) + .chain(session.next_regular_session_day(current_date).map(|day| { + DatedSession::Regular { + day, + session: session.clone(), + } + })) + .collect(), + Session::Extra(extra) => vec![extra.into()], + }) + //filter out and entries which would lay in the past + .filter(|dated_session| dated_session.day() >= current_date) + //group sessions on the same day together + .fold( + BTreeMap::<_, VecDeque<_>>::new(), + |mut map, dated_session| { + map + .entry(dated_session.day()) + .or_default() + .push_back(dated_session); + map + }, + ); - Self { sessions } - } + Self { sessions } + } } impl Iterator for DatedSessionIter { - type Item = DatedSession; + type Item = DatedSession; - fn next(&mut self) -> Option { - let mut entry = self.sessions.first_entry()?; - let session = entry.get_mut().pop_front()?; - if entry.get().is_empty() { - entry.remove(); - } - - //make sure regular sessions remain in map - if let DatedSession::Regular { ref session, day } = session { - //calculate next day, because next_regular_session_day of a session day returns that very session day - if let Some(next_day) = day.checked_add_days(Days::new(1)) { - if let Some(next_session_day) = session.next_regular_session_day(next_day) { - self - .sessions - .entry(next_session_day) - .or_default() - .push_back(DatedSession::Regular { - day: next_session_day, - session: session.clone(), - }) - } - } - } - Some(session) + fn next(&mut self) -> Option { + let mut entry = self.sessions.first_entry()?; + let session = entry.get_mut().pop_front()?; + if entry.get().is_empty() { + entry.remove(); } + + //make sure regular sessions remain in map + if let DatedSession::Regular { ref session, day } = session { + //calculate next day, because next_regular_session_day of a session day returns that very session day + if let Some(next_day) = day.checked_add_days(Days::new(1)) { + if let Some(next_session_day) = session.next_regular_session_day(next_day) { + self + .sessions + .entry(next_session_day) + .or_default() + .push_back(DatedSession::Regular { + day: next_session_day, + session: session.clone(), + }) + } + } + } + Some(session) + } } #[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] pub enum DatedSession { - Regular { - day: Day, - session: RegularSession, - }, - Extra(ExtraSession), - Canceled { - day: Day, - regular: RegularSession, - cause: Cancellation, - }, - Altered { - ///the day when the regular session would have applied - day: Day, - regular: RegularSession, - cause: Alternation, - }, + Regular { + day: Day, + session: RegularSession, + }, + Extra(ExtraSession), + Canceled { + day: Day, + regular: RegularSession, + cause: Cancellation, + }, + Altered { + ///the day when the regular session would have applied + day: Day, + regular: RegularSession, + cause: Alternation, + }, } impl From for DatedSession { - fn from(value: ExtraSession) -> Self { - Self::Extra(value) - } + fn from(value: ExtraSession) -> Self { + Self::Extra(value) + } } impl Dated for DatedSession { - fn day(&self) -> Day { - match *self { - DatedSession::Regular { day, .. } => day, - DatedSession::Extra(ref extra) => extra.day(), - DatedSession::Canceled { day, .. } => day, - DatedSession::Altered { day, ref cause, .. } => cause.new_day.unwrap_or(day), - } + fn day(&self) -> Day { + match *self { + DatedSession::Regular { day, .. } => day, + DatedSession::Extra(ref extra) => extra.day(), + DatedSession::Canceled { day, .. } => day, + DatedSession::Altered { day, ref cause, .. } => cause.new_day.unwrap_or(day), } + } } impl OptNoted for DatedSession { - fn note(&self) -> Option<&str> { - match self { - DatedSession::Regular { session, .. } => session.note(), - DatedSession::Extra(extra) => extra.note(), - DatedSession::Canceled { cause, .. } => cause.note(), - DatedSession::Altered { cause, .. } => cause.note(), - } + fn note(&self) -> Option<&str> { + match self { + DatedSession::Regular { session, .. } => session.note(), + DatedSession::Extra(extra) => extra.note(), + DatedSession::Canceled { cause, .. } => cause.note(), + DatedSession::Altered { cause, .. } => cause.note(), } + } } #[cfg(test)] mod test { - use crate::session::iter::{DatedSession, DatedSessionIter}; - use crate::session::rule::WeekdayOfMonth; - use crate::session::{Alternation, Cancellation, ExtraSession, RegularSession, WithNote}; - use crate::test_util::{date, day}; - use chrono::Weekday; + use crate::session::iter::{DatedSession, DatedSessionIter}; + use crate::session::rule::WeekdayOfMonth; + use crate::session::{Alternation, Cancellation, ExtraSession, RegularSession, WithNote}; + use crate::test_util::{date, day}; + use chrono::Weekday; - #[test] - fn test_regular() { - let tue_session = RegularSession::from(WeekdayOfMonth::new(3, Weekday::Tue)); - let sun_session = RegularSession::from(WeekdayOfMonth::new(1, Weekday::Sun)); - let mut iter = DatedSessionIter::new( - date(17, 2, 2025), - [tue_session.clone().into(), sun_session.clone().into()], - ); + #[test] + fn test_regular() { + let tue_session = RegularSession::from(WeekdayOfMonth::new(3, Weekday::Tue)); + let sun_session = RegularSession::from(WeekdayOfMonth::new(1, Weekday::Sun)); + let mut iter = DatedSessionIter::new( + date(17, 2, 2025), + [tue_session.clone().into(), sun_session.clone().into()], + ); - assert_eq!( - iter.next(), - Some(DatedSession::Regular { - day: day(18, 2, 2025), - session: tue_session.clone() - }) - ); - assert_eq!( - iter.next(), - Some(DatedSession::Regular { - day: day(2, 3, 2025), - session: sun_session.clone() - }) - ); - assert_eq!( - iter.next(), - Some(DatedSession::Regular { - day: day(18, 3, 2025), - session: tue_session.clone() - }) - ); - } + assert_eq!( + iter.next(), + Some(DatedSession::Regular { + day: day(18, 2, 2025), + session: tue_session.clone() + }) + ); + assert_eq!( + iter.next(), + Some(DatedSession::Regular { + day: day(2, 3, 2025), + session: sun_session.clone() + }) + ); + assert_eq!( + iter.next(), + Some(DatedSession::Regular { + day: day(18, 3, 2025), + session: tue_session.clone() + }) + ); + } - #[test] - fn test_old() { - let extra_session = ExtraSession::from(day(15, 2, 2025)); - let regular_session = RegularSession::from(WeekdayOfMonth::new(3, Weekday::Tue)) - .with_note("18:30 Uhr") - .except(day(18, 2, 2025), Cancellation::new()) - .except(day(18, 3, 2025), Alternation::from(day(21, 1, 2025))); + #[test] + fn test_old() { + let extra_session = ExtraSession::from(day(15, 2, 2025)); + let regular_session = RegularSession::from(WeekdayOfMonth::new(3, Weekday::Tue)) + .with_note("18:30 Uhr") + .except(day(18, 2, 2025), Cancellation::new()) + .except(day(18, 3, 2025), Alternation::from(day(21, 1, 2025))); - let mut iter = DatedSessionIter::new( - day(19, 2, 2025), - [regular_session.clone().into(), extra_session.into()], - ); + let mut iter = DatedSessionIter::new( + day(19, 2, 2025), + [regular_session.clone().into(), extra_session.into()], + ); - assert_eq!( - iter.next(), - Some(DatedSession::Regular { - day: day(15, 4, 2025), - session: regular_session.clone(), - }) - ); - } + assert_eq!( + iter.next(), + Some(DatedSession::Regular { + day: day(15, 4, 2025), + session: regular_session.clone(), + }) + ); + } - #[test] - fn test_invalid_except() { - let regular_session = RegularSession::from(WeekdayOfMonth::new(3, Weekday::Tue)) - .except(day(17, 2, 2025), Cancellation::new()); + #[test] + fn test_invalid_except() { + let regular_session = RegularSession::from(WeekdayOfMonth::new(3, Weekday::Tue)) + .except(day(17, 2, 2025), Cancellation::new()); - let mut iter = DatedSessionIter::new(day(17, 2, 2025), [regular_session.clone().into()]); + let mut iter = DatedSessionIter::new(day(17, 2, 2025), [regular_session.clone().into()]); - assert_eq!( - iter.next(), - Some(DatedSession::Regular { - day: day(18, 2, 2025), - session: regular_session.clone() - }) - ); - } + assert_eq!( + iter.next(), + Some(DatedSession::Regular { + day: day(18, 2, 2025), + session: regular_session.clone() + }) + ); + } - #[test] - fn test_extra_altered_and_canceled() { - //an extra session on the same day as a tuesday session - let extra_session = ExtraSession::from(day(18, 2, 2025)).with_note("morning session"); - //an alternation of a tuesday session without moving it - let (note_alternation_day, note_alternation) = ( - day(18, 3, 2025), - Alternation { - note: Some("a little different today".into()), - ..Default::default() - }, - ); - let tue_session = RegularSession::from(WeekdayOfMonth::new(3, Weekday::Tue)) - .except(note_alternation_day, note_alternation.clone()); - //a sunday session moved in front of a tuesday session - let moved_sun_session_day = day(16, 2, 2025); - let (day_alternation_day, day_alternation) = - (day(2, 3, 2025), Alternation::from(moved_sun_session_day)); - //a canceled sunday session - let (cancel_day, cancellation) = (day(6, 4, 2025), Cancellation::new()); - let sun_session = RegularSession::from(WeekdayOfMonth::new(1, Weekday::Sun)) - .except(day_alternation_day, day_alternation.clone()) - .except(cancel_day, cancellation.clone()); + #[test] + fn test_extra_altered_and_canceled() { + //an extra session on the same day as a tuesday session + let extra_session = ExtraSession::from(day(18, 2, 2025)).with_note("morning session"); + //an alternation of a tuesday session without moving it + let (note_alternation_day, note_alternation) = ( + day(18, 3, 2025), + Alternation { + note: Some("a little different today".into()), + ..Default::default() + }, + ); + let tue_session = RegularSession::from(WeekdayOfMonth::new(3, Weekday::Tue)) + .except(note_alternation_day, note_alternation.clone()); + //a sunday session moved in front of a tuesday session + let moved_sun_session_day = day(16, 2, 2025); + let (day_alternation_day, day_alternation) = + (day(2, 3, 2025), Alternation::from(moved_sun_session_day)); + //a canceled sunday session + let (cancel_day, cancellation) = (day(6, 4, 2025), Cancellation::new()); + let sun_session = RegularSession::from(WeekdayOfMonth::new(1, Weekday::Sun)) + .except(day_alternation_day, day_alternation.clone()) + .except(cancel_day, cancellation.clone()); - let mut iter = DatedSessionIter::new( - day(15, 2, 2025), - [ - extra_session.clone().into(), - tue_session.clone().into(), - sun_session.clone().into(), - ], - ); + let mut iter = DatedSessionIter::new( + day(15, 2, 2025), + [ + extra_session.clone().into(), + tue_session.clone().into(), + sun_session.clone().into(), + ], + ); - //at first comes the moved sunday - assert_eq!( - iter.next(), - Some(DatedSession::Altered { - day: day_alternation_day, - regular: sun_session.clone(), - cause: day_alternation.clone(), - }) - ); - //then comes the extra session on tuesday (because it was first in the list) - assert_eq!( - iter.next(), - Some(DatedSession::Extra(extra_session.clone())) - ); - //after that comes a regular tuesday session - assert_eq!( - iter.next(), - Some(DatedSession::Regular { - day: day(18, 2, 2025), - session: tue_session.clone() - }) - ); - //now we have an altered but not moved tuesday session - assert_eq!( - iter.next(), - Some(DatedSession::Altered { - day: note_alternation_day, - regular: tue_session.clone(), - cause: note_alternation.clone(), - }) - ); - //the next sunday session was canceled - assert_eq!( - iter.next(), - Some(DatedSession::Canceled { - day: cancel_day, - regular: sun_session.clone(), - cause: cancellation.clone(), - }) - ); - //and now we are back to regular sessions - assert_eq!( - iter.next(), - Some(DatedSession::Regular { - day: day(15, 4, 2025), - session: tue_session.clone() - }) - ); - assert_eq!( - iter.next(), - Some(DatedSession::Regular { - day: day(4, 5, 2025), - session: sun_session.clone() - }) - ); - } + //at first comes the moved sunday + assert_eq!( + iter.next(), + Some(DatedSession::Altered { + day: day_alternation_day, + regular: sun_session.clone(), + cause: day_alternation.clone(), + }) + ); + //then comes the extra session on tuesday (because it was first in the list) + assert_eq!( + iter.next(), + Some(DatedSession::Extra(extra_session.clone())) + ); + //after that comes a regular tuesday session + assert_eq!( + iter.next(), + Some(DatedSession::Regular { + day: day(18, 2, 2025), + session: tue_session.clone() + }) + ); + //now we have an altered but not moved tuesday session + assert_eq!( + iter.next(), + Some(DatedSession::Altered { + day: note_alternation_day, + regular: tue_session.clone(), + cause: note_alternation.clone(), + }) + ); + //the next sunday session was canceled + assert_eq!( + iter.next(), + Some(DatedSession::Canceled { + day: cancel_day, + regular: sun_session.clone(), + cause: cancellation.clone(), + }) + ); + //and now we are back to regular sessions + assert_eq!( + iter.next(), + Some(DatedSession::Regular { + day: day(15, 4, 2025), + session: tue_session.clone() + }) + ); + assert_eq!( + iter.next(), + Some(DatedSession::Regular { + day: day(4, 5, 2025), + session: sun_session.clone() + }) + ); + } } diff --git a/session_iter/src/session/rule.rs b/session_iter/src/session/rule.rs index 3d3155c..b7793b2 100644 --- a/session_iter/src/session/rule.rs +++ b/session_iter/src/session/rule.rs @@ -5,148 +5,148 @@ use std::ops::Deref; #[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] pub enum SessionRule { - WeekdayOfMonth(WeekdayOfMonth), + WeekdayOfMonth(WeekdayOfMonth), } impl From for SessionRule { - fn from(value: WeekdayOfMonth) -> Self { - Self::WeekdayOfMonth(value) - } + fn from(value: WeekdayOfMonth) -> Self { + Self::WeekdayOfMonth(value) + } } impl SessionRule { - pub fn to_session_day_iter(&self, start_date: D) -> SessionDayIter<&Self> - where - D: Into, - { - SessionDayIter { - rule: self, - start_date: Some(start_date.into()), - } + pub fn to_session_day_iter(&self, start_date: D) -> SessionDayIter<&Self> + where + D: Into, + { + SessionDayIter { + rule: self, + start_date: Some(start_date.into()), } + } } impl SessionRuleLike for SessionRule { - fn determine_next_date(&self, current_date: Day) -> Option { - match self { - SessionRule::WeekdayOfMonth(weekday_of_month) => { - weekday_of_month.determine_next_date(current_date) - } - } + fn determine_next_date(&self, current_date: Day) -> Option { + match self { + SessionRule::WeekdayOfMonth(weekday_of_month) => { + weekday_of_month.determine_next_date(current_date) + } } + } - fn accepts(&self, day: Day) -> bool { - match self { - SessionRule::WeekdayOfMonth(weekday_of_month) => weekday_of_month.accepts(day), - } + fn accepts(&self, day: Day) -> bool { + match self { + SessionRule::WeekdayOfMonth(weekday_of_month) => weekday_of_month.accepts(day), } + } } #[derive(Debug, Clone)] pub struct SessionDayIter { - pub rule: R, - pub start_date: Option, + pub rule: R, + pub start_date: Option, } impl Iterator for SessionDayIter where - R: Deref, - S: SessionRuleLike, + R: Deref, + S: SessionRuleLike, { - type Item = Day; + type Item = Day; - fn next(&mut self) -> Option { - let start_date = self.start_date?; - let session_date = self.rule.determine_next_date(start_date); - self.start_date = session_date - .and_then(|session_date| session_date.checked_add_days(Days::new(1))) - .map(Day::from); - session_date - } + fn next(&mut self) -> Option { + let start_date = self.start_date?; + let session_date = self.rule.determine_next_date(start_date); + self.start_date = session_date + .and_then(|session_date| session_date.checked_add_days(Days::new(1))) + .map(Day::from); + session_date + } } impl SessionDayIter where - R: Deref, + R: Deref, { - pub fn to_owned(&self) -> SessionDayIter { - SessionDayIter { - rule: self.rule.clone(), - start_date: self.start_date, - } + pub fn to_owned(&self) -> SessionDayIter { + SessionDayIter { + rule: self.rule.clone(), + start_date: self.start_date, } + } } #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] pub struct WeekdayOfMonth { - pub n: u8, - pub weekday: Weekday, + pub n: u8, + pub weekday: Weekday, } impl WeekdayOfMonth { - pub fn new(n: u8, weekday: Weekday) -> Self { - Self { n, weekday } - } + pub fn new(n: u8, weekday: Weekday) -> Self { + Self { n, weekday } + } } impl SessionRuleLike for WeekdayOfMonth { - fn determine_next_date(&self, current_date: Day) -> Option { - let session_this_month = NaiveDate::from_weekday_of_month_opt( - current_date.year(), - current_date.month(), - self.weekday, - self.n, - )?; + fn determine_next_date(&self, current_date: Day) -> Option { + let session_this_month = NaiveDate::from_weekday_of_month_opt( + current_date.year(), + current_date.month(), + self.weekday, + self.n, + )?; - let date = if session_this_month >= current_date.date() { - session_this_month - } else { - let session_next_month = current_date.checked_add_months(Months::new(1))?; - NaiveDate::from_weekday_of_month_opt( - session_next_month.year(), - session_next_month.month(), - self.weekday, - self.n, - )? - }; + let date = if session_this_month >= current_date.date() { + session_this_month + } else { + let session_next_month = current_date.checked_add_months(Months::new(1))?; + NaiveDate::from_weekday_of_month_opt( + session_next_month.year(), + session_next_month.month(), + self.weekday, + self.n, + )? + }; - Some(date.into()) - } + Some(date.into()) + } - fn accepts(&self, day: Day) -> bool { - day.weekday() == self.weekday && day.weekday_of_month() == self.n - } + fn accepts(&self, day: Day) -> bool { + day.weekday() == self.weekday && day.weekday_of_month() == self.n + } } pub trait SessionRuleLike { - /// Determines the next session date in form of a [Day], possibly including the `current_date`. - fn determine_next_date(&self, current_date: Day) -> Option; + /// Determines the next session date in form of a [Day], possibly including the `current_date`. + fn determine_next_date(&self, current_date: Day) -> Option; - /// Whether this rule would be able to produce the given `day`. - fn accepts(&self, day: Day) -> bool; + /// Whether this rule would be able to produce the given `day`. + fn accepts(&self, day: Day) -> bool; } #[cfg(test)] mod test_weekday_of_month { - use crate::session::rule::{SessionRuleLike, WeekdayOfMonth}; - use crate::test_util::day; - use chrono::Weekday; + use crate::session::rule::{SessionRuleLike, WeekdayOfMonth}; + use crate::test_util::day; + use chrono::Weekday; - #[test] - fn test_next_date() { - let rule = WeekdayOfMonth::new(3, Weekday::Tue); + #[test] + fn test_next_date() { + let rule = WeekdayOfMonth::new(3, Weekday::Tue); - assert_eq!( - rule.determine_next_date(day(17, 2, 2025)), - Some(day(18, 2, 2025)) - ); - assert_eq!( - rule.determine_next_date(day(18, 2, 2025)), - Some(day(18, 2, 2025)) - ); - assert_eq!( - rule.determine_next_date(day(19, 2, 2025)), - Some(day(18, 3, 2025)) - ); - } + assert_eq!( + rule.determine_next_date(day(17, 2, 2025)), + Some(day(18, 2, 2025)) + ); + assert_eq!( + rule.determine_next_date(day(18, 2, 2025)), + Some(day(18, 2, 2025)) + ); + assert_eq!( + rule.determine_next_date(day(19, 2, 2025)), + Some(day(18, 3, 2025)) + ); + } } diff --git a/session_iter/src/test_util.rs b/session_iter/src/test_util.rs index b138f7d..a3b201d 100644 --- a/session_iter/src/test_util.rs +++ b/session_iter/src/test_util.rs @@ -2,9 +2,9 @@ use crate::day::Day; use chrono::NaiveDate; pub fn date(day: u32, month: u32, year: i32) -> NaiveDate { - NaiveDate::from_ymd_opt(year, month, day).expect("valid date") + NaiveDate::from_ymd_opt(year, month, day).expect("valid date") } pub fn day(day: u32, month: u32, year: i32) -> Day { - date(day, month, year).into() -} \ No newline at end of file + date(day, month, year).into() +}