1
use crate::serialize::WriteModel;
2
use std::io::Write;
3

            
4
impl WriteModel for crate::model::component::CalendarComponent {
5
102
    fn write_model<W: Write>(&self, writer: &mut W) -> anyhow::Result<()> {
6
102
        use crate::model::component::CalendarComponent;
7
102

            
8
102
        match self {
9
18
            CalendarComponent::Event(component) => {
10
18
                writer.write_all(b"BEGIN:VEVENT")?;
11
140
                for property in &component.properties {
12
122
                    writer.write_all(b"\r\n")?;
13
122
                    property.write_model(writer)?;
14
                }
15
30
                for alarm in &component.alarms {
16
12
                    writer.write_all(b"\r\n")?;
17
12
                    alarm.write_model(writer)?;
18
                }
19
18
                writer.write_all(b"\r\nEND:VEVENT")?;
20
            }
21
6
            CalendarComponent::ToDo(component) => {
22
6
                writer.write_all(b"BEGIN:VTODO")?;
23
100
                for property in &component.properties {
24
94
                    writer.write_all(b"\r\n")?;
25
94
                    property.write_model(writer)?;
26
                }
27
8
                for alarm in &component.alarms {
28
2
                    writer.write_all(b"\r\n")?;
29
2
                    alarm.write_model(writer)?;
30
                }
31
6
                writer.write_all(b"\r\nEND:VTODO")?;
32
            }
33
4
            CalendarComponent::Journal(component) => {
34
4
                writer.write_all(b"BEGIN:VJOURNAL")?;
35
64
                for property in &component.properties {
36
60
                    writer.write_all(b"\r\n")?;
37
60
                    property.write_model(writer)?;
38
                }
39
4
                writer.write_all(b"\r\nEND:VJOURNAL")?;
40
            }
41
8
            CalendarComponent::FreeBusy(component) => {
42
8
                writer.write_all(b"BEGIN:VFREEBUSY")?;
43
78
                for property in &component.properties {
44
70
                    writer.write_all(b"\r\n")?;
45
70
                    property.write_model(writer)?;
46
                }
47
8
                writer.write_all(b"\r\nEND:VFREEBUSY")?;
48
            }
49
12
            CalendarComponent::TimeZone(component) => {
50
12
                writer.write_all(b"BEGIN:VTIMEZONE")?;
51
44
                for property in &component.properties {
52
32
                    writer.write_all(b"\r\n")?;
53
32
                    property.write_model(writer)?;
54
                }
55
48
                for component in &component.components {
56
36
                    writer.write_all(b"\r\n")?;
57
36
                    component.write_model(writer)?;
58
                }
59
12
                writer.write_all(b"\r\nEND:VTIMEZONE")?;
60
            }
61
14
            CalendarComponent::Standard(component) => {
62
14
                writer.write_all(b"BEGIN:STANDARD")?;
63
90
                for property in &component.properties {
64
76
                    writer.write_all(b"\r\n")?;
65
76
                    property.write_model(writer)?;
66
                }
67
14
                writer.write_all(b"\r\nEND:STANDARD")?;
68
            }
69
22
            CalendarComponent::Daylight(component) => {
70
22
                writer.write_all(b"BEGIN:DAYLIGHT")?;
71
138
                for property in &component.properties {
72
116
                    writer.write_all(b"\r\n")?;
73
116
                    property.write_model(writer)?;
74
                }
75
22
                writer.write_all(b"\r\nEND:DAYLIGHT")?;
76
            }
77
14
            CalendarComponent::Alarm(component) => {
78
14
                writer.write_all(b"BEGIN:VALARM")?;
79
108
                for property in &component.properties {
80
94
                    writer.write_all(b"\r\n")?;
81
94
                    property.write_model(writer)?;
82
                }
83
14
                writer.write_all(b"\r\nEND:VALARM")?;
84
            }
85
2
            CalendarComponent::IanaComponent(component) => {
86
2
                writer.write_all(b"BEGIN:")?;
87
2
                writer.write_all(component.name.as_bytes())?;
88
6
                for property in &component.properties {
89
4
                    writer.write_all(b"\r\n")?;
90
4
                    property.write_model(writer)?;
91
                }
92
2
                writer.write_all(b"\r\nEND:")?;
93
2
                writer.write_all(component.name.as_bytes())?;
94
            }
95
2
            CalendarComponent::XComponent(component) => {
96
2
                writer.write_all(b"BEGIN:")?;
97
2
                writer.write_all(component.name.as_bytes())?;
98
6
                for property in &component.properties {
99
4
                    writer.write_all(b"\r\n")?;
100
4
                    property.write_model(writer)?;
101
                }
102
2
                writer.write_all(b"\r\nEND:")?;
103
2
                writer.write_all(component.name.as_bytes())?;
104
            }
105
        }
106

            
107
102
        Ok(())
108
102
    }
109
}