1
use crate::model::param::{
2
    AlternateRepresentationParam, CalendarUserTypeParam, CommonNameParam, DelegatedFromParam,
3
    DelegatedToParam, DirectoryEntryReferenceParam, EncodingParam, FormatTypeParam,
4
    FreeBusyTimeTypeParam, LanguageParam, MembersParam, ParticipationStatusParam, RangeParam,
5
    RelationshipTypeParam, RoleParam, RsvpParam, SentByParam, TimeZoneIdParam,
6
    TriggerRelationshipParam, ValueTypeParam,
7
};
8
use crate::serialize::WriteModel;
9
use std::io::Write;
10

            
11
impl WriteModel for crate::model::param::Param {
12
836
    fn write_model<W: Write>(&self, writer: &mut W) -> anyhow::Result<()> {
13
836
        use crate::model::param::Param;
14
836

            
15
836
        match self {
16
40
            Param::AltRep(AlternateRepresentationParam { uri }) => {
17
40
                write!(writer, "ALTREP=\"{}\"", uri)?;
18
            }
19
14
            Param::CommonName(CommonNameParam { name }) => {
20
14
                write!(writer, "CN={}", name)?;
21
            }
22
18
            Param::ValueType(ValueTypeParam { value }) => {
23
18
                write!(writer, "VALUE=")?;
24
18
                value.write_model(writer)?;
25
            }
26
30
            Param::TimeZoneId(TimeZoneIdParam { tz_id, unique }) => {
27
30
                writer.write_all(b"TZID=")?;
28
30
                if *unique {
29
30
                    writer.write_all(b"/")?;
30
                }
31
30
                writer.write_all(tz_id.as_bytes())?;
32
            }
33
70
            Param::Language(LanguageParam { language }) => {
34
70
                writer.write_all(b"LANGUAGE=")?;
35
70
                language.write_model(writer)?;
36
            }
37
14
            Param::DirectoryEntryReference(DirectoryEntryReferenceParam { uri }) => {
38
14
                write!(writer, "DIR=\"{}\"", uri)?;
39
            }
40
14
            Param::SentBy(SentByParam { address }) => {
41
14
                write!(writer, "SENT-BY=\"{}\"", address)?;
42
            }
43
6
            Param::Range(RangeParam { range }) => {
44
6
                write!(writer, "RANGE=")?;
45
6
                range.write_model(writer)?;
46
            }
47
            Param::FormatType(FormatTypeParam {
48
10
                type_name,
49
10
                sub_type_name,
50
10
            }) => {
51
10
                write!(writer, "FMTTYPE={}/{}", type_name, sub_type_name)?;
52
            }
53
2
            Param::Encoding(EncodingParam { encoding }) => {
54
2
                writer.write_all(b"ENCODING=")?;
55
2
                encoding.write_model(writer)?;
56
            }
57
6
            Param::CalendarUserType(CalendarUserTypeParam { cu_type }) => {
58
6
                writer.write_all(b"CUTYPE=")?;
59
6
                cu_type.write_model(writer)?;
60
            }
61
6
            Param::Members(MembersParam { members }) => {
62
6
                writer.write_all(b"MEMBER=")?;
63
6
                if let Some(member) = members.first() {
64
6
                    write!(writer, "\"{}\"", member)?;
65
                }
66
6
                for member in members.iter().skip(1) {
67
                    write!(writer, ",\"{}\"", member)?;
68
                }
69
            }
70
6
            Param::Role(RoleParam { role }) => {
71
6
                writer.write_all(b"ROLE=")?;
72
6
                role.write_model(writer)?;
73
            }
74
6
            Param::ParticipationStatus(ParticipationStatusParam { status }) => {
75
6
                writer.write_all(b"PARTSTAT=")?;
76
6
                status.write_model(writer)?;
77
            }
78
6
            Param::Rsvp(RsvpParam { rsvp }) => {
79
6
                writer.write_all(b"RSVP=")?;
80
6
                rsvp.write_model(writer)?;
81
            }
82
6
            Param::DelegatedTo(DelegatedToParam { delegates }) => {
83
6
                writer.write_all(b"DELEGATED-TO=")?;
84
6
                if let Some(delegate) = delegates.first() {
85
6
                    write!(writer, "\"{}\"", delegate)?;
86
                }
87
6
                for delegate in delegates.iter().skip(1) {
88
                    write!(writer, ",\"{}\"", delegate)?;
89
                }
90
            }
91
6
            Param::DelegatedFrom(DelegatedFromParam { delegators }) => {
92
6
                writer.write_all(b"DELEGATED-FROM=")?;
93
6
                if let Some(delegate) = delegators.first() {
94
6
                    write!(writer, "\"{}\"", delegate)?;
95
                }
96
6
                for delegate in delegators.iter().skip(1) {
97
                    write!(writer, ",\"{}\"", delegate)?;
98
                }
99
            }
100
6
            Param::RelationshipType(RelationshipTypeParam { relationship }) => {
101
6
                writer.write_all(b"RELTYPE=")?;
102
6
                relationship.write_model(writer)?;
103
            }
104
2
            Param::FreeBusyTimeType(FreeBusyTimeTypeParam { fb_type }) => {
105
2
                writer.write_all(b"FBTYPE=")?;
106
2
                fb_type.write_model(writer)?;
107
            }
108
            Param::TriggerRelationship(TriggerRelationshipParam {
109
4
                trigger_relationship: related,
110
4
            }) => {
111
4
                writer.write_all(b"RELATED=")?;
112
4
                related.write_model(writer)?;
113
            }
114
564
            Param::Other { name, value } => {
115
564
                write!(writer, "{}={}", name, value)?;
116
            }
117
            Param::Others { name, values } => {
118
                write!(writer, "{}=", name)?;
119
                if let Some(value) = values.first() {
120
                    write!(writer, "\"{}\"", value)?;
121
                }
122
                for value in values.iter().skip(1) {
123
                    write!(writer, ",\"{}\"", value)?;
124
                }
125
            }
126
        }
127

            
128
836
        Ok(())
129
836
    }
130
}