Introduce more styles and dates sessions

This commit is contained in:
Leonard Steppy 2025-02-15 16:14:23 +01:00
parent da42de5c92
commit 9439593409
3 changed files with 104 additions and 8 deletions

View File

@ -1,9 +1,102 @@
use crate::session_date_calculator::Day;
use chrono::Weekday;
use crate::session_date_calculator::{Day, NthWeekday};
use chrono::{NaiveDate, Weekday};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub mod session_date_calculator;
pub mod webpage;
#[derive(Debug, Clone)]
pub struct DatedSession {
pub day: Day,
pub session: Session,
pub applying_exception: Option<Exception>,
}
impl Noted for DatedSession {
fn note(&self) -> Option<&String> {
match &self.applying_exception {
Some(e) => e.note(),
None => self.session.note(),
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum Session {
Regular {
rule: NthWeekday,
note: Option<String>,
except: HashMap<NaiveDate, Exception>,
},
Extra {
date: NaiveDate,
note: Option<String>,
},
}
impl Session {
pub fn into_dated(self, day: Day) -> Result<DatedSession, Self> {
if !self.is_applicable_to(&day) {
return Err(self);
}
let dated_session = DatedSession {
applying_exception: match self {
Session::Regular { ref except, .. } => except.get(&day.date).cloned(),
_ => None,
},
session: self,
day,
};
Ok(dated_session)
}
pub fn is_applicable_to(&self, day: &Day) -> bool {
match *self {
Session::Regular { rule, .. } => rule.matches(day),
Session::Extra { date, .. } => day.date == date,
}
}
}
impl Noted for Session {
fn note(&self) -> Option<&String> {
match self {
Session::Regular { note, .. } => note,
Session::Extra { note, .. } => note,
}
.as_ref()
}
}
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum Exception {
Cancel {
note: Option<String>,
},
Alter {
#[serde(default)]
date: Option<NaiveDate>,
#[serde(default)]
note: Option<String>,
},
}
impl Noted for Exception {
fn note(&self) -> Option<&String> {
match self {
Exception::Cancel { note, .. } => note,
Exception::Alter { note, .. } => note,
}
.as_ref()
}
}
pub trait Noted {
fn note(&self) -> Option<&String>;
}
pub fn localize_day(day: &Day) -> String {
format!(
"{}, {}",
@ -18,4 +111,4 @@ pub fn localize_day(day: &Day) -> String {
},
day.date.format("%d.%m.%Y")
)
}
}

View File

@ -5,7 +5,7 @@ use std::fmt::{Display, Formatter};
use std::num::ParseIntError;
use std::str::FromStr;
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct NthWeekday {
pub n: u8,
pub weekday: Weekday,

View File

@ -5,7 +5,7 @@
--white: white;
--black: black;
--red: red;
--yellow: #f0f00f;
--yellow: #bbbb11;
--green: #1fd51f;
}
ul {
@ -70,6 +70,9 @@ html, body {
.highlight-background {
background-color: var(--green);
}
.red-text {
color: var(--red);
}
.cancel-background {
background-color: var(--red);
}
.change-background {
background-color: var(--yellow);
}