backend/model/dto/
plants_impl.rs

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