Improve type system with ResidentId and ToxicPair structs
This commit is contained in:
@@ -2,12 +2,37 @@ use chrono::Month;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
resident::{Resident, ResidentDTO},
|
||||
resident::{Resident, ResidentDTO, ResidentId},
|
||||
slot::Day,
|
||||
};
|
||||
|
||||
const YEAR: i32 = 2026;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ToxicPair((ResidentId, ResidentId));
|
||||
|
||||
impl ToxicPair {
|
||||
pub fn new(res_id_1: &str, res_id_2: &str) -> Self {
|
||||
Self((
|
||||
ResidentId(res_id_1.to_string()),
|
||||
ResidentId(res_id_2.to_string()),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn matches(&self, other: &ToxicPair) -> bool {
|
||||
let p1 = &self.0;
|
||||
let p2 = &other.0;
|
||||
|
||||
(p1.0 == p2.0 && p1.1 == p2.1) || (p1.0 == p2.1 && p1.1 == p2.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(ResidentId, ResidentId)> for ToxicPair {
|
||||
fn from(value: (ResidentId, ResidentId)) -> Self {
|
||||
Self((value.0, value.1))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct UserConfigDTO {
|
||||
month: usize,
|
||||
@@ -23,7 +48,7 @@ pub struct UserConfig {
|
||||
pub year: i32,
|
||||
pub holidays: Vec<usize>,
|
||||
pub residents: Vec<Resident>,
|
||||
pub toxic_pairs: Vec<(String, String)>,
|
||||
pub toxic_pairs: Vec<ToxicPair>,
|
||||
}
|
||||
|
||||
impl UserConfig {
|
||||
@@ -51,7 +76,7 @@ impl UserConfig {
|
||||
self.residents.push(resident);
|
||||
}
|
||||
|
||||
pub fn with_toxic_pairs(mut self, toxic_pairs: Vec<(String, String)>) -> Self {
|
||||
pub fn with_toxic_pairs(mut self, toxic_pairs: Vec<ToxicPair>) -> Self {
|
||||
self.toxic_pairs = toxic_pairs;
|
||||
self
|
||||
}
|
||||
@@ -99,7 +124,11 @@ impl From<UserConfigDTO> for UserConfig {
|
||||
year: value.year,
|
||||
holidays: value.holidays,
|
||||
residents: value.residents.into_iter().map(Resident::from).collect(),
|
||||
toxic_pairs: value.toxic_pairs,
|
||||
toxic_pairs: value
|
||||
.toxic_pairs
|
||||
.into_iter()
|
||||
.map(|p| ToxicPair::new(&p.0, &p.1))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user