docx_reader/reader/
numberings.rsuse std::io::Read;
use std::str::FromStr;
use xml::reader::{EventReader, XmlEvent};
use crate::reader::{FromXML, ReaderError};
use super::*;
impl FromXML for Numberings {
fn from_xml<R: Read>(reader: R) -> Result<Self, ReaderError> {
let mut parser = EventReader::new(reader);
let mut nums = Self::default();
loop {
let e = parser.next();
match e {
Ok(XmlEvent::StartElement {
attributes, name, ..
}) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
match e {
XMLElement::AbstractNumbering => {
let mut id = 0;
for a in attributes {
let local_name = &a.name.local_name;
if local_name == "abstractNumId" {
id = usize::from_str(&a.value)?;
}
}
let mut abs_num = AbstractNumbering::new(id);
loop {
let e = parser.next();
match e {
Ok(XmlEvent::StartElement {
attributes, name, ..
}) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
match e {
XMLElement::Level => {
let l = Level::read(&mut parser, &attributes)?;
abs_num = abs_num.add_level(l);
}
XMLElement::StyleLink => {
abs_num = abs_num.style_link(&attributes[0].value)
}
XMLElement::NumStyleLink => {
abs_num =
abs_num.num_style_link(&attributes[0].value)
}
_ => {}
}
}
Ok(XmlEvent::EndElement { name, .. }) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
if let XMLElement::AbstractNumbering = e {
nums = nums.add_abstract_numbering(abs_num);
break;
}
}
_ => {}
}
}
continue;
}
XMLElement::Num => {
let mut id = 0;
for a in attributes {
let local_name = &a.name.local_name;
if local_name == "numId" {
id = usize::from_str(&a.value)?;
}
}
let mut abs_num_id = 0;
let mut level_overrides = vec![];
loop {
let e = parser.next();
match e {
Ok(XmlEvent::StartElement {
attributes, name, ..
}) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
match e {
XMLElement::AbstractNumberingId => {
abs_num_id = usize::from_str(&attributes[0].value)?
}
XMLElement::LvlOverride => {
if let Ok(o) =
LevelOverride::read(&mut parser, &attributes)
{
level_overrides.push(o);
}
}
_ => {}
}
}
Ok(XmlEvent::EndElement { name, .. }) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
if let XMLElement::Num = e {
let num = Numbering::new(id, abs_num_id);
nums =
nums.add_numbering(num.overrides(level_overrides));
break;
}
}
_ => {}
}
}
continue;
}
_ => {}
}
}
Ok(XmlEvent::EndElement { name, .. }) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
if let XMLElement::Numbering = e {
break;
}
}
Ok(XmlEvent::EndDocument { .. }) => break,
Err(_) => return Err(ReaderError::XMLReadError),
_ => {}
}
}
Ok(nums)
}
}