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(reader: &mut Reader) -> Result { 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, } impl FromXml for AbstractDoc { fn from_xml(reader: &mut Reader) -> Result { 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) } }