Compare commits

...

7 Commits

Author SHA1 Message Date
796412888d Cleanup Cargo.toml 2025-02-21 11:49:19 +01:00
796aa3e846 Cleanup assets and copy over old styles and title 2025-02-21 11:47:37 +01:00
e0a227486b Cleanup app 2025-02-21 11:45:29 +01:00
7b28c580f4 Add dioxus template for simpler experimenting 2025-02-21 11:37:07 +01:00
3c12e3ff29 Reformat project 2025-02-21 10:52:55 +01:00
1d760a2415 Reformat project 2025-02-21 10:49:50 +01:00
f9aae520e9 Add dioxus project skeleton 2025-02-20 10:56:28 +01:00
26 changed files with 1087 additions and 576 deletions

View File

@ -2,7 +2,7 @@
resolver = "2"
members = [
"leptos_webpage", "session_iter", "app",
"leptos_webpage", "session_iter", "app", "dioxus_template",
]
[profile]

7
app/.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
# Generated by Cargo
# will have compiled files and executables
/target
.DS_Store
# These are backup files generated by rustfmt
**/*.rs.bk

16
app/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "app"
version = "0.1.0"
authors = ["steppy <jonlaurem@gmail.com>"]
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dioxus = { version = "0.6.0", features = ["fullstack"] }
[features]
default = ["web"]
web = ["dioxus/web"]
desktop = ["dioxus/desktop"]
mobile = ["dioxus/mobile"]

24
app/Dioxus.toml Normal file
View File

@ -0,0 +1,24 @@
[application]
# App (Project) Name
name = "app"
[web.app]
# HTML title tag content
title = "Band Sessions"
# include `assets` in web platform
[web.resource]
# Additional CSS style files
style = []
# Additional JavaScript files
script = []
[web.resource.dev]
# Javascript code file
# serve: [dev-server] only
script = []

18
app/README.md Normal file
View File

@ -0,0 +1,18 @@
# Development
Your new bare-bones project includes minimal organization with a single `main.rs` file and a few assets.
### Serving Your App
Run the following command in the root of your project to start developing with the default platform:
```bash
dx serve --platform web
```
To run for a different platform, use the `--platform platform` flag. E.g.
```bash
dx serve --platform desktop
```

112
app/assets/styles.css Normal file
View File

@ -0,0 +1,112 @@
:root {
--darkred: darkred;
--darkgreen: #196e0a;
--darkgray: #292929;
--gray: #606060;
--white: white;
--black: black;
--red: red;
--yellow: #bbbb11;
--green: #1fd51f;
}
ul {
list-style-position: inside;
}
body {
font-size: 2rem;
}
html, body {
margin: 0;
padding: 0;
}
p {
margin: 0;
}
@media screen and (max-width: 1000px) {
body {
font-size: 3rem;
}
}
.background {
background-color: var(--darkgray);
color: var(--white);
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
overflow: scroll;
}
.column {
display: flex;
flex-direction: column;
gap: 10px;
}
.box {
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
text-align: center;
justify-content: center;
align-items: center;
margin: 0 auto;
width: 80%;
max-width: 800px;
box-sizing: border-box;
}
.wide {
width: 100%;
max-width: 1000px;
}
.button {
cursor: pointer;
}
.button:hover {
filter: brightness(1.2);
}
.elem-background {
background-color: var(--darkgreen);
}
.error-background {
background-color: var(--darkred);
}
.highlight-background {
background-color: var(--green);
}
.cancel-background {
background-color: var(--red);
}
.change-background {
background-color: var(--yellow);
}
.small-text {
color: var(--darkgray);
font-size: 1rem;
text-align: left;
}
.strikethrough {
text-decoration: line-through;
}
.bold {
font-weight: bold;
}

15
app/src/main.rs Normal file
View File

@ -0,0 +1,15 @@
use dioxus::prelude::*;
const MAIN_CSS: Asset = asset!("/assets/styles.css");
fn main() {
launch(App);
}
#[component]
fn App() -> Element {
rsx! {
document::Link { rel: "stylesheet", href: MAIN_CSS }
"Hello world"
}
}

7
dioxus_template/.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
# Generated by Cargo
# will have compiled files and executables
/target
.DS_Store
# These are backup files generated by rustfmt
**/*.rs.bk

View File

@ -0,0 +1,16 @@
[package]
name = "dioxus_template"
version = "0.1.0"
authors = ["steppy <jonlaurem@gmail.com>"]
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dioxus = { version = "0.6.0", features = ["router", "fullstack"] }
[features]
default = ["web"]
web = ["dioxus/web"]
desktop = ["dioxus/desktop"]
mobile = ["dioxus/mobile"]

View File

@ -0,0 +1,24 @@
[application]
# App (Project) Name
name = "dioxus_template"
[web.app]
# HTML title tag content
title = "dioxus_template"
# include `assets` in web platform
[web.resource]
# Additional CSS style files
style = []
# Additional JavaScript files
script = []
[web.resource.dev]
# Javascript code file
# serve: [dev-server] only
script = []

17
dioxus_template/README.md Normal file
View File

@ -0,0 +1,17 @@
# Development
Your new bare-bones project includes minimal organization with a single `main.rs` file and a few assets.
### Serving Your App
Run the following command in the root of your project to start developing with the default platform:
```bash
dx serve --platform web
```
To run for a different platform, use the `--platform platform` flag. E.g.
```bash
dx serve --platform desktop
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 23 KiB

View File

@ -0,0 +1,107 @@
/* App-wide styling */
body {
background-color: #0f1116;
color: #ffffff;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 20px;
}
#hero {
margin: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
#links {
width: 400px;
text-align: left;
font-size: x-large;
color: white;
display: flex;
flex-direction: column;
}
#links a {
color: white;
text-decoration: none;
margin-top: 20px;
margin: 10px 0px;
border: white 1px solid;
border-radius: 5px;
padding: 10px;
}
#links a:hover {
background-color: #1f1f1f;
cursor: pointer;
}
#header {
max-width: 1200px;
}
/* Navbar */
#navbar {
display: flex;
flex-direction: row;
}
#navbar a {
color: #ffffff;
margin-right: 20px;
text-decoration: none;
transition: color 0.2s ease;
}
#navbar a:hover {
cursor: pointer;
color: #91a4d2;
}
/* Blog page */
#blog {
margin-top: 50px;
}
#blog a {
color: #ffffff;
margin-top: 50px;
}
/* Echo */
#echo {
width: 360px;
margin-left: auto;
margin-right: auto;
margin-top: 50px;
background-color: #1e222d;
padding: 20px;
border-radius: 10px;
}
#echo>h4 {
margin: 0px 0px 15px 0px;
}
#echo>input {
border: none;
border-bottom: 1px white solid;
background-color: transparent;
color: #ffffff;
transition: border-bottom-color 0.2s ease;
outline: none;
display: block;
padding: 0px 0px 5px 0px;
width: 100%;
}
#echo>input:focus {
border-bottom-color: #6d85c6;
}
#echo>p {
margin: 20px 0px 0px auto;
}

121
dioxus_template/src/main.rs Normal file
View File

@ -0,0 +1,121 @@
use dioxus::prelude::*;
#[derive(Debug, Clone, Routable, PartialEq)]
#[rustfmt::skip]
enum Route {
#[layout(Navbar)]
#[route("/")]
Home {},
#[route("/blog/:id")]
Blog { id: i32 },
}
const FAVICON: Asset = asset!("/assets/favicon.ico");
const MAIN_CSS: Asset = asset!("/assets/main.css");
const HEADER_SVG: Asset = asset!("/assets/header.svg");
fn main() {
launch(App);
}
#[component]
fn App() -> Element {
rsx! {
document::Link { rel: "icon", href: FAVICON }
document::Link { rel: "stylesheet", href: MAIN_CSS }
Router::<Route> {}
}
}
#[component]
pub fn Hero() -> Element {
rsx! {
div { id: "hero",
img { src: HEADER_SVG, id: "header" }
div { id: "links",
a { href: "https://dioxuslabs.com/learn/0.6/", "📚 Learn Dioxus" }
a { href: "https://dioxuslabs.com/awesome", "🚀 Awesome Dioxus" }
a { href: "https://github.com/dioxus-community/", "📡 Community Libraries" }
a { href: "https://github.com/DioxusLabs/sdk", "⚙️ Dioxus Development Kit" }
a { href: "https://marketplace.visualstudio.com/items?itemName=DioxusLabs.dioxus",
"💫 VSCode Extension"
}
a { href: "https://discord.gg/XgGxMSkvUM", "👋 Community Discord" }
}
}
}
}
/// Home page
#[component]
fn Home() -> Element {
rsx! {
Hero {}
Echo {}
}
}
/// Blog page
#[component]
pub fn Blog(id: i32) -> Element {
rsx! {
div { id: "blog",
// Content
h1 { "This is blog #{id}!" }
p {
"In blog #{id}, we show how the Dioxus router works and how URL parameters can be passed as props to our route components."
}
// Navigation links
Link { to: Route::Blog { id: id - 1 }, "Previous" }
span { " <---> " }
Link { to: Route::Blog { id: id + 1 }, "Next" }
}
}
}
/// Shared navbar component.
#[component]
fn Navbar() -> Element {
rsx! {
div { id: "navbar",
Link { to: Route::Home {}, "Home" }
Link { to: Route::Blog { id: 1 }, "Blog" }
}
Outlet::<Route> {}
}
}
/// Echo component that demonstrates fullstack server functions.
#[component]
fn Echo() -> Element {
let mut response = use_signal(|| String::new());
rsx! {
div { id: "echo",
h4 { "ServerFn Echo" }
input {
placeholder: "Type here to echo...",
oninput: move |event| async move {
let data = echo_server(event.value()).await.unwrap();
response.set(data);
},
}
if !response().is_empty() {
p {
"Server echoed: "
i { "{response}" }
}
}
}
}
}
/// Echo the user input on the server.
#[server(EchoServer)]
async fn echo_server(input: String) -> Result<String, ServerFnError> {
Ok(input)
}

View File

@ -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::<SessionConfig, _>("session_config", &out_dir).map_err(|e| format!("Failed to create session_config: {e}"))?;
Ok(())
create_default_config::<SessionConfig, _>("session_config", &out_dir)
.map_err(|e| format!("Failed to create session_config: {e}"))?;
Ok(())
}
fn create_default_config<T, P>(name: &str, out_dir: P) -> io::Result<()>
where
T: Serialize + Default,
P: AsRef<Path>,
T: Serialize + Default,
P: AsRef<Path>,
{
create_config(name, out_dir, &T::default())
create_config(name, out_dir, &T::default())
}
fn create_config<T, P>(name: &str, out_dir: P, default_config: &T) -> io::Result<()>
where
T: Serialize,
P: AsRef<Path>,
T: Serialize,
P: AsRef<Path>,
{
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(())
}

View File

@ -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")
)
}

View File

@ -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);
}

View File

@ -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::<SessionConfig>("session_config").await });
let session_config =
LocalResource::new(|| async { load_config::<SessionConfig>("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! { <Sessions config /> }.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! { <Sessions config /> }.into_any()
}
Err(e) => view! {
<div class="box error-background">
<h1>"Error"</h1>
<p>{e}</p>
</div>
}
.into_any(),
})
};
.into_any(),
})
};
view! {
view! {
<div class="background">
<Suspense fallback=|| "Laden...">{session_dates}</Suspense>
</div>
@ -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::<Vec<_>>());
let mut session_iter = DatedSessionIter::new(Day::default(), config.sessions);
let (dated_sessions, mut_dated_sessions) =
signal(session_iter.by_ref().take(2).collect::<Vec<_>>());
view! {
view! {
<div class="column">
<div class="wide box elem-background">
<h1>"Anstehende Proben Termine"</h1>
@ -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! {
<div class="box elem-background">
<p class="small-text">"<Regulärer Termin>"</p>
<p><b>{localize_day(&day)}</b></p>
<p>{session.note}</p>
</div>
}
.into_any(),
DatedSession::Extra(session) => view! {
.into_any(),
DatedSession::Extra(session) => view! {
<div class="box highlight-background">
<p class="small-text">"<Extra Probe>"</p>
<p><b>{localize_day(&session.day)}</b></p>
<p>{session.note}</p>
</div>
}
.into_any(),
DatedSession::Canceled { regular, day, .. } => view! {
.into_any(),
DatedSession::Canceled { regular, day, .. } => view! {
<div class="box cancel-background">
<p class="small-text">"<Entfall>"</p>
<p class="strikethrough bold">{localize_day(&day)}</p>
<p>{regular.note}</p>
</div>
}
.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! {
<span class="strikethrough">{localize_day(&day)}</span>
" "
{localize_day(&new_day)}
}
.into_any(),
};
.into_any(),
};
view! {
view! {
<div class="box change-background">
<p class="small-text">"<Änderung>"</p>
<p class="bold">{day}</p>
<p>{cause.note.or(regular.note)}</p>
</div>
}
.into_any()
}
.into_any()
}
}
}
async fn load_config<T>(name: &str) -> Result<Option<T>, 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<String>,
pub sessions: Vec<Session>,
pub motd: Option<String>,
pub sessions: Vec<Session>,
}
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(),
],
}
}
}

View File

@ -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<NaiveDate> 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<Day> for NaiveDate {
fn from(day: Day) -> Self {
day.date
}
}
fn from(day: Day) -> Self {
day.date
}
}

View File

@ -2,4 +2,3 @@ pub mod day;
pub mod session;
#[cfg(test)]
mod test_util;

View File

@ -1 +1 @@
fn main() {}
fn main() {}

View File

@ -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<Day, Except>,
pub rule: SessionRule,
pub note: Note,
#[serde(with = "serde_json_any_key::any_key_map")]
pub except: BTreeMap<Day, Except>,
}
impl From<SessionRule> 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<D, E>(mut self, day: D, except: E) -> Self
where
D: Into<Day>,
E: Into<Except>,
{
self.except.insert(day.into(), except.into());
self
}
pub fn except<D, E>(mut self, day: D, except: E) -> Self
where
D: Into<Day>,
E: Into<Except>,
{
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<D>(&self, current_date: D) -> Option<Day>
where
D: Into<Day>,
{
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<D>(&self, current_date: D) -> Option<Day>
where
D: Into<Day>,
{
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<Day> 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<Day>,
pub note: Note,
///the date when the alternation should show up in the calendar, or the original date
pub new_day: Option<Day>,
}
impl From<Day> 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;
}

View File

@ -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<Day, VecDeque<DatedSession>>,
sessions: BTreeMap<Day, VecDeque<DatedSession>>,
}
impl DatedSessionIter {
pub fn new<D, S>(start_date: D, sessions: S) -> DatedSessionIter
where
D: Into<Day>,
S: IntoIterator<Item=Session>,
{
let current_date = start_date.into();
pub fn new<D, S>(start_date: D, sessions: S) -> DatedSessionIter
where
D: Into<Day>,
S: IntoIterator<Item = Session>,
{
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<Self::Item> {
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<Self::Item> {
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<ExtraSession> 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()
})
);
}
}

View File

@ -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<WeekdayOfMonth> 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<D>(&self, start_date: D) -> SessionDayIter<&Self>
where
D: Into<Day>,
{
SessionDayIter {
rule: self,
start_date: Some(start_date.into()),
}
pub fn to_session_day_iter<D>(&self, start_date: D) -> SessionDayIter<&Self>
where
D: Into<Day>,
{
SessionDayIter {
rule: self,
start_date: Some(start_date.into()),
}
}
}
impl SessionRuleLike for SessionRule {
fn determine_next_date(&self, current_date: Day) -> Option<Day> {
match self {
SessionRule::WeekdayOfMonth(weekday_of_month) => {
weekday_of_month.determine_next_date(current_date)
}
}
fn determine_next_date(&self, current_date: Day) -> Option<Day> {
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<R> {
pub rule: R,
pub start_date: Option<Day>,
pub rule: R,
pub start_date: Option<Day>,
}
impl<R, S> Iterator for SessionDayIter<R>
where
R: Deref<Target=S>,
S: SessionRuleLike,
R: Deref<Target = S>,
S: SessionRuleLike,
{
type Item = Day;
type Item = Day;
fn next(&mut self) -> Option<Self::Item> {
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<Self::Item> {
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<R> SessionDayIter<R>
where
R: Deref<Target=SessionRule>,
R: Deref<Target = SessionRule>,
{
pub fn to_owned(&self) -> SessionDayIter<SessionRule> {
SessionDayIter {
rule: self.rule.clone(),
start_date: self.start_date,
}
pub fn to_owned(&self) -> SessionDayIter<SessionRule> {
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<Day> {
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<Day> {
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<Day>;
/// Determines the next session date in form of a [Day], possibly including the `current_date`.
fn determine_next_date(&self, current_date: Day) -> Option<Day>;
/// 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))
);
}
}

View File

@ -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()
}
date(day, month, year).into()
}