1use std::io::Read;
2use std::str::FromStr;
3
4use super::*;
5
6impl ElementReader for Insert {
7 fn read<R: Read>(
8 r: &mut EventReader<R>,
9 attrs: &[OwnedAttribute],
10 ) -> Result<Self, ReaderError> {
11 let mut ins = Insert::new_with_empty();
12 loop {
13 let e = r.next();
14 match e {
15 Ok(XmlEvent::StartElement {
16 name, attributes, ..
17 }) => {
18 let e = XMLElement::from_str(&name.local_name).unwrap();
19 match e {
20 XMLElement::Run => ins = ins.add_run(Run::read(r, &attributes)?),
21 XMLElement::Delete => ins = ins.add_delete(Delete::read(r, &attributes)?),
22 XMLElement::CommentRangeStart => {
23 if let Some(id) = read(&attributes, "id") {
24 if let Ok(id) = usize::from_str(&id) {
25 let comment = Comment::new(id);
26 ins = ins.add_comment_start(comment);
27 }
28 }
29 continue;
30 }
31 XMLElement::CommentRangeEnd => {
32 if let Some(id) = read(&attributes, "id") {
33 if let Ok(id) = usize::from_str(&id) {
34 ins = ins.add_comment_end(id);
35 }
36 }
37 continue;
38 }
39 _ => {}
40 }
41 }
42 Ok(XmlEvent::EndElement { name, .. }) => {
43 let e = XMLElement::from_str(&name.local_name).unwrap();
44 if e == XMLElement::Insert {
45 for attr in attrs {
46 let local_name = &attr.name.local_name;
47 if local_name == "author" {
48 ins = ins.author(&attr.value);
49 } else if local_name == "date" {
50 ins = ins.date(&attr.value);
51 }
52 }
53 return Ok(ins);
54 }
55 }
56 Err(_) => return Err(ReaderError::XMLReadError),
57 _ => {}
58 }
59 }
60 }
61}