summaryrefslogtreecommitdiffstats
path: root/src/component.rs
blob: f87e9925baafcc76a2092b6da22fbfd8d05fc879 (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
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
use std::str::FromStr;
use std::collections::BTreeMap;

use property::Property;
use parser::Parser;

use error::*;

#[derive(Clone, Debug)]
pub struct Component {
    /// The name of the component, such as `VCARD` or `VEVENT`.
    pub name: String,

    /// The component's properties.
    pub props: BTreeMap<String, Vec<Property>>,

    /// The component's child- or sub-components.
    pub subcomponents: Vec<Component>
}

impl Component {
    pub fn new<N: Into<String>>(name: N) -> Component {
        Component {
            name: name.into(),
            props: BTreeMap::new(),
            subcomponents: vec![]
        }
    }

    /// Append the given property, preserve other same-named properties.
    pub fn push(&mut self, prop: Property) {
        self.props.entry(prop.name.clone()).or_insert_with(Vec::new).push(prop);
    }

    /// Set the given property, remove other same-named properties.
    pub fn set(&mut self, prop: Property) {
        self.props.insert(prop.name.clone(), vec![prop]);
    }

    /// Retrieve one property by key. Returns `None` if not exactly one property was found.
    pub fn get_only<P: AsRef<str>>(&self, name: P) -> Option<&Property> {
        match self.props.get(name.as_ref()) {
            Some(x) if x.len() == 1 => Some(&x[0]),
            _ => None
        }
    }

    /// Retrieve properties by key. Returns an empty slice if key doesn't exist.
    pub fn get_all<P: AsRef<str>>(&self, name: P) -> &[Property] {
        static EMPTY: &'static [Property] = &[];
        match self.props.get(name.as_ref()) {
            Some(values) => &values[..],
            None => EMPTY
        }
    }

    /// Remove a single property.
    pub fn pop<P: AsRef<str>>(&mut self, name: P) -> Option<Property> {
        match self.props.get_mut(name.as_ref()) {
            Some(values) => values.pop(),
            None => None
        }
    }

    /// Remove all properties
    pub fn remove<P: AsRef<str>>(&mut self, name: P) -> Option<Vec<Property>> {
        self.props.remove(name.as_ref())
    }
}

impl FromStr for Component {
    type Err = VObjectErrorKind;

    /// Same as `vobject::parse_component`
    fn from_str(s: &str) -> Result<Component> {
        parse_component(s)
    }
}

/// Parse exactly one component. Trailing data generates errors.
pub fn parse_component(s: &str) -> Result<Component> {
    let (rv, new_s) = read_component(s)?;
    if !new_s.is_empty() {
        let s = format!("Trailing data: `{}`", new_s);
        return Err(VObjectErrorKind::ParserError(s));
    }

    Ok(rv)
}

/// Parse one component and return the rest of the string.
pub fn read_component(s: &str) -> Result<(Component, &str)> {
    let mut parser = Parser::new(s);
    let rv = parser.consume_component()?;
    let new_s = if parser.eof() {
        ""
    } else {
        &parser.input[parser.pos..]
    };
    Ok((rv, new_s))
}

/// Write a component to a String.
pub fn write_component(c: &Component) -> String {
    fn inner(buf: &mut String, c: &Component) {
        buf.push_str("BEGIN:");
        buf.push_str(&c.name);
        buf.push_str("\r\n");

        for (prop_name, props) in &c.props {
            for prop in props.iter() {
                if let Some(ref x) = prop.prop_group {
                    buf.push_str(&x);
                    buf.push('.');
                };
                buf.push_str(&prop_name);
                for (param_key, param_value) in &prop.params {
                    buf.push(';');
                    buf.push_str(&param_key);
                    buf.push('=');
                    buf.push_str(&param_value);
                }
                buf.push(':');
                buf.push_str(&fold_line(&prop.raw_value));
                buf.push_str("\r\n");
            }
        }

        for subcomponent in &c.subcomponents {
            inner(buf, subcomponent);
        }

        buf.push_str("END:");
        buf.push_str(&c.name);
        buf.push_str("\r\n");
    }

    let mut buf = String::new();
    inner(&mut buf, c);
    buf
}

/// Fold contentline to 75 bytes or less. This function assumes the input
/// to be unfolded, which means no '\n' or '\r' in it.
pub fn fold_line(line: &str) -> String {
    let limit = 75;
    let len = line.len();
    let mut bytes_remaining = len;
    let mut ret = String::with_capacity(len + (len / limit * 3));

    let mut pos = 0;
    let mut next_pos = limit;
    while bytes_remaining > limit {
        while line.is_char_boundary(next_pos) == false {
            next_pos -= 1;
        }
        ret.push_str(&line[pos..next_pos]);
        ret.push_str("\r\n ");

        bytes_remaining -= next_pos - pos;
        pos = next_pos;
        next_pos += limit;
    }

    ret.push_str(&line[len - bytes_remaining..]);
    ret
}


#[cfg(test)]
mod tests {
    use component::fold_line;

    #[test]
    fn test_fold() {
        let line = "This should be multiple lines and fold on char boundaries. 毎害止\
                   加食下組多地将写館来局必第。東証細再記得玲祉込吉宣会法授";
        let expected = "This should be multiple lines and fold on char boundaries. 毎害止\
                       加食\r\n 下組多地将写館来局必第。東証細再記得玲祉込吉宣会法\r\n 授";
        assert_eq!(expected, fold_line(line));
        assert_eq!("ab", fold_line("ab"));
    }

}