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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use chrono::Duration;
use std::{fmt, iter::FromIterator, mem, ops::Deref};
use crate::{components::*, Parameter, Property};
mod calendar_component;
pub use calendar_component::CalendarComponent;
#[derive(Default, Debug, PartialEq, Eq)]
pub struct Calendar {
properties: Vec<Property>,
components: Vec<CalendarComponent>,
}
impl Calendar {
pub fn new() -> Self {
Default::default()
}
#[deprecated(note = "Use .push() instead")]
#[doc(hidden)]
pub fn add<T: Into<CalendarComponent>>(&mut self, component: T) -> &mut Self {
self.push(component)
}
pub fn append(&mut self, other: &mut Calendar) {
self.components.append(&mut other.components);
}
pub fn append_property(&mut self, property: Property) -> &mut Self {
self.properties.push(property);
self
}
fn property_value(&self, key: &str) -> Option<&str> {
Some(
self.properties
.iter()
.find(|property| property.key() == key)?
.value(),
)
}
pub fn extend<T, U>(&mut self, other: T)
where
T: IntoIterator<Item = U>,
U: Into<CalendarComponent>,
{
self.components.extend(other.into_iter().map(Into::into));
}
pub fn push<T: Into<CalendarComponent>>(&mut self, component: T) -> &mut Self {
self.components.push(component.into());
self
}
pub fn name(&mut self, name: &str) -> &mut Self {
self.append_property(Property::new("NAME", name));
self.append_property(Property::new("X-WR-CALNAME", name));
self
}
pub fn get_name(&self) -> Option<&str> {
self.property_value("NAME")
.or_else(|| self.property_value("X-WR-CALNAME"))
}
pub fn description(&mut self, description: &str) -> &mut Self {
self.append_property(Property::new("DESCRIPTION", description));
self.append_property(Property::new("X-WR-CALDESC", description));
self
}
pub fn get_description(&self) -> Option<&str> {
self.property_value("DESCRIPTION")
.or_else(|| self.property_value("X-WR-CALDESC"))
}
pub fn timezone(&mut self, timezone: &str) -> &mut Self {
self.append_property(Property::new("TIMEZONE-ID", timezone));
self.append_property(Property::new("X-WR-TIMEZONE", timezone));
self
}
pub fn get_timezone(&self) -> Option<&str> {
self.property_value("TIMEZONE_ID")
.or_else(|| self.property_value("X-WR-TIMEZONE"))
}
pub fn ttl(&mut self, duration: &Duration) -> &mut Self {
let duration_string = duration.to_string();
self.append_property(
Property::new("REFRESH-INTERVAL", &duration_string)
.append_parameter(Parameter::new("VALUE", "DURATION"))
.done(),
);
self.append_property(Property::new("X-PUBLISHED-TTL", &duration_string));
self
}
pub fn done(&mut self) -> Self {
Calendar {
properties: mem::take(&mut self.properties),
components: mem::take(&mut self.components),
}
}
fn fmt_write<W: fmt::Write>(&self, out: &mut W) -> Result<(), fmt::Error> {
write_crlf!(out, "BEGIN:VCALENDAR")?;
write_crlf!(out, "VERSION:2.0")?;
write_crlf!(out, "PRODID:ICALENDAR-RS")?;
write_crlf!(out, "CALSCALE:GREGORIAN")?;
for property in &self.properties {
property.fmt_write(out)?;
}
for component in &self.components {
component.fmt_write(out)?;
}
write_crlf!(out, "END:VCALENDAR")?;
Ok(())
}
pub fn print(&self) -> Result<(), fmt::Error> {
print_crlf!("{}", self);
Ok(())
}
}
impl fmt::Display for Calendar {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.fmt_write(f)
}
}
impl TryInto<String> for &Calendar {
type Error = std::fmt::Error;
fn try_into(self) -> Result<String, Self::Error> {
let mut out_string = String::new();
self.fmt_write(&mut out_string)?;
Ok(out_string)
}
}
impl Deref for Calendar {
type Target = [CalendarComponent];
fn deref(&self) -> &[CalendarComponent] {
self.components.deref()
}
}
impl AsRef<[CalendarComponent]> for Calendar {
fn as_ref(&self) -> &[CalendarComponent] {
self.components.deref()
}
}
impl<T: Into<CalendarComponent>, const N: usize> From<[T; N]> for Calendar {
fn from(elements: [T; N]) -> Self {
elements.into_iter().collect()
}
}
impl<C: Into<CalendarComponent>> From<C> for Calendar {
fn from(element: C) -> Self {
Calendar {
components: vec![element.into()],
..Default::default()
}
}
}
impl<C: Into<CalendarComponent>> FromIterator<C> for Calendar {
fn from_iter<T: IntoIterator<Item = C>>(iter: T) -> Self {
Calendar {
components: iter.into_iter().map(Into::into).collect(),
..Default::default()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn calendar_extend_components() {
let mut calendar = Calendar::new();
let components = vec![
CalendarComponent::Event(Event::new()),
CalendarComponent::Event(Event::new()),
];
calendar.extend(components);
assert_eq!(calendar.components.len(), 2);
}
#[test]
fn calendar_extend_events() {
let mut calendar = Calendar::new();
let events = vec![Event::new(), Event::new()];
calendar.extend(events);
assert_eq!(calendar.components.len(), 2);
}
#[test]
fn get_properties_unset() {
let calendar = Calendar::new();
assert_eq!(calendar.get_name(), None);
assert_eq!(calendar.get_description(), None);
assert_eq!(calendar.get_timezone(), None);
}
#[test]
fn get_properties_set() {
let calendar = Calendar::new()
.name("name")
.description("description")
.timezone("timezone")
.done();
assert_eq!(calendar.get_name(), Some("name"));
assert_eq!(calendar.get_description(), Some("description"));
assert_eq!(calendar.get_timezone(), Some("timezone"));
}
#[test]
fn get_properties_alternate() {
let calendar = Calendar::new()
.append_property(Property::new("X-WR-CALNAME", "name"))
.append_property(Property::new("X-WR-CALDESC", "description"))
.append_property(Property::new("X-WR-TIMEZONE", "timezone"))
.done();
assert_eq!(calendar.get_name(), Some("name"));
assert_eq!(calendar.get_description(), Some("description"));
assert_eq!(calendar.get_timezone(), Some("timezone"));
}
}