-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathmod.rs
More file actions
92 lines (73 loc) · 2.01 KB
/
mod.rs
File metadata and controls
92 lines (73 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//! Main Document part
//!
//! The corresponding ZIP item is `/word/document.xml`.
mod body;
mod bookmark_end;
mod bookmark_start;
mod r#break;
mod grid_column;
mod hyperlink;
mod paragraph;
mod run;
mod table;
mod table_cell;
mod table_grid;
mod table_row;
mod text;
pub use self::{
body::*, bookmark_end::*, bookmark_start::*, grid_column::*, hyperlink::*, paragraph::*,
r#break::*, run::*, table::*, table::*, table_cell::*, table_grid::*, table_row::*, text::*,
};
use std::io::Write;
use strong_xml::{XmlRead, XmlResult, XmlWrite, XmlWriter};
use crate::__xml_test_suites;
use crate::schema::SCHEMA_MAIN;
use std::ops::{Deref, DerefMut};
/// The root element of the main document part.
#[derive(Debug, Default, XmlRead)]
#[cfg_attr(test, derive(PartialEq))]
#[xml(tag = "w:document")]
pub struct Document<'a> {
/// Specifies the body of the docment.
#[xml(child = "w:body")]
pub body: Body<'a>,
}
impl<'a> Document<'a> {
pub fn add_content<T: Into<BodyContent<'a>>>(&mut self, content: T) -> &mut Self {
self.body.add_content(content);
self
}
}
impl<'a> Deref for Document<'a> {
type Target = Body<'a>;
fn deref(&self) -> &Self::Target {
&self.body
}
}
impl<'a> DerefMut for Document<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.body
}
}
impl<'a> XmlWrite for Document<'a> {
fn to_writer<W: Write>(&self, writer: &mut XmlWriter<W>) -> XmlResult<()> {
let Document { body } = self;
log::debug!("[Document] Started writing.");
writer.write_element_start("w:document")?;
writer.write_attribute("xmlns:w", SCHEMA_MAIN)?;
writer.write_element_end_open()?;
body.to_writer(writer)?;
writer.write_element_end_close("w:document")?;
log::debug!("[Document] Finished writing.");
Ok(())
}
}
__xml_test_suites!(
Document,
Document::default(),
format!(
r#"<w:document xmlns:w="{}"><w:body/></w:document>"#,
SCHEMA_MAIN
)
.as_str(),
);