Skip to main content

docx_rs/reader/
a_graphic_data.rs

1#![allow(clippy::single_match)]
2
3use std::io::Read;
4use std::str::FromStr;
5
6use super::*;
7
8impl ElementReader for AGraphicData {
9    fn read<R: Read>(
10        r: &mut EventReader<R>,
11        attrs: &[OwnedAttribute],
12    ) -> Result<Self, ReaderError> {
13        let mut t = GraphicDataType::Unsupported;
14        for a in attrs {
15            if a.name.local_name == "uri" {
16                t = GraphicDataType::from_str(&a.value).unwrap();
17            }
18        }
19        let mut graphic_data = AGraphicData::new(t);
20        loop {
21            let e = r.next();
22            match e {
23                Ok(XmlEvent::StartElement {
24                    name, attributes, ..
25                }) => {
26                    let e = WpsXMLElement::from_str(&name.local_name)
27                        .expect("should convert to XMLElement");
28                    match e {
29                        WpsXMLElement::Wsp => {
30                            let shape = WpsShape::read(r, &attributes)?;
31                            graphic_data = graphic_data.add_shape(shape);
32                        }
33                        _ => {}
34                    }
35                }
36                Ok(XmlEvent::EndElement { name, .. }) => {
37                    let e = AXMLElement::from_str(&name.local_name).unwrap();
38                    if e == AXMLElement::GraphicData {
39                        return Ok(graphic_data);
40                    }
41                }
42                Err(_) => return Err(ReaderError::XMLReadError),
43                _ => {}
44            }
45        }
46    }
47}