aboutsummaryrefslogtreecommitdiff
path: root/src/xmlutil.rs
blob: a0cebaf818203cc04ab9d0ef7a26adc4d99b977b (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
use std::io::BufRead;

use quick_xml::reader::Reader;
use quick_xml::events::Event;
use errors::{ErrorKind, Result};

pub fn element_text<B: BufRead>(reader: &mut Reader<B>) -> Result<String> {
    let content: Result<String>;
    let mut buf = Vec::new();

    if let Ok(Event::Text(e)) = reader.read_event(&mut buf) {
        content = match e.unescape_and_decode(&reader) {
            Ok(s) => Ok(s),
            Err(e) => Err(e.into()),
        }
    } else {
        return Err(ErrorKind::ElementText(reader.buffer_position()).into())
    }

    match reader.read_event(&mut buf) {
        Ok(Event::End(_)) => (),
        _ => return Err(ErrorKind::ClosingTag(reader.buffer_position()).into())
    }

    content
}

pub trait FromXml: Sized {
    fn from_xml<B: BufRead>(reader: &mut Reader<B>) -> Result<Self>;
}