2019-12-13 05:58:01 +02:00
|
|
|
use super::{Paragraph, SectionProperty, Table};
|
2019-11-07 09:08:59 +02:00
|
|
|
use crate::documents::BuildXML;
|
|
|
|
use crate::xml_builder::*;
|
|
|
|
|
2019-11-07 11:45:03 +02:00
|
|
|
#[derive(Debug)]
|
2019-12-08 21:14:27 +02:00
|
|
|
pub struct Document {
|
|
|
|
pub(crate) children: Vec<DocumentChild>,
|
2019-12-13 05:58:01 +02:00
|
|
|
pub section_property: SectionProperty,
|
2019-11-12 12:21:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2019-12-08 21:14:27 +02:00
|
|
|
pub enum DocumentChild {
|
|
|
|
Paragraph(Paragraph),
|
|
|
|
Table(Table),
|
2019-11-07 10:31:04 +02:00
|
|
|
}
|
2019-11-07 09:08:59 +02:00
|
|
|
|
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-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-11-13 09:51:58 +02:00
|
|
|
self.children.push(DocumentChild::Table(t));
|
2019-11-07 10:31:04 +02:00
|
|
|
self
|
|
|
|
}
|
2019-11-07 09:08:59 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl Default for Document {
|
2019-11-07 09:08:59 +02:00
|
|
|
fn default() -> Self {
|
2019-11-07 10:31:04 +02:00
|
|
|
Self {
|
2019-11-12 12:21:08 +02:00
|
|
|
children: Vec::new(),
|
2019-12-13 05:58:01 +02:00
|
|
|
section_property: SectionProperty::new(),
|
2019-11-07 10:31:04 +02:00
|
|
|
}
|
2019-11-07 09:08:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
2019-11-12 12:21:08 +02:00
|
|
|
let mut b = XMLBuilder::new()
|
|
|
|
.declaration(Some(true))
|
2019-11-07 10:31:04 +02:00
|
|
|
.open_document()
|
2019-11-12 12:21:08 +02:00
|
|
|
.open_body();
|
|
|
|
for c in &self.children {
|
|
|
|
match c {
|
2019-11-13 09:51:58 +02:00
|
|
|
DocumentChild::Paragraph(p) => b = b.add_child(p),
|
|
|
|
DocumentChild::Table(t) => b = b.add_child(t),
|
2019-11-12 12:21:08 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-13 05:58:01 +02:00
|
|
|
b.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"?>
|
|
|
|
<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" mc:Ignorable="w14 wp14">
|
2019-12-13 05:58:01 +02:00
|
|
|
<w:body><w:p><w:pPr><w:pStyle w:val="Normal" /><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>"#
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|