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 {
|
pub fn with_holidays(mut self, holidays: Vec<usize>) -> Self {
|
||||||
self.holidays = holidays;
|
self.holidays = holidays;
|
||||||
self
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ struct AppState {
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
fn generate(config: UserConfigDTO, state: tauri::State<'_, AppState>) -> MonthlySchedule {
|
fn generate(config: UserConfigDTO, state: tauri::State<'_, AppState>) -> MonthlySchedule {
|
||||||
info!("{:?}", config);
|
info!("{:?}", config);
|
||||||
let mut config = UserConfig::from_dto(config);
|
let mut config = UserConfig::from(config);
|
||||||
config.calculate_workload_limits();
|
config.calculate_workload_limits();
|
||||||
config.calculate_holiday_limits();
|
config.calculate_holiday_limits();
|
||||||
config.calculate_shift_type_fairness();
|
config.calculate_shift_type_fairness();
|
||||||
@@ -48,7 +48,7 @@ fn generate(config: UserConfigDTO, state: tauri::State<'_, AppState>) -> Monthly
|
|||||||
/// export into docx
|
/// export into docx
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
fn export(config: UserConfigDTO, state: tauri::State<'_, AppState>) {
|
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();
|
let schedule = state.schedule.lock().unwrap();
|
||||||
schedule.export(FileType::Docx, &config);
|
schedule.export(FileType::Docx, &config);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,17 +70,19 @@ impl Resident {
|
|||||||
self.manual_shifts = manual_shifts;
|
self.manual_shifts = manual_shifts;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from_dto(dto: ResidentDTO) -> Self {
|
impl From<ResidentDTO> for Resident {
|
||||||
|
fn from(value: ResidentDTO) -> Self {
|
||||||
Self {
|
Self {
|
||||||
id: dto.id,
|
id: value.id,
|
||||||
name: dto.name,
|
name: value.name,
|
||||||
negative_shifts: dto
|
negative_shifts: value
|
||||||
.negative_shifts
|
.negative_shifts
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|d| Day(d as u8))
|
.map(|d| Day(d as u8))
|
||||||
.collect(),
|
.collect(),
|
||||||
manual_shifts: dto
|
manual_shifts: value
|
||||||
.manual_shifts
|
.manual_shifts
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|s| {
|
.map(|s| {
|
||||||
@@ -91,9 +93,9 @@ impl Resident {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
max_shifts: dto.max_shifts,
|
max_shifts: value.max_shifts,
|
||||||
allowed_types: dto.allowed_types,
|
allowed_types: value.allowed_types,
|
||||||
reduced_load: dto.reduced_load,
|
reduced_load: value.reduced_load,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user