use std::io::Read; use std::str::FromStr; use xml::attribute::OwnedAttribute; use xml::reader::{EventReader, XmlEvent}; use super::*; impl ElementReader for AGraphic { fn read( r: &mut EventReader, _attrs: &[OwnedAttribute], ) -> Result { let mut graphic = AGraphic::new(); loop { let e = r.next(); match e { Ok(XmlEvent::StartElement { name, attributes, .. }) => { let e = AXMLElement::from_str(&name.local_name) .expect("should convert to XMLElement"); if let AXMLElement::GraphicData = e { let data = AGraphicData::read(r, &attributes)?; graphic = graphic.add_graphic_data(data); } } Ok(XmlEvent::EndElement { name, .. }) => { let e = AXMLElement::from_str(&name.local_name).unwrap(); if e == AXMLElement::Graphic { return Ok(graphic); } } Err(_) => return Err(ReaderError::XMLReadError), _ => {} } } } } #[cfg(test)] mod tests { use super::*; #[cfg(test)] use pretty_assertions::assert_eq; #[test] fn test_read_graphic_with_textbox() { let c = r#" pattern1 "#; let mut parser = EventReader::new(c.as_bytes()); let g = AGraphic::read(&mut parser, &[]).unwrap(); assert_eq!( g, AGraphic::new().add_graphic_data( AGraphicData::new(GraphicDataType::WpShape).add_shape( WpsShape::new().add_text_box(WpsTextBox::new().add_content( TextBoxContent::new().add_paragraph( Paragraph::new().add_run(Run::new().add_text("pattern1")) ) )) ) ) ); } }