docx-rs/docx-core/src/documents/document.rs

79 lines
2.6 KiB
Rust
Raw Normal View History

2019-11-12 12:21:08 +02:00
use super::{Paragraph, 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-11-15 11:15:43 +02:00
pub struct Document<'a> {
2019-12-05 08:44:18 +02:00
pub(crate) children: Vec<DocumentChild<'a>>,
2019-11-12 12:21:08 +02:00
}
#[derive(Debug, Clone)]
2019-11-15 11:15:43 +02:00
pub enum DocumentChild<'a> {
Paragraph(Paragraph<'a>),
Table(Table<'a>),
2019-11-07 10:31:04 +02:00
}
2019-11-07 09:08:59 +02:00
2019-11-15 11:15:43 +02:00
impl<'a> Document<'a> {
pub fn new() -> Document<'a> {
2019-11-07 09:08:59 +02:00
Default::default()
}
2019-11-07 10:31:04 +02:00
2019-11-15 11:15:43 +02:00
pub fn add_paragraph(mut self, p: Paragraph<'a>) -> 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-11-15 11:15:43 +02:00
pub fn add_table(mut self, t: Table<'a>) -> 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-11-15 11:15:43 +02:00
impl<'a> Default for Document<'a> {
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-11-07 10:31:04 +02:00
}
2019-11-07 09:08:59 +02:00
}
}
2019-11-15 11:15:43 +02:00
impl<'a> BuildXML for Document<'a> {
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
}
}
b.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-11-12 06:33:45 +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:body>
2019-11-07 09:08:59 +02:00
</w:document>"#
);
}
}