docx_rs/reader/
numbering_property.rs

1use std::io::Read;
2use std::str::FromStr;
3
4use xml::attribute::OwnedAttribute;
5use xml::reader::{EventReader, XmlEvent};
6
7use super::*;
8
9impl ElementReader for NumberingProperty {
10    fn read<R: Read>(
11        r: &mut EventReader<R>,
12        _attrs: &[OwnedAttribute],
13    ) -> Result<Self, ReaderError> {
14        let mut id: Option<usize> = None;
15        let mut level: Option<usize> = None;
16        loop {
17            let e = r.next();
18            match e {
19                Ok(XmlEvent::StartElement {
20                    attributes, name, ..
21                }) => {
22                    let e = XMLElement::from_str(&name.local_name).unwrap();
23                    match e {
24                        XMLElement::IndentLevel => {
25                            level = Some(usize::from_str(&attributes[0].value)?);
26                            continue;
27                        }
28                        XMLElement::NumberingId => {
29                            id = Some(usize::from_str(&attributes[0].value)?);
30                            continue;
31                        }
32                        _ => {}
33                    }
34                }
35                Ok(XmlEvent::EndElement { name, .. }) => {
36                    let e = XMLElement::from_str(&name.local_name).unwrap();
37                    if e == XMLElement::NumberingProperty {
38                        if level.is_none() {
39                            if let Some(id) = id {
40                                return Ok(NumberingProperty::new().id(NumberingId::new(id)));
41                            }
42                        }
43                        if let Some(id) = id {
44                            let np = NumberingProperty::new()
45                                .add_num(NumberingId::new(id), IndentLevel::new(level.unwrap()));
46                            return Ok(np);
47                        }
48                        return Ok(NumberingProperty::new());
49                    }
50                }
51                Err(_) => return Err(ReaderError::XMLReadError),
52                _ => {}
53            }
54        }
55    }
56}