docx_rs/reader/
comment.rs1use std::io::Read;
2use std::str::FromStr;
3
4use xml::attribute::OwnedAttribute;
5use xml::reader::{EventReader, XmlEvent};
6
7use super::*;
8use crate::escape;
9
10impl ElementReader for Comment {
11 fn read<R: Read>(
12 r: &mut EventReader<R>,
13 attrs: &[OwnedAttribute],
14 ) -> Result<Self, ReaderError> {
15 let id = usize::from_str(&read(attrs, "id").expect("should comment id exists."))?;
16 let mut comment = Comment::new(id);
17 if let Some(author) = read(attrs, "author") {
18 comment = comment.author(escape::escape(author.as_str()));
19 };
20 if let Some(date) = read(attrs, "date") {
21 comment = comment.date(date);
22 }
23 loop {
24 let e = r.next();
25 match e {
26 Ok(XmlEvent::StartElement {
27 name, attributes, ..
28 }) => {
29 let e = XMLElement::from_str(&name.local_name)
30 .expect("should convert to XMLElement");
31 if let XMLElement::Paragraph = e {
32 let p = Paragraph::read(r, &attributes)?;
33 comment = comment.add_paragraph(p);
34 }
35 }
36 Ok(XmlEvent::EndElement { name, .. }) => {
37 let e = XMLElement::from_str(&name.local_name).unwrap();
38 if e == XMLElement::Comment {
39 return Ok(comment);
40 }
41 }
42 Err(_) => return Err(ReaderError::XMLReadError),
43 _ => {}
44 }
45 }
46 }
47}