Replace from_dto fn with From trait impl
This commit is contained in:
@@ -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
|
||||
@@ -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<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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user