summaryrefslogtreecommitdiffstats
path: root/src/calendar.rs
blob: a6c3660395d45b65d786ba91046e6d91134035d7 (plain)
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
use components::*;

use std::fmt;
use std::ops::Deref;
use std::convert::Into;


#[derive(Debug)]
pub enum CalendarElement{
    Todo(Todo),
    Event(Event)
}

impl Into<CalendarElement> for Event {
    fn into(self) -> CalendarElement {
        CalendarElement::Event(self)
    }
}

impl Into<CalendarElement> for Todo {
    fn into(self) -> CalendarElement {
        CalendarElement::Todo(self)
    }
}

impl CalendarElement {
    fn fmt_write<W: fmt::Write>(&self, out: &mut W) -> Result<(), fmt::Error> {
        match *self {
            CalendarElement::Todo(ref todo)   => todo.fmt_write(out),
            CalendarElement::Event(ref event) => event.fmt_write(out)
        }
    }
}

/// Represents a calendar
///
/// You can `.add()` `Component`s to this.
#[derive(Default,Debug)]
pub struct Calendar {
    components: Vec<CalendarElement>
}

impl Calendar {

    /// Creates a new Calendar.
    pub fn new() -> Self {
        Default::default()
    }

    #[deprecated(note="Use .push() instead")]
    #[doc(hidden)]
    pub fn add<T:Into<CalendarElement>>(&mut self, component:T) -> &mut Self {
        self.push(component)
    }

    /// Moves all the elements of other into Self, leaving other empty.
    pub fn append(&mut self, other: &mut Calendar) {
        self.components.append(&mut other.components);
    }

    /// Extends this `Calendar` with the contends of another.
    pub fn extend<T>(&mut self, other: T)
        where T: IntoIterator<Item=CalendarElement>
    {
        self.components.extend(other);
    }

    /// Appends an element to the back of the `Calendar`.
    pub fn push<T:Into<CalendarElement>>(&mut self, component:T) -> &mut Self {
        self.components.push(component.into());
        self
    }

    /// Writes `Calendar` into a `Writer` using `std::fmt`.
    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 component in &self.components {
            component.fmt_write(out)?;
        }
        write_crlf!(out, "END:VCALENDAR")?;
        Ok(())
    }

    /// Prints to stdout
    /// FIXME code repetition
    pub fn print(&self) -> Result<(), fmt::Error> {
        let mut out = String::new();
        try!(self.fmt_write(&mut out));
        print_crlf!("{}", out);
        Ok(())
    }
}

impl ToString for Calendar {
    /// # panics
    fn to_string(&self) -> String {
        let mut out_string = String::new();
        self.fmt_write(&mut out_string).unwrap();
        out_string
    }
}

impl Deref for Calendar {
    type Target = [CalendarElement];

    fn deref(&self) -> &[CalendarElement]{
        self.components.deref()
    }
}