diff options
Diffstat (limited to 'src/revision.rs')
-rw-r--r-- | src/revision.rs | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/revision.rs b/src/revision.rs new file mode 100644 index 0000000..5d47f1a --- /dev/null +++ b/src/revision.rs @@ -0,0 +1,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), + _ => (), + } + } + } +} |