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