2020-02-11 10:01:39 +02:00
|
|
|
use serde::ser::{SerializeStruct, Serializer};
|
|
|
|
use serde::Serialize;
|
|
|
|
|
2020-09-02 12:23:53 +03:00
|
|
|
use super::*;
|
2019-11-07 09:08:59 +02:00
|
|
|
use crate::documents::BuildXML;
|
|
|
|
use crate::xml_builder::*;
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-12-08 21:14:27 +02:00
|
|
|
pub struct Document {
|
2020-02-11 10:01:39 +02:00
|
|
|
pub children: Vec<DocumentChild>,
|
2019-12-13 05:58:01 +02:00
|
|
|
pub section_property: SectionProperty,
|
2019-12-16 04:36:04 +02:00
|
|
|
pub has_numbering: bool,
|
2019-11-12 12:21:08 +02:00
|
|
|
}
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2019-12-08 21:14:27 +02:00
|
|
|
pub enum DocumentChild {
|
|
|
|
Paragraph(Paragraph),
|
|
|
|
Table(Table),
|
2020-09-02 12:23:53 +03:00
|
|
|
BookmarkStart(BookmarkStart),
|
|
|
|
BookmarkEnd(BookmarkEnd),
|
2020-12-18 09:09:48 +02:00
|
|
|
CommentStart(Box<CommentRangeStart>),
|
|
|
|
CommentEnd(CommentRangeEnd),
|
2019-11-07 10:31:04 +02:00
|
|
|
}
|
2019-11-07 09:08:59 +02:00
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
impl Serialize for DocumentChild {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
match *self {
|
|
|
|
DocumentChild::Paragraph(ref p) => {
|
|
|
|
let mut t = serializer.serialize_struct("Paragraph", 2)?;
|
|
|
|
t.serialize_field("type", "paragraph")?;
|
|
|
|
t.serialize_field("data", p)?;
|
|
|
|
t.end()
|
|
|
|
}
|
|
|
|
DocumentChild::Table(ref c) => {
|
|
|
|
let mut t = serializer.serialize_struct("Table", 2)?;
|
|
|
|
t.serialize_field("type", "table")?;
|
|
|
|
t.serialize_field("data", c)?;
|
|
|
|
t.end()
|
|
|
|
}
|
2020-09-02 12:23:53 +03:00
|
|
|
DocumentChild::BookmarkStart(ref c) => {
|
|
|
|
let mut t = serializer.serialize_struct("BookmarkStart", 2)?;
|
|
|
|
t.serialize_field("type", "bookmarkStart")?;
|
|
|
|
t.serialize_field("data", c)?;
|
|
|
|
t.end()
|
|
|
|
}
|
|
|
|
DocumentChild::BookmarkEnd(ref c) => {
|
|
|
|
let mut t = serializer.serialize_struct("BookmarkEnd", 2)?;
|
|
|
|
t.serialize_field("type", "bookmarkEnd")?;
|
|
|
|
t.serialize_field("data", c)?;
|
|
|
|
t.end()
|
|
|
|
}
|
2020-12-18 09:09:48 +02:00
|
|
|
DocumentChild::CommentStart(ref r) => {
|
|
|
|
let mut t = serializer.serialize_struct("CommentRangeStart", 2)?;
|
|
|
|
t.serialize_field("type", "commentRangeStart")?;
|
|
|
|
t.serialize_field("data", r)?;
|
|
|
|
t.end()
|
|
|
|
}
|
|
|
|
DocumentChild::CommentEnd(ref r) => {
|
|
|
|
let mut t = serializer.serialize_struct("CommentRangeEnd", 2)?;
|
|
|
|
t.serialize_field("type", "commentRangeEnd")?;
|
|
|
|
t.serialize_field("data", r)?;
|
|
|
|
t.end()
|
|
|
|
}
|
2020-02-11 10:01:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Document {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
children: Vec::new(),
|
|
|
|
section_property: SectionProperty::new(),
|
|
|
|
has_numbering: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl Document {
|
|
|
|
pub fn new() -> Document {
|
2019-11-07 09:08:59 +02:00
|
|
|
Default::default()
|
|
|
|
}
|
2019-11-07 10:31:04 +02:00
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn add_paragraph(mut self, p: Paragraph) -> Self {
|
2019-12-16 04:36:04 +02:00
|
|
|
if p.has_numbering {
|
|
|
|
self.has_numbering = true
|
|
|
|
}
|
2019-11-13 09:51:58 +02:00
|
|
|
self.children.push(DocumentChild::Paragraph(p));
|
2019-11-12 12:21:08 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn add_table(mut self, t: Table) -> Self {
|
2019-12-16 04:36:04 +02:00
|
|
|
if t.has_numbering {
|
|
|
|
self.has_numbering = true
|
|
|
|
}
|
2019-11-13 09:51:58 +02:00
|
|
|
self.children.push(DocumentChild::Table(t));
|
2019-11-07 10:31:04 +02:00
|
|
|
self
|
|
|
|
}
|
2020-09-02 12:23:53 +03:00
|
|
|
|
|
|
|
pub fn add_bookmark_start(mut self, id: usize, name: impl Into<String>) -> Self {
|
|
|
|
self.children
|
|
|
|
.push(DocumentChild::BookmarkStart(BookmarkStart::new(id, name)));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_bookmark_end(mut self, id: usize) -> Self {
|
|
|
|
self.children
|
|
|
|
.push(DocumentChild::BookmarkEnd(BookmarkEnd::new(id)));
|
|
|
|
self
|
|
|
|
}
|
2020-10-09 14:30:55 +03:00
|
|
|
|
2020-12-18 09:09:48 +02:00
|
|
|
pub fn add_comment_start(mut self, comment: Comment) -> Self {
|
|
|
|
self.children.push(DocumentChild::CommentStart(Box::new(
|
|
|
|
CommentRangeStart::new(comment),
|
|
|
|
)));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_comment_end(mut self, id: usize) -> Self {
|
|
|
|
self.children
|
|
|
|
.push(DocumentChild::CommentEnd(CommentRangeEnd::new(id)));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-10-09 14:30:55 +03:00
|
|
|
pub fn page_size(mut self, size: PageSize) -> Self {
|
|
|
|
self.section_property = self.section_property.page_size(size);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn page_margin(mut self, margin: crate::types::PageMargin) -> Self {
|
|
|
|
self.section_property = self.section_property.page_margin(margin);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-03-24 09:51:11 +02:00
|
|
|
pub fn page_orient(mut self, o: crate::types::PageOrientationType) -> Self {
|
|
|
|
self.section_property = self.section_property.page_orient(o);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-03-19 18:38:22 +02:00
|
|
|
pub fn doc_grid(mut self, doc_grid: DocGrid) -> Self {
|
|
|
|
self.section_property = self.section_property.doc_grid(doc_grid);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-10-09 14:30:55 +03:00
|
|
|
pub fn default_section_property(mut self, property: SectionProperty) -> Self {
|
|
|
|
self.section_property = property;
|
|
|
|
self
|
|
|
|
}
|
2021-11-24 18:49:27 +02:00
|
|
|
|
2021-11-25 16:33:13 +02:00
|
|
|
pub fn header_reference(mut self, r: HeaderReference) -> Self {
|
|
|
|
self.section_property = self.section_property.header_reference(r);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
pub fn footer_reference(mut self, r: FooterReference) -> Self {
|
|
|
|
self.section_property = self.section_property.footer_reference(r);
|
|
|
|
self
|
|
|
|
}
|
2019-11-07 09:08:59 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 09:09:48 +02:00
|
|
|
impl BuildXML for DocumentChild {
|
|
|
|
fn build(&self) -> Vec<u8> {
|
|
|
|
match self {
|
|
|
|
DocumentChild::Paragraph(v) => v.build(),
|
|
|
|
DocumentChild::Table(v) => v.build(),
|
|
|
|
DocumentChild::BookmarkStart(v) => v.build(),
|
|
|
|
DocumentChild::BookmarkEnd(v) => v.build(),
|
|
|
|
DocumentChild::CommentStart(v) => v.build(),
|
|
|
|
DocumentChild::CommentEnd(v) => v.build(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl BuildXML for Document {
|
2019-11-07 09:08:59 +02:00
|
|
|
fn build(&self) -> Vec<u8> {
|
2020-12-18 09:09:48 +02:00
|
|
|
XMLBuilder::new()
|
2019-11-12 12:21:08 +02:00
|
|
|
.declaration(Some(true))
|
2019-11-07 10:31:04 +02:00
|
|
|
.open_document()
|
2020-12-18 09:09:48 +02:00
|
|
|
.open_body()
|
|
|
|
.add_children(&self.children)
|
|
|
|
.add_child(&self.section_property)
|
|
|
|
.close()
|
|
|
|
.close()
|
|
|
|
.build()
|
2019-11-07 09:08:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
2019-11-11 07:39:22 +02:00
|
|
|
use super::super::Run;
|
2019-11-07 09:08:59 +02:00
|
|
|
use super::*;
|
|
|
|
#[cfg(test)]
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_document() {
|
2019-11-07 10:31:04 +02:00
|
|
|
let b = Document::new()
|
2019-11-13 09:51:58 +02:00
|
|
|
.add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello")))
|
2019-11-07 10:31:04 +02:00
|
|
|
.build();
|
2019-11-07 09:08:59 +02:00
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2019-11-07 10:31:04 +02:00
|
|
|
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
2020-08-13 19:57:59 +03:00
|
|
|
<w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 wp14">
|
2021-11-25 16:33:13 +02:00
|
|
|
<w:body><w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r></w:p><w:sectPr><w:pgSz w:w="11906" w:h="16838" /><w:pgMar w:top="1985" w:right="1701" w:bottom="1701" w:left="1701" w:header="851" w:footer="992" w:gutter="0" /><w:cols w:space="425" /><w:docGrid w:type="lines" w:linePitch="360" /></w:sectPr></w:body>
|
2019-11-07 09:08:59 +02:00
|
|
|
</w:document>"#
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|