diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index 06a1332..e5c4ca1 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -50,20 +50,6 @@ impl UserConfig { } } - pub fn from_dto(dto: UserConfigDTO) -> Self { - Self { - month: Month::try_from(dto.month as u8).unwrap(), - year: dto.year, - holidays: dto.holidays, - residents: dto.residents.into_iter().map(Resident::from_dto).collect(), - toxic_pairs: dto.toxic_pairs, - workload_limits: HashMap::new(), - holiday_limits: HashMap::new(), - shift_type_limits: HashMap::new(), - shift_type_threshold: HashMap::new(), - } - } - pub fn with_holidays(mut self, holidays: Vec) -> Self { self.holidays = holidays; self @@ -184,7 +170,7 @@ impl UserConfig { } } - /// shift type count fairness + /// shift type count fairness pub fn calculate_shift_type_fairness(&mut self) { let mut global_supply = self.get_initial_supply(); let mut local_limits = HashMap::new(); @@ -323,6 +309,22 @@ impl UserConfig { } } +impl From for UserConfig { + fn from(value: UserConfigDTO) -> Self { + Self { + month: Month::try_from(value.month as u8).unwrap(), + year: value.year, + holidays: value.holidays, + residents: value.residents.into_iter().map(Resident::from).collect(), + toxic_pairs: value.toxic_pairs, + workload_limits: HashMap::new(), + holiday_limits: HashMap::new(), + shift_type_limits: HashMap::new(), + shift_type_threshold: HashMap::new(), + } + } +} + #[cfg(test)] mod tests { use std::collections::HashMap; diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index b12839a..6a645e7 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -28,7 +28,7 @@ struct AppState { #[tauri::command] fn generate(config: UserConfigDTO, state: tauri::State<'_, AppState>) -> MonthlySchedule { info!("{:?}", config); - let mut config = UserConfig::from_dto(config); + let mut config = UserConfig::from(config); config.calculate_workload_limits(); config.calculate_holiday_limits(); config.calculate_shift_type_fairness(); @@ -48,7 +48,7 @@ fn generate(config: UserConfigDTO, state: tauri::State<'_, AppState>) -> Monthly /// export into docx #[tauri::command] fn export(config: UserConfigDTO, state: tauri::State<'_, AppState>) { - let config = UserConfig::from_dto(config); + let config = UserConfig::from(config); let schedule = state.schedule.lock().unwrap(); schedule.export(FileType::Docx, &config); } diff --git a/src-tauri/src/resident.rs b/src-tauri/src/resident.rs index 34fc8f1..3d8e62b 100644 --- a/src-tauri/src/resident.rs +++ b/src-tauri/src/resident.rs @@ -70,17 +70,19 @@ impl Resident { self.manual_shifts = manual_shifts; self } +} - pub fn from_dto(dto: ResidentDTO) -> Self { +impl From for Resident { + fn from(value: ResidentDTO) -> Self { Self { - id: dto.id, - name: dto.name, - negative_shifts: dto + id: value.id, + name: value.name, + negative_shifts: value .negative_shifts .into_iter() .map(|d| Day(d as u8)) .collect(), - manual_shifts: dto + manual_shifts: value .manual_shifts .into_iter() .map(|s| { @@ -91,9 +93,9 @@ impl Resident { } }) .collect(), - max_shifts: dto.max_shifts, - allowed_types: dto.allowed_types, - reduced_load: dto.reduced_load, + max_shifts: value.max_shifts, + allowed_types: value.allowed_types, + reduced_load: value.reduced_load, } } -} +} \ No newline at end of file