aboutsummaryrefslogtreecommitdiff
path: root/src/revision.rs
blob: 5d47f1a35df857cb1cdf87c945f95f1162d35a3c (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::Result;
use xmlutil::{FromXml, element_text};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum RevisionModel {
    Css,
    Javascript,
    Json,
    MassMessageListContent,
    Scribunto,
    Wikitext,
    Unknown(String),
}

impl Default for RevisionModel {
    fn default() -> RevisionModel { RevisionModel::Unknown(String::from("?")) }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum RevisionFormat {
    Json,
    Css,
    Javascript,
    Plain,
    Wiki,
    Unknown(String),
}

impl Default for RevisionFormat {
    fn default() -> RevisionFormat { RevisionFormat::Unknown(String::from("?")) }
}

#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct Revision {
    pub model: RevisionModel,
    pub format: RevisionFormat,
    pub text: String,
}

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

        loop {
            match reader.read_event(&mut buf) {
                Ok(Event::Start(ref e)) => {
                    match e.name() {
                        b"model" => {
                            let res = element_text(reader);
                            let model = res?;
                            rev.model = match model.as_ref() {
                                "css" => RevisionModel::Css,
                                "javascript" => RevisionModel::Javascript,
                                "json" => RevisionModel::Json,
                                "MassMessageListContent" => RevisionModel::MassMessageListContent,
                                "Scribunto" => RevisionModel::Scribunto,
                                "wikitext" => RevisionModel::Wikitext,
                                _ => RevisionModel::Unknown(model),
                            };
                        },
                        b"format" => {
                            let res = element_text(reader);
                            let format = res?;
                            rev.format = match format.as_ref() {
                                "application/json" => RevisionFormat::Json,
                                "text/css" => RevisionFormat::Css,
                                "text/javascript" => RevisionFormat::Javascript,
                                "text/plain" => RevisionFormat::Plain,
                                "text/x-wiki" => RevisionFormat::Wiki,
                                _ => RevisionFormat::Unknown(format),
                            };
                        },
                        b"text" => {
                            let res = element_text(reader);
                            rev.text = res?;
                        },
                        _ => (),
                    }
                }
                Ok(Event::End(e)) => {
                    if e.name() == b"revision" {
                        return Ok(rev)
                    }
                },
                Err(err) => bail!(err),
                _ => (),
            }
        }
    }
}