Replace from_dto fn with From trait impl

This commit is contained in:
2026-01-13 23:11:44 +02:00
parent 4b49b4b54e
commit 8865f0b95a
3 changed files with 30 additions and 26 deletions

View File

@@ -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<usize>) -> Self {
self.holidays = holidays;
self
@@ -323,6 +309,22 @@ impl UserConfig {
}
}
impl From<UserConfigDTO> 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;

View File

@@ -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);
}

View File

@@ -70,17 +70,19 @@ impl Resident {
self.manual_shifts = manual_shifts;
self
}
}
pub fn from_dto(dto: ResidentDTO) -> Self {
impl From<ResidentDTO> 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,
}
}
}