Move valid_residents to the scheduler impl and simplify it

This commit is contained in:
2026-01-14 21:15:57 +02:00
parent 2febcb7344
commit 1ce55675db
4 changed files with 63 additions and 118 deletions

View File

@@ -23,7 +23,7 @@ impl MonthlySchedule {
pub fn prefill(&mut self, config: &UserConfig) {
for r in &config.residents {
for s in &r.manual_shifts {
self.insert(*s, r);
self.insert(*s, &r.id);
}
}
}
@@ -32,10 +32,10 @@ impl MonthlySchedule {
self.0.get(slot)
}
pub fn current_workload(&self, resident: &Resident) -> usize {
pub fn current_workload(&self, resident_id: &str) -> usize {
self.0
.values()
.filter(|res_id| res_id == &&resident.id)
.filter(|res_id| res_id == &resident_id)
.count()
}
@@ -92,8 +92,8 @@ impl MonthlySchedule {
true
}
pub fn insert(&mut self, slot: Slot, resident: &Resident) {
self.0.insert(slot, resident.id.clone());
pub fn insert(&mut self, slot: Slot, resident_id: &str) {
self.0.insert(slot, resident_id.to_string());
}
pub fn remove(&mut self, slot: Slot) {
@@ -177,7 +177,7 @@ impl MonthlySchedule {
};
if let Some(resident) = config.residents.iter().find(|r| &r.id == res_id) {
let current_workload = self.current_workload(resident);
let current_workload = self.current_workload(&resident.id);
if let Some(&limit) = bounds.max_workloads.get(res_id) {
let mut workload_limit = limit;
@@ -293,7 +293,7 @@ impl MonthlySchedule {
residents.sort_by_key(|r| &r.name);
for res in residents {
let total = self.current_workload(res);
let total = self.current_workload(&res.id);
let o1 = self.count_shifts(&res.id, Some(ShiftType::OpenFirst));
let o2 = self.count_shifts(&res.id, Some(ShiftType::OpenSecond));
let cl = self.count_shifts(&res.id, Some(ShiftType::Closed));
@@ -380,10 +380,10 @@ mod tests {
let slot_1 = Slot::new(Day(1), ShiftPosition::First);
let slot_2 = Slot::new(Day(1), ShiftPosition::Second);
schedule.insert(slot_1, &resident);
schedule.insert(slot_1, &resident.id);
assert_eq!(schedule.get_resident_id(&slot_1), Some(&"1".to_string()));
assert_eq!(schedule.current_workload(&resident), 1);
assert_eq!(schedule.current_workload(&resident.id), 1);
assert_eq!(schedule.get_resident_id(&slot_2), None);
}
@@ -391,12 +391,12 @@ mod tests {
fn test_remove_resident(mut schedule: MonthlySchedule, resident: Resident) {
let slot_1 = Slot::new(Day(1), ShiftPosition::First);
schedule.insert(slot_1, &resident);
assert_eq!(schedule.current_workload(&resident), 1);
schedule.insert(slot_1, &resident.id);
assert_eq!(schedule.current_workload(&resident.id), 1);
schedule.remove(slot_1);
assert_eq!(schedule.get_resident_id(&slot_1), None);
assert_eq!(schedule.current_workload(&resident), 0);
assert_eq!(schedule.current_workload(&resident.id), 0);
}
#[rstest]
@@ -405,9 +405,9 @@ mod tests {
let slot_2 = Slot::new(Day(1), ShiftPosition::Second);
let slot_3 = Slot::new(Day(2), ShiftPosition::First);
schedule.insert(slot_1, &resident);
schedule.insert(slot_2, &resident);
schedule.insert(slot_3, &resident);
schedule.insert(slot_1, &resident.id);
schedule.insert(slot_2, &resident.id);
schedule.insert(slot_3, &resident.id);
assert!(!schedule.same_resident_in_consecutive_days(&slot_1));
assert!(!schedule.same_resident_in_consecutive_days(&slot_2));
@@ -422,8 +422,8 @@ mod tests {
let stefanos = &toxic_config.residents[0];
let iordanis = &toxic_config.residents[1];
schedule.insert(slot_1, stefanos);
schedule.insert(slot_2, iordanis);
schedule.insert(slot_1, &stefanos.id);
schedule.insert(slot_2, &iordanis.id);
assert!(schedule.has_toxic_pair(&slot_2, &toxic_config))
}
@@ -442,13 +442,13 @@ mod tests {
bounds.max_workloads.insert("1".to_string(), 1);
bounds.max_workloads.insert("2".to_string(), 2);
schedule.insert(slot_1, &stefanos);
schedule.insert(slot_1, &stefanos.id);
assert!(!schedule.is_workload_unbalanced(&slot_1, &config, &bounds));
schedule.insert(slot_2, &iordanis);
schedule.insert(slot_2, &iordanis.id);
assert!(!schedule.is_workload_unbalanced(&slot_2, &config, &bounds));
schedule.insert(slot_3, &stefanos);
schedule.insert(slot_3, &stefanos.id);
assert!(schedule.is_workload_unbalanced(&slot_3, &config, &bounds));
}
@@ -466,13 +466,13 @@ mod tests {
bounds.max_holiday_shifts.insert("1".to_string(), 1);
bounds.max_holiday_shifts.insert("2".to_string(), 1);
schedule.insert(slot_1, &stefanos);
schedule.insert(slot_1, &stefanos.id);
assert!(!schedule.is_holiday_workload_imbalanced(&slot_1, &config, &bounds));
schedule.insert(slot_2, &iordanis);
schedule.insert(slot_2, &iordanis.id);
assert!(!schedule.is_holiday_workload_imbalanced(&slot_2, &config, &bounds));
schedule.insert(slot_7, &stefanos);
schedule.insert(slot_7, &stefanos.id);
assert!(schedule.is_holiday_workload_imbalanced(&slot_7, &config, &bounds));
}
}