Skip to main content

docx_rs/reader/
move_to.rs

1use std::io::Read;
2use std::str::FromStr;
3
4use super::*;
5
6impl ElementReader for MoveTo {
7    fn read<R: Read>(
8        r: &mut EventReader<R>,
9        attrs: &[OwnedAttribute],
10    ) -> Result<Self, ReaderError> {
11        let mut mt = MoveTo::new_with_empty();
12        loop {
13            let e = r.next_event();
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 => mt = mt.add_run(Run::read(r, &attributes)?),
21                        XMLElement::Delete => mt = mt.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                                    mt = mt.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                                    mt = mt.add_comment_end(id);
35                                }
36                            }
37                            continue;
38                        }
39                        XMLElement::MoveFrom => {
40                            // Skip moveFrom subtree — ghost text must not flatten into MoveTo children
41                            ignore::ignore_element(XMLElement::MoveFrom, XMLElement::MoveFrom, r);
42                        }
43                        _ => {}
44                    }
45                }
46                Ok(XmlEvent::EndElement { name, .. }) => {
47                    let e = XMLElement::from_str(&name.local_name).unwrap();
48                    if e == XMLElement::MoveTo {
49                        for attr in attrs {
50                            let local_name = &attr.name.local_name;
51                            if local_name == "author" {
52                                mt = mt.author(&attr.value);
53                            } else if local_name == "date" {
54                                mt = mt.date(&attr.value);
55                            }
56                        }
57                        return Ok(mt);
58                    }
59                }
60                Err(_) => return Err(ReaderError::XMLReadError),
61                _ => {}
62            }
63        }
64    }
65}