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
//! Wrapper around `PropertyParser`
//!
//! #### Warning
//!   The parsers (`VcardParser` / `IcalParser`) only parse the content and set to uppercase
//!   the case-insensitive fields.  No checks are made on the fields validity.
//!
//!

pub mod ical;
pub mod vcard;

// Sys mods
use std::io::BufRead;
use std::cell::RefCell;

// Internal mods
use property::{Property, PropertyParser};
use parser::errors::*;

/// An interface for an Ical/Vcard component.
///
/// It take a `PropertyParser` and fill the component with. It's also able to create
/// sub-component used by event and alarms.
pub trait Component {
    /// Add the givent sub component.
    fn add_sub_component<B: BufRead>(&mut self,
                                     value: &str,
                                     line_parser: &RefCell<PropertyParser<B>>)
                                     -> Result<()>;

    /// Add the givent property.
    fn add_property(&mut self, property: Property);

    /// Parse the content from `line_parser` and fill the component with.
    fn parse<B: BufRead>(&mut self, line_parser: &RefCell<PropertyParser<B>>) -> Result<()> {

        loop {
            let line: Property;

            {
                line = match line_parser.borrow_mut().next() {
                    Some(val) => val,
                    None => return Err(ErrorKind::NotComplete.into()),
                }?;
            }

            match line.name.as_str() {
                "END" => break,
                "BEGIN" => {
                    match line.value {
                        Some(v) => self.add_sub_component(v.as_str(), line_parser)?,
                        None => return Err(ErrorKind::NotComplete.into()),
                    }
                }

                _ => self.add_property(line),
            };
        }

        Ok(())
    }
}


#[allow(missing_docs)]
pub mod errors {
    //! The parser errors.

    use property;

    error_chain! {
        types {
            Error, ErrorKind, ResultExt, Result;
        }

        foreign_links {
            Property(property::errors::Error);
        }

        errors {

            /// The current component is invalid.
            InvalidComponent {
                description("The current component is invalid.")
                    display("invalid component")
            }

            /// the current object is not complete.
            NotComplete {
                description("The current object is not complete.")
                    display("incomplete object")
            }

            /// A header is missing.
            MissingHeader {
                description("A header is missing.")
                    display("missing header")
            }
        }
    }
}