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