1
use crate::convert::{convert_string, ToModel};
2
use crate::model::param::{
3
    AlternateRepresentationParam, CalendarUserTypeParam, CommonNameParam, DelegatedFromParam,
4
    DelegatedToParam, DirectoryEntryReferenceParam, EncodingParam, FormatTypeParam,
5
    FreeBusyTimeTypeParam, LanguageParam, MembersParam, Param as ModelParam,
6
    ParticipationStatusParam, RangeParam, RelationshipTypeParam, RoleParam, RsvpParam, SentByParam,
7
    TimeZoneIdParam, TriggerRelationshipParam, ValueTypeParam,
8
};
9
use crate::parser::types::ParamValue as ParserParam;
10

            
11
impl ToModel for ParserParam<'_> {
12
    type Model = ModelParam;
13

            
14
2720
    fn to_model(&self) -> anyhow::Result<Self::Model> {
15
2720
        Ok(match self {
16
120
            ParserParam::AltRep { uri } => ModelParam::AltRep(AlternateRepresentationParam {
17
120
                uri: convert_string(uri),
18
120
            }),
19
44
            ParserParam::CommonName { name } => ModelParam::CommonName(CommonNameParam {
20
44
                name: name.to_string(),
21
44
            }),
22
26
            ParserParam::CalendarUserType { cu_type } => {
23
26
                ModelParam::CalendarUserType(CalendarUserTypeParam {
24
26
                    cu_type: cu_type.clone(),
25
26
                })
26
            }
27
20
            ParserParam::DelegatedFrom { delegators } => {
28
20
                ModelParam::DelegatedFrom(DelegatedFromParam {
29
24
                    delegators: delegators.iter().map(|d| convert_string(d)).collect(),
30
20
                })
31
            }
32
20
            ParserParam::DelegatedTo { delegates } => ModelParam::DelegatedTo(DelegatedToParam {
33
24
                delegates: delegates.iter().map(|d| convert_string(d)).collect(),
34
20
            }),
35
44
            ParserParam::DirectoryEntryReference { uri } => {
36
44
                ModelParam::DirectoryEntryReference(DirectoryEntryReferenceParam {
37
44
                    uri: String::from_utf8_lossy(uri).to_string(),
38
44
                })
39
            }
40
16
            ParserParam::Encoding { encoding } => ModelParam::Encoding(EncodingParam {
41
16
                encoding: encoding.clone(),
42
16
            }),
43
            ParserParam::FormatType {
44
24
                type_name,
45
24
                sub_type_name,
46
24
            } => ModelParam::FormatType(FormatTypeParam {
47
24
                type_name: type_name.to_string(),
48
24
                sub_type_name: sub_type_name.to_string(),
49
24
            }),
50
8
            ParserParam::FreeBusyTimeType { fb_type } => {
51
8
                ModelParam::FreeBusyTimeType(FreeBusyTimeTypeParam {
52
8
                    fb_type: fb_type.clone(),
53
8
                })
54
            }
55
212
            ParserParam::Language { language } => ModelParam::Language(LanguageParam {
56
212
                language: language.clone(),
57
212
            }),
58
20
            ParserParam::Members { members } => ModelParam::Members(MembersParam {
59
24
                members: members.iter().map(|m| convert_string(m)).collect(),
60
20
            }),
61
24
            ParserParam::ParticipationStatus { status } => {
62
24
                ModelParam::ParticipationStatus(ParticipationStatusParam {
63
24
                    status: status.clone(),
64
24
                })
65
            }
66
20
            ParserParam::Range { range } => ModelParam::Range(RangeParam {
67
20
                range: range.clone(),
68
20
            }),
69
10
            ParserParam::Related { related } => {
70
10
                ModelParam::TriggerRelationship(TriggerRelationshipParam {
71
10
                    trigger_relationship: related.clone(),
72
10
                })
73
            }
74
20
            ParserParam::RelationshipType { relationship } => {
75
20
                ModelParam::RelationshipType(RelationshipTypeParam {
76
20
                    relationship: relationship.clone(),
77
20
                })
78
            }
79
26
            ParserParam::Role { role } => ModelParam::Role(RoleParam { role: role.clone() }),
80
26
            ParserParam::Rsvp { rsvp } => ModelParam::Rsvp(RsvpParam { rsvp: *rsvp }),
81
46
            ParserParam::SentBy { address } => ModelParam::SentBy(SentByParam {
82
46
                address: convert_string(address),
83
46
            }),
84
112
            ParserParam::TimeZoneId { tz_id, unique } => ModelParam::TimeZoneId(TimeZoneIdParam {
85
112
                tz_id: tz_id.to_string(),
86
112
                unique: *unique,
87
112
            }),
88
154
            ParserParam::ValueType { value } => ModelParam::ValueType(ValueTypeParam {
89
154
                value: value.clone(),
90
154
            }),
91
1728
            ParserParam::Other { name, value } => ModelParam::Other {
92
1728
                name: convert_string(name),
93
1728
                value: convert_string(value),
94
1728
            },
95
            ParserParam::Others { name, values } => ModelParam::Others {
96
                name: convert_string(name),
97
                values: values.iter().map(|v| convert_string(v)).collect(),
98
            },
99
        })
100
2720
    }
101
}