Improve error handling, logging

This commit is contained in:
2026-01-18 00:02:11 +02:00
parent 125ddc3117
commit 33de9720bf
11 changed files with 211 additions and 109 deletions

View File

@@ -1,3 +1,4 @@
use anyhow::Context;
use chrono::Month;
use serde::{Deserialize, Serialize};
@@ -6,6 +7,7 @@ use crate::{
slot::Day,
};
const MONTH: u8 = 2;
const YEAR: i32 = 2026;
#[derive(Debug, Clone)]
@@ -53,30 +55,6 @@ pub struct UserConfig {
}
impl UserConfig {
pub fn new(month: u8) -> Self {
let month = Month::try_from(month).unwrap();
let total_days = month.num_days(YEAR).unwrap();
let total_slots = (1..=total_days)
.map(|d| if Day(d).is_open_shift() { 2 } else { 1 })
.sum();
let total_holiday_slots = (1..=total_days)
.filter(|&d| Day(d).is_weekend(month.number_from_month(), YEAR))
.map(|d| if Day(d).is_open_shift() { 2 } else { 1 })
.sum();
Self {
month,
year: YEAR,
holidays: vec![],
residents: vec![],
toxic_pairs: vec![],
total_days,
total_slots,
total_holiday_slots,
}
}
pub fn with_holidays(mut self, holidays: Vec<u8>) -> Self {
self.holidays = holidays;
self.total_holiday_slots = self.total_holiday_slots();
@@ -113,7 +91,7 @@ impl UserConfig {
impl Default for UserConfig {
fn default() -> Self {
let month = Month::try_from(2).unwrap();
let month = Month::try_from(MONTH).unwrap();
let total_days = month.num_days(YEAR).unwrap();
@@ -139,11 +117,13 @@ impl Default for UserConfig {
}
}
impl From<UserConfigDTO> for UserConfig {
fn from(value: UserConfigDTO) -> Self {
let month = Month::try_from(value.month).unwrap();
impl TryFrom<UserConfigDTO> for UserConfig {
type Error = anyhow::Error;
let total_days = month.num_days(YEAR).unwrap();
fn try_from(value: UserConfigDTO) -> Result<Self, Self::Error> {
let month = Month::try_from(value.month)?;
let total_days = month.num_days(value.year).context("Failed to parse")?;
let total_slots = (1..=total_days)
.map(|d| if Day(d).is_open_shift() { 2 } else { 1 })
@@ -157,7 +137,7 @@ impl From<UserConfigDTO> for UserConfig {
.map(|d| if Day(d).is_open_shift() { 2 } else { 1 })
.sum();
Self {
Ok(Self {
month,
year: value.year,
holidays: value.holidays,
@@ -170,6 +150,6 @@ impl From<UserConfigDTO> for UserConfig {
total_days,
total_slots,
total_holiday_slots,
}
})
}
}