docx_rs/reader/
footer.rs

1use std::io::Read;
2use std::str::FromStr;
3
4use crate::reader::*;
5use xml::reader::{EventReader, XmlEvent};
6
7use super::{Paragraph, Table};
8
9impl FromXML for Footer {
10    fn from_xml<R: Read>(reader: R) -> Result<Self, ReaderError> {
11        let mut parser = EventReader::new(reader);
12        let mut footer = Self::default();
13        loop {
14            let e = parser.next();
15            match e {
16                Ok(XmlEvent::StartElement {
17                    attributes, name, ..
18                }) => {
19                    let e = XMLElement::from_str(&name.local_name).unwrap();
20                    match e {
21                        XMLElement::Paragraph => {
22                            if let Ok(p) = Paragraph::read(&mut parser, &attributes) {
23                                footer = footer.add_paragraph(p);
24                            }
25                            continue;
26                        }
27                        XMLElement::Table => {
28                            if let Ok(t) = Table::read(&mut parser, &attributes) {
29                                footer = footer.add_table(t);
30                            }
31                            continue;
32                        }
33                        XMLElement::StructuredDataTag => {
34                            if let Ok(tag) = StructuredDataTag::read(&mut parser, &attributes) {
35                                footer = footer.add_structured_data_tag(tag);
36                            }
37                            continue;
38                        }
39                        _ => {}
40                    }
41                }
42                Ok(XmlEvent::EndDocument) => break,
43                Err(_) => return Err(ReaderError::XMLReadError),
44                _ => {}
45            }
46        }
47        Ok(footer)
48    }
49}
50
51#[test]
52fn test_footer_from_xml() {
53    let xml = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
54<w:ftr xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
55    xmlns:o="urn:schemas-microsoft-com:office:office"
56    xmlns:v="urn:schemas-microsoft-com:vml"
57    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
58    xmlns:w10="urn:schemas-microsoft-com:office:word"
59    xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
60    xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"
61    xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
62    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
63    xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
64    xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" mc:Ignorable="w14 wp14">
65    <w:p w14:paraId="12345678">
66        <w:pPr>
67            <w:rPr />
68        </w:pPr>
69        <w:r>
70            <w:rPr />
71            <w:t xml:space="preserve">Hello Footer</w:t>
72        </w:r>
73    </w:p>
74</w:ftr>"#;
75    let h = Footer::from_xml(xml.as_bytes()).unwrap();
76    let expected =
77        Footer::new().add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello Footer")));
78    assert_eq!(h, expected)
79}