docx_reader/reader/
numberings.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use 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)
	}
}