backend/model/dto/
plants_impl.rs

1//! Contains the implementation of [`PlantsSummaryDto`].
2
3use crate::model::{entity::Plants, r#enum::life_cycle::LifeCycle};
4
5use super::PlantsSummaryDto;
6
7impl From<Plants> for PlantsSummaryDto {
8    fn from(plants: Plants) -> Self {
9        let life_cycle: Vec<LifeCycle> = plants
10            .life_cycle
11            .map_or(vec![], |v| v.into_iter().flatten().collect());
12
13        // Handle Option<Vec<Option<T>>> conversions
14        let edible_parts = plants.edible_parts.map_or(vec![], |v| v);
15        let sowing_outdoors = plants.sowing_outdoors.map_or(vec![], |v| v);
16        let harvest_time = plants.harvest_time.map_or(vec![], |v| v);
17
18        Self {
19            id: plants.id,
20            unique_name: plants.unique_name,
21            common_name_en: plants.common_name_en,
22            common_name_de: plants.common_name_de,
23            spread: plants.spread,
24            life_cycle,
25            herbaceous_or_woody: plants.herbaceous_or_woody,
26            has_drought_tolerance: plants.has_drought_tolerance,
27            hardiness_zone: plants.hardiness_zone,
28            functions: plants.functions,
29            edible: plants.edible,
30            edible_parts,
31            warning: plants.warning,
32            sowing_outdoors,
33            harvest_time,
34            icon_path: plants.icon_path,
35        }
36    }
37}
38
39impl<T> From<(T, Plants)> for PlantsSummaryDto {
40    fn from((_, plants): (T, Plants)) -> Self {
41        Self::from(plants)
42    }
43}