Change ResidentId(String) to ResidentId(u8), impl Copy for ShiftType, use u8 for all UserConfig params
This commit is contained in:
@@ -12,11 +12,8 @@ const YEAR: i32 = 2026;
|
||||
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 new(res_id_1: u8, res_id_2: u8) -> Self {
|
||||
Self((ResidentId(res_id_1), ResidentId(res_id_2)))
|
||||
}
|
||||
|
||||
pub fn matches(&self, other: &ToxicPair) -> bool {
|
||||
@@ -35,18 +32,18 @@ impl From<(ResidentId, ResidentId)> for ToxicPair {
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct UserConfigDTO {
|
||||
month: usize,
|
||||
month: u8,
|
||||
year: i32,
|
||||
holidays: Vec<usize>,
|
||||
holidays: Vec<u8>,
|
||||
residents: Vec<ResidentDTO>,
|
||||
toxic_pairs: Vec<(String, String)>,
|
||||
toxic_pairs: Vec<(u8, u8)>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UserConfig {
|
||||
pub month: Month,
|
||||
pub year: i32,
|
||||
pub holidays: Vec<usize>,
|
||||
pub holidays: Vec<u8>,
|
||||
pub residents: Vec<Resident>,
|
||||
pub toxic_pairs: Vec<ToxicPair>,
|
||||
|
||||
@@ -80,7 +77,7 @@ impl UserConfig {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_holidays(mut self, holidays: Vec<usize>) -> Self {
|
||||
pub fn with_holidays(mut self, holidays: Vec<u8>) -> Self {
|
||||
self.holidays = holidays;
|
||||
self.total_holiday_slots = self.total_holiday_slots();
|
||||
self
|
||||
@@ -110,7 +107,7 @@ impl UserConfig {
|
||||
pub fn is_holiday_or_weekend_slot(&self, day: u8) -> bool {
|
||||
let day = Day(day);
|
||||
day.is_weekend(self.month.number_from_month(), self.year)
|
||||
|| self.holidays.contains(&(day.0 as usize))
|
||||
|| self.holidays.contains(&(day.0))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,7 +141,7 @@ impl Default for UserConfig {
|
||||
|
||||
impl From<UserConfigDTO> for UserConfig {
|
||||
fn from(value: UserConfigDTO) -> Self {
|
||||
let month = Month::try_from(value.month as u8).unwrap();
|
||||
let month = Month::try_from(value.month).unwrap();
|
||||
|
||||
let total_days = month.num_days(YEAR).unwrap();
|
||||
|
||||
@@ -155,7 +152,7 @@ impl From<UserConfigDTO> for UserConfig {
|
||||
let total_holiday_slots = (1..=total_days)
|
||||
.filter(|&d| {
|
||||
Day(d).is_weekend(month.number_from_month(), value.year)
|
||||
|| value.holidays.contains(&(d as usize))
|
||||
|| value.holidays.contains(&d)
|
||||
})
|
||||
.map(|d| if Day(d).is_open_shift() { 2 } else { 1 })
|
||||
.sum();
|
||||
@@ -168,7 +165,7 @@ impl From<UserConfigDTO> for UserConfig {
|
||||
toxic_pairs: value
|
||||
.toxic_pairs
|
||||
.into_iter()
|
||||
.map(|p| ToxicPair::new(&p.0, &p.1))
|
||||
.map(|p| ToxicPair::new(p.0, p.1))
|
||||
.collect(),
|
||||
total_days,
|
||||
total_slots,
|
||||
|
||||
Reference in New Issue
Block a user