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

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

use errors::Result;
use xmlutil::{FromXml, element_text};
use revision::Revision;

#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct Page {
    pub namespace: i32,
    pub title: String,
    pub redirect: Option<String>,
    pub revision: Revision,
}

impl FromXml for Page {
    fn from_xml<B: BufRead>(reader: &mut Reader<B>) -> Result<Self> {
        let mut page = Page::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);
                            page.title = res?;
                        },
                        b"ns" => {
                            let res = element_text(reader);
                            page.namespace = res?.parse()?;
                        },
                        b"revision" => {
                            match Revision::from_xml(reader) {
                                Ok(rev) => {
                                    page.revision = rev;
                                },
                                Err(err) => bail!(err),
                            }
                        },
                        _ => (),
                    }
                },
                Ok(Event::Empty(e)) => {
                    match e.name() {
                        b"redirect" => {
                            for attr in e.attributes().with_checks(false) {
                                if let Ok(attr) = attr {
                                    if attr.key == b"title" {
                                        page.redirect = Some(str::from_utf8(attr.value)?.into());
                                    }
                                }
                            }
                        },
                        _ => (),
                    }
                },
                Ok(Event::End(e)) => {
                    if e.name() == b"page" {
                        return Ok(page)
                    }
                },
                Err(err) => bail!(err),
                _ => (),
            }
        }
    }
}

pub struct PageIter<B: BufRead> {
    reader: Reader<B>,
    buf: Vec<u8>,
}

impl<B: BufRead> Iterator for PageIter<B> {
    type Item = Page;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.reader.read_event(&mut self.buf) {
                Ok(Event::Start(ref e)) => {
                    match e.name() {
                        b"page" => {
                            match Page::from_xml(&mut self.reader) {
                                Ok(page) => {
                                    return Some(page)
                                },
                                Err(err) => {
                                    println!("parse error: {}", err);
                                    return None
                                }
                            }
                        }
                        _ => (),
                    }
                },
                Ok(Event::Eof) => return None,
                Err(err) => {
                    println!("parse error: {}", err);
                    return None
                }
                _ => (),
            }
        }
    }
}

impl<B: BufRead> PageIter<B> {
    pub fn new(reader: Reader<B>) -> Self {
        PageIter {
            reader,
            buf: Vec::new(),
        }
    }
}