aboutsummaryrefslogtreecommitdiff
path: root/src/xmlutil.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/xmlutil.rs')
-rw-r--r--src/xmlutil.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/xmlutil.rs b/src/xmlutil.rs
new file mode 100644
index 0000000..a0cebaf
--- /dev/null
+++ b/src/xmlutil.rs
@@ -0,0 +1,30 @@
+use std::io::BufRead;
+
+use quick_xml::reader::Reader;
+use quick_xml::events::Event;
+use errors::{ErrorKind, Result};
+
+pub fn element_text<B: BufRead>(reader: &mut Reader<B>) -> Result<String> {
+ let content: Result<String>;
+ let mut buf = Vec::new();
+
+ if let Ok(Event::Text(e)) = reader.read_event(&mut buf) {
+ content = match e.unescape_and_decode(&reader) {
+ Ok(s) => Ok(s),
+ Err(e) => Err(e.into()),
+ }
+ } else {
+ return Err(ErrorKind::ElementText(reader.buffer_position()).into())
+ }
+
+ match reader.read_event(&mut buf) {
+ Ok(Event::End(_)) => (),
+ _ => return Err(ErrorKind::ClosingTag(reader.buffer_position()).into())
+ }
+
+ content
+}
+
+pub trait FromXml: Sized {
+ fn from_xml<B: BufRead>(reader: &mut Reader<B>) -> Result<Self>;
+}