aboutsummaryrefslogtreecommitdiff
path: root/aero-ical/src/prune.rs
blob: 3eb50ca4347a1ddc3c228fc6d0e2115b25fdef54 (plain) (blame)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use aero_dav::caltypes as cal;
use icalendar::parser::{Component, Property};

pub fn component<'a>(src: &'a Component<'a>, prune: &cal::Comp) -> Option<Component<'a>> {
    if src.name.as_str() != prune.name.as_str() {
        return None;
    }

    let name = src.name.clone();

    let properties = match &prune.prop_kind {
        Some(cal::PropKind::AllProp) | None => src.properties.clone(),
        Some(cal::PropKind::Prop(l)) => src
            .properties
            .iter()
            .filter_map(|prop| {
                let sel_filt = match l
                    .iter()
                    .find(|filt| filt.name.0.as_str() == prop.name.as_str())
                {
                    Some(v) => v,
                    None => return None,
                };

                match sel_filt.novalue {
                    None | Some(false) => Some(prop.clone()),
                    Some(true) => Some(Property {
                        name: prop.name.clone(),
                        params: prop.params.clone(),
                        val: "".into(),
                    }),
                }
            })
            .collect::<Vec<_>>(),
    };

    let components = match &prune.comp_kind {
        Some(cal::CompKind::AllComp) | None => src.components.clone(),
        Some(cal::CompKind::Comp(many_inner_prune)) => src
            .components
            .iter()
            .filter_map(|src_component| {
                many_inner_prune
                    .iter()
                    .find_map(|inner_prune| component(src_component, inner_prune))
            })
            .collect::<Vec<_>>(),
    };

    Some(Component {
        name,
        properties,
        components,
    })
}