2020-02-11 10:01:39 +02:00
|
|
|
use std::io::Read;
|
|
|
|
use xml::reader::{EventReader, XmlEvent};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::reader::{FromXML, ReaderError};
|
|
|
|
|
|
|
|
impl FromXML for Rels {
|
|
|
|
fn from_xml<R: Read>(reader: R) -> Result<Self, ReaderError> {
|
|
|
|
let parser = EventReader::new(reader);
|
|
|
|
let mut s = Self::default();
|
|
|
|
let mut depth = 0;
|
|
|
|
for e in parser {
|
|
|
|
match e {
|
|
|
|
Ok(XmlEvent::StartElement { attributes, .. }) => {
|
|
|
|
if depth == 1 {
|
|
|
|
let mut rel_type = "".to_owned();
|
|
|
|
let mut target = "".to_owned();
|
|
|
|
for attr in attributes {
|
|
|
|
let name: &str = &attr.name.local_name;
|
2021-06-30 16:37:13 +03:00
|
|
|
if name == "Type" {
|
2020-02-11 10:01:39 +02:00
|
|
|
rel_type = attr.value.clone();
|
|
|
|
} else if name == "Target" {
|
|
|
|
target = attr.value.clone();
|
|
|
|
}
|
|
|
|
}
|
2021-06-30 16:37:13 +03:00
|
|
|
s = s.add_rel(rel_type, target);
|
2020-02-11 10:01:39 +02:00
|
|
|
}
|
|
|
|
depth += 1;
|
|
|
|
}
|
|
|
|
Ok(XmlEvent::EndElement { .. }) => {
|
|
|
|
depth -= 1;
|
|
|
|
}
|
|
|
|
Err(_) => return Err(ReaderError::XMLReadError),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
#[cfg(test)]
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_xml() {
|
|
|
|
let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
|
|
|
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml" />
|
|
|
|
</Relationships>"#;
|
|
|
|
let c = Rels::from_xml(xml.as_bytes()).unwrap();
|
2021-11-29 19:36:04 +02:00
|
|
|
let rels =
|
|
|
|
vec![
|
|
|
|
(
|
2020-02-11 10:01:39 +02:00
|
|
|
"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"
|
|
|
|
.to_owned(),
|
|
|
|
"rId1".to_owned(),
|
|
|
|
"docProps/core.xml".to_owned(),
|
2021-11-29 19:36:04 +02:00
|
|
|
)];
|
2020-02-11 10:01:39 +02:00
|
|
|
assert_eq!(Rels { rels }, c);
|
|
|
|
}
|
|
|
|
}
|