backend/model/dto/
plants_impl.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//! Contains the implementation of [`PlantsSummaryDto`].

use crate::model::{entity::Plants, r#enum::life_cycle::LifeCycle};

use super::PlantsSummaryDto;

impl From<Plants> for PlantsSummaryDto {
    fn from(plants: Plants) -> Self {
        let life_cycle: Vec<LifeCycle> = plants
            .life_cycle
            .map_or(vec![], |v| v.into_iter().flatten().collect());

        // Handle Option<Vec<Option<T>>> conversions
        let edible_parts = plants.edible_parts.map_or(vec![], |v| v);
        let sowing_outdoors = plants.sowing_outdoors.map_or(vec![], |v| v);
        let harvest_time = plants.harvest_time.map_or(vec![], |v| v);

        Self {
            id: plants.id,
            unique_name: plants.unique_name,
            common_name_en: plants.common_name_en,
            common_name_de: plants.common_name_de,
            spread: plants.spread,
            life_cycle,
            functions: plants.functions,
            edible: plants.edible,
            edible_parts,
            warning: plants.warning,
            sowing_outdoors,
            harvest_time,
        }
    }
}

impl<T> From<(T, Plants)> for PlantsSummaryDto {
    fn from((_, plants): (T, Plants)) -> Self {
        Self::from(plants)
    }
}