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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! A Rust library for parsing and generating docx files.
//!
//! # Create a new document
//!
//! Use `Docx::default` to create a new empty `Docx`, then use
//! [`Docx::write_file`] for saving it to a file.
//!
//! [`Docx::write_file`]: struct.Docx.html#method.write_file
//!
//! ```no_run
//! use docx_rust::document::Paragraph;
//! use docx_rust::Docx;
//!
//! let mut docx = Docx::default();
//!
//! // create a new paragraph and insert it
//! let para = Paragraph::default().push_text("Lorem Ipsum");
//! docx.document.push(para);
//!
//! docx.write_file("demo.docx").unwrap();
//! ```
//!
//! Also see: [`Docx::write`].
//!
//! [`Docx::write`]: struct.Docx.html#method.write
//!
//! # Reading from files
//!
//! Use [`DocxFile::from_file`] to extract content from docx files, then use
//! [`DocxFile::parse`] to generate a `Docx` struct.
//!
//! [`DocxFile::from_file`]: struct.DocxFile.html#method.from_file
//! [`DocxFile::parse`]: struct.DocxFile.html#method.parse
//!
//! ```no_run
//! use docx_rust::document::Paragraph;
//! use docx_rust::DocxFile;
//!
//! let docx = DocxFile::from_file("origin.docx").unwrap();
//! let mut docx = docx.parse().unwrap();
//!
//! let para = Paragraph::default().push_text("Lorem Ipsum");
//! docx.document.push(para);
//!
//! docx.write_file("origin_appended.docx").unwrap();
//! ```
//!
//! To reduce allocations, `DocxFile::parse` returns a `Docx` struct contains
//! references to `DocxFile` itself. It means you have to make sure that
//! `DocxFile` lives as long as its returned `Docx`:
//!
//! ```compile_fail
//! use docx_rust::DocxFile;
//!
//! let mut docx_option = None;
//! {
//!     let docx_file = DocxFile::from_file("foo.docx").unwrap();
//!     let mut docx = docx_file.parse().unwrap();
//!     docx_option = Some(docx);
//!     // `docx_file` gets dropped here and code fails to compile
//! }
//! docx_option.unwrap().write_file("foo.docx").unwrap();
//! ```
//!
//! Also see: [`DocxFile::from_reader`].
//!
//! [`DocxFile::from_reader`]: struct.DocxFile.html#method.from_reader
//!
//! # Similar Projects
//!
//! [`bokuweb/docx-rs`]: A .docx file writer with Rust/WebAssembly.
//!
//! [`bokuweb/docx-rs`]: https://github.com/bokuweb/docx-rs
//!
//! # License
//!
//! MIT
//!

mod macros;

pub mod app;
pub mod content_type;
pub mod core;
pub mod document;
mod docx;
mod error;
pub mod font_table;
pub mod formatting;
pub mod media;
pub mod rels;
mod schema;
pub mod settings;
pub mod styles;
pub mod web_settings;

use std::io::Write;

use strong_xml::{XmlWrite, XmlWriter};

pub use crate::docx::{Docx, DocxFile};
pub use crate::error::{DocxError, DocxResult};

pub fn write_attr<W: Write, T: XmlWrite>(
    element: &Option<T>,
    writer: &mut XmlWriter<W>,
) -> Result<(), strong_xml::XmlError> {
    if let Some(e) = element {
        e.to_writer(writer)?;
    };
    Ok(())
}