Skip to main content

docx_rs/reader/
paragraph_borders.rs

1use std::io::Read;
2use std::str::FromStr;
3
4use super::*;
5use crate::types::*;
6
7impl ElementReader for ParagraphBorders {
8    fn read<R: Read>(r: &mut EventReader<R>, _: &[OwnedAttribute]) -> Result<Self, ReaderError> {
9        let mut borders = ParagraphBorders::with_empty();
10        loop {
11            match r.next_event() {
12                Ok(XmlEvent::StartElement {
13                    attributes, name, ..
14                }) => {
15                    let position = match XMLElement::from_str(&name.local_name).unwrap() {
16                        XMLElement::Top => Some(ParagraphBorderPosition::Top),
17                        XMLElement::Left => Some(ParagraphBorderPosition::Left),
18                        XMLElement::Bottom => Some(ParagraphBorderPosition::Bottom),
19                        XMLElement::Right => Some(ParagraphBorderPosition::Right),
20                        XMLElement::Between => Some(ParagraphBorderPosition::Between),
21                        XMLElement::Bar => Some(ParagraphBorderPosition::Bar),
22                        _ => None,
23                    };
24
25                    if let Some(position) = position {
26                        let attributes = read_border(&attributes)?;
27                        let mut border = ParagraphBorder::new(position)
28                            .val(attributes.border_type)
29                            .color(attributes.color);
30                        if let Some(size) = attributes.size {
31                            border = border.size(size as usize);
32                        }
33                        if let Some(space) = attributes.space {
34                            border = border.space(space as usize);
35                        }
36                        borders = borders.set(border);
37                    }
38                }
39                Ok(XmlEvent::EndElement { name, .. }) => {
40                    if XMLElement::from_str(&name.local_name).unwrap()
41                        == XMLElement::ParagraphBorders
42                    {
43                        return Ok(borders);
44                    }
45                }
46                Err(_) => return Err(ReaderError::XMLReadError),
47                _ => {}
48            }
49        }
50    }
51}