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

use quick_xml::events::Event;
use quick_xml::reader::Reader;

use errors::{Error, ErrorKind};
use xmlutil::{FromXml, element_text};

#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct AbstractLink {
    pub anchor: String,
    pub link: String,
}

impl FromXml for AbstractLink {
    fn from_xml<B: BufRead>(reader: &mut Reader<B>) -> Result<Self, Error> {
        let mut link = AbstractLink::default();
        let mut buf = Vec::new();

        loop {
            match reader.read_event(&mut buf) {
                Ok(Event::Start(ref e)) => {
                    match e.name() {
                        b"anchor" => {
                            let res = element_text(reader);
                            link.anchor = res?;
                        },
                        b"link" => {
                            let res = element_text(reader);
                            link.link = res?;
                        },
                        _ => (),
                    }
                },
                Ok(Event::End(_)) => {
                    return Ok(link)
                },
                Err(err) => bail!(err),
                _ => (),
            }
        }
    }
}

#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct AbstractDoc {
    pub title: String,
    pub url: String,
    pub abstract_text: String,
    pub links: Vec<AbstractLink>,
}

impl FromXml for AbstractDoc {
    fn from_xml<B: BufRead>(reader: &mut Reader<B>) -> Result<Self, Error> {
        let mut abs = AbstractDoc::default();
        let mut buf = Vec::new();

        loop {
            match reader.read_event(&mut buf) {
                Ok(Event::Start(ref e)) => {
                    match e.name() {
                        b"title" => {
                            let res = element_text(reader);
                            abs.title = res?;
                        },
                        b"url" => {
                            let res = element_text(reader);
                            abs.url = res?;
                        },
                        b"abstract" => {
                            let res = element_text(reader);
                            abs.abstract_text = res?;
                        },
                        b"sublink" => {
                            match AbstractLink::from_xml(reader) {
                                Ok(link) => {
                                    abs.links.push(link);
                                },
                                Err(err) => bail!(err),
                            }
                        },
                        _ => {},
                    }
                },
                Ok(Event::End(_)) => {
                    return Ok(abs);
                },
                Ok(Event::Eof) => break,
                Err(err) => bail!(err),
                _ => (),
            }
        }

        bail!(ErrorKind::EOF)
    }
}