1use std::cell::RefCell;
3use std::io::BufRead;
4
5#[cfg(feature = "serde-derive")]
6extern crate serde;
7
8use crate::parser::Component;
10use crate::parser::ParserError;
11use crate::property::{Property, PropertyParser};
12
13#[derive(Debug, Clone, Default)]
14#[cfg_attr(feature = "serde-derive", derive(serde::Serialize, serde::Deserialize))]
15pub struct IcalCalendar {
17    pub properties: Vec<Property>,
18    pub events: Vec<IcalEvent>,
19    pub alarms: Vec<IcalAlarm>,
20    pub todos: Vec<IcalTodo>,
21    pub journals: Vec<IcalJournal>,
22    pub free_busys: Vec<IcalFreeBusy>,
23    pub timezones: Vec<IcalTimeZone>,
24}
25
26impl IcalCalendar {
27    pub fn new() -> IcalCalendar {
28        IcalCalendar {
29            properties: Vec::new(),
30            events: Vec::new(),
31            alarms: Vec::new(),
32            todos: Vec::new(),
33            journals: Vec::new(),
34            free_busys: Vec::new(),
35            timezones: Vec::new(),
36        }
37    }
38}
39
40impl Component for IcalCalendar {
41    fn add_property(&mut self, property: Property) {
42        self.properties.push(property);
43    }
44
45    fn add_sub_component<B: BufRead>(
46        &mut self,
47        value: &str,
48        line_parser: &RefCell<PropertyParser<B>>,
49    ) -> Result<(), ParserError> {
50        match value {
51            "VALARM" => {
52                let mut alarm = IcalAlarm::new();
53                alarm.parse(line_parser)?;
54                self.alarms.push(alarm);
55            }
56            "VEVENT" => {
57                let mut event = IcalEvent::new();
58                event.parse(line_parser)?;
59                self.events.push(event);
60            }
61            "VTODO" => {
62                let mut todo = IcalTodo::new();
63                todo.parse(line_parser)?;
64                self.todos.push(todo);
65            }
66            "VJOURNAL" => {
67                let mut journal = IcalJournal::new();
68                journal.parse(line_parser)?;
69                self.journals.push(journal);
70            }
71            "VFREEBUSY" => {
72                let mut free_busy = IcalFreeBusy::new();
73                free_busy.parse(line_parser)?;
74                self.free_busys.push(free_busy);
75            }
76            "VTIMEZONE" => {
77                let mut timezone = IcalTimeZone::new();
78                timezone.parse(line_parser)?;
79                self.timezones.push(timezone);
80            }
81            _ => return Err(ParserError::InvalidComponent.into()),
82        };
83
84        Ok(())
85    }
86}
87
88#[derive(Debug, Clone, Default)]
89#[cfg_attr(feature = "serde-derive", derive(serde::Serialize, serde::Deserialize))]
90pub struct IcalAlarm {
91    pub properties: Vec<Property>,
92}
93
94impl IcalAlarm {
95    pub fn new() -> IcalAlarm {
96        IcalAlarm {
97            properties: Vec::new(),
98        }
99    }
100}
101
102impl Component for IcalAlarm {
103    fn add_property(&mut self, property: Property) {
104        self.properties.push(property);
105    }
106
107    fn add_sub_component<B: BufRead>(
108        &mut self,
109        _: &str,
110        _: &RefCell<PropertyParser<B>>,
111    ) -> Result<(), ParserError> {
112        Err(ParserError::InvalidComponent.into())
113    }
114}
115
116#[derive(Debug, Clone, Default)]
117#[cfg_attr(feature = "serde-derive", derive(serde::Serialize, serde::Deserialize))]
118pub struct IcalEvent {
119    pub properties: Vec<Property>,
120    pub alarms: Vec<IcalAlarm>,
121}
122
123impl IcalEvent {
124    pub fn new() -> IcalEvent {
125        IcalEvent {
126            properties: Vec::new(),
127            alarms: Vec::new(),
128        }
129    }
130}
131
132impl Component for IcalEvent {
133    fn add_property(&mut self, property: Property) {
134        self.properties.push(property);
135    }
136
137    fn add_sub_component<B: BufRead>(
138        &mut self,
139        value: &str,
140        line_parser: &RefCell<PropertyParser<B>>,
141    ) -> Result<(), ParserError> {
142        match value {
143            "VALARM" => {
144                let mut alarm = IcalAlarm::new();
145                alarm.parse(line_parser)?;
146                self.alarms.push(alarm);
147            }
148            _ => return Err(ParserError::InvalidComponent.into()),
149        };
150
151        Ok(())
152    }
153}
154
155#[derive(Debug, Clone, Default)]
156#[cfg_attr(feature = "serde-derive", derive(serde::Serialize, serde::Deserialize))]
157pub struct IcalJournal {
158    pub properties: Vec<Property>,
159}
160
161impl IcalJournal {
162    pub fn new() -> IcalJournal {
163        IcalJournal {
164            properties: Vec::new(),
165        }
166    }
167}
168
169impl Component for IcalJournal {
170    fn add_property(&mut self, property: Property) {
171        self.properties.push(property);
172    }
173
174    fn add_sub_component<B: BufRead>(
175        &mut self,
176        _: &str,
177        _: &RefCell<PropertyParser<B>>,
178    ) -> Result<(), ParserError> {
179        Err(ParserError::InvalidComponent.into())
180    }
181}
182
183#[derive(Debug, Clone, Default)]
184#[cfg_attr(feature = "serde-derive", derive(serde::Serialize, serde::Deserialize))]
185pub struct IcalTodo {
186    pub properties: Vec<Property>,
187    pub alarms: Vec<IcalAlarm>,
188}
189
190impl IcalTodo {
191    pub fn new() -> IcalTodo {
192        IcalTodo {
193            properties: Vec::new(),
194            alarms: Vec::new(),
195        }
196    }
197}
198
199impl Component for IcalTodo {
200    fn add_property(&mut self, property: Property) {
201        self.properties.push(property);
202    }
203
204    fn add_sub_component<B: BufRead>(
205        &mut self,
206        value: &str,
207        line_parser: &RefCell<PropertyParser<B>>,
208    ) -> Result<(), ParserError> {
209        match value {
210            "VALARM" => {
211                let mut alarm = IcalAlarm::new();
212                alarm.parse(line_parser)?;
213                self.alarms.push(alarm);
214            }
215            _ => return Err(ParserError::InvalidComponent.into()),
216        };
217
218        Ok(())
219    }
220}
221
222#[derive(Debug, Clone, Default)]
223#[cfg_attr(feature = "serde-derive", derive(serde::Serialize, serde::Deserialize))]
224pub struct IcalTimeZone {
225    pub properties: Vec<Property>,
226    pub transitions: Vec<IcalTimeZoneTransition>,
227}
228
229impl IcalTimeZone {
230    pub fn new() -> IcalTimeZone {
231        IcalTimeZone {
232            properties: Vec::new(),
233            transitions: Vec::new(),
234        }
235    }
236}
237
238impl Component for IcalTimeZone {
239    fn add_property(&mut self, property: Property) {
240        self.properties.push(property);
241    }
242
243    fn add_sub_component<B: BufRead>(
244        &mut self,
245        value: &str,
246        line_parser: &RefCell<PropertyParser<B>>,
247    ) -> Result<(), ParserError> {
248        match value {
249            "STANDARD" | "DAYLIGHT" => {
250                let mut transition = IcalTimeZoneTransition::new();
251                transition.parse(line_parser)?;
252                self.transitions.push(transition);
253            }
254            _ => return Err(ParserError::InvalidComponent.into()),
255        };
256
257        Ok(())
258    }
259}
260
261#[derive(Debug, Clone, Default)]
262#[cfg_attr(feature = "serde-derive", derive(serde::Serialize, serde::Deserialize))]
263pub struct IcalTimeZoneTransition {
264    pub properties: Vec<Property>,
265}
266
267impl IcalTimeZoneTransition {
268    pub fn new() -> IcalTimeZoneTransition {
269        IcalTimeZoneTransition {
270            properties: Vec::new(),
271        }
272    }
273}
274
275impl Component for IcalTimeZoneTransition {
276    fn add_property(&mut self, property: Property) {
277        self.properties.push(property);
278    }
279
280    fn add_sub_component<B: BufRead>(
281        &mut self,
282        _: &str,
283        _: &RefCell<PropertyParser<B>>,
284    ) -> Result<(), ParserError> {
285        Err(ParserError::InvalidComponent.into())
286    }
287}
288
289#[derive(Debug, Clone, Default)]
290#[cfg_attr(feature = "serde-derive", derive(serde::Serialize, serde::Deserialize))]
291pub struct IcalFreeBusy {
292    pub properties: Vec<Property>,
293}
294
295impl IcalFreeBusy {
296    pub fn new() -> IcalFreeBusy {
297        IcalFreeBusy {
298            properties: Vec::new(),
299        }
300    }
301}
302
303impl Component for IcalFreeBusy {
304    fn add_property(&mut self, property: Property) {
305        self.properties.push(property);
306    }
307
308    fn add_sub_component<B: BufRead>(
309        &mut self,
310        _: &str,
311        _: &RefCell<PropertyParser<B>>,
312    ) -> Result<(), ParserError> {
313        Err(ParserError::InvalidComponent.into())
314    }
315}