diff --git a/docx-core/src/documents/content_types.rs b/docx-core/src/documents/content_types.rs index 6770c9f..8775fac 100644 --- a/docx-core/src/documents/content_types.rs +++ b/docx-core/src/documents/content_types.rs @@ -55,6 +55,8 @@ impl BuildXML for ContentTypes { mod tests { use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; use std::str; #[test] diff --git a/docx-core/src/documents/elements/color.rs b/docx-core/src/documents/elements/color.rs new file mode 100644 index 0000000..36d5667 --- /dev/null +++ b/docx-core/src/documents/elements/color.rs @@ -0,0 +1,34 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +pub struct Color { + val: String, +} + +impl Color { + pub fn new(val: impl Into) -> Color { + Color { val: val.into() } + } +} + +impl BuildXML for Color { + fn build(&self) -> Vec { + XMLBuilder::new().color(&self.val).build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_build() { + let c = Color::new("FFFFFF"); + let b = c.build(); + assert_eq!(str::from_utf8(&b).unwrap(), r#""#); + } +} diff --git a/docx-core/src/documents/elements/doc_defaults.rs b/docx-core/src/documents/elements/doc_defaults.rs index c8334c6..294cc36 100644 --- a/docx-core/src/documents/elements/doc_defaults.rs +++ b/docx-core/src/documents/elements/doc_defaults.rs @@ -19,9 +19,8 @@ impl DocDefaults { impl BuildXML for DocDefaults { fn build(&self) -> Vec { let b = XMLBuilder::new(); - let run_property_default = self.run_property_default.build(); b.open_doc_defaults() - .add_child_buffer(&run_property_default) + .add_child(&self.run_property_default) .close() .build() } diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index 363f96b..45c41ce 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -1,4 +1,5 @@ mod based_on; +mod color; mod doc_defaults; mod name; mod next; @@ -10,6 +11,7 @@ mod style; mod sz; pub use based_on::*; +pub use color::*; pub use doc_defaults::*; pub use name::*; pub use next::*; diff --git a/docx-core/src/documents/elements/paragraph_property.rs b/docx-core/src/documents/elements/paragraph_property.rs index 29071ac..e1a9f52 100644 --- a/docx-core/src/documents/elements/paragraph_property.rs +++ b/docx-core/src/documents/elements/paragraph_property.rs @@ -25,6 +25,8 @@ impl BuildXML for ParagraphProperty { mod tests { use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; use std::str; #[test] diff --git a/docx-core/src/documents/elements/run_property.rs b/docx-core/src/documents/elements/run_property.rs index 3b704ff..d1df540 100644 --- a/docx-core/src/documents/elements/run_property.rs +++ b/docx-core/src/documents/elements/run_property.rs @@ -1,32 +1,45 @@ -use super::Sz; +use super::{Color, Sz}; use crate::documents::BuildXML; use crate::xml_builder::*; pub struct RunProperty { sz: Option, + color: Option, } impl RunProperty { pub fn new() -> RunProperty { - RunProperty { sz: None } + Default::default() } - pub fn add_sz(mut self, sz: Sz) -> RunProperty { - self.sz = Some(sz); + pub fn add_sz(mut self, sz: usize) -> RunProperty { + self.sz = Some(Sz::new(sz)); self } + + pub fn add_color(mut self, color: &str) -> RunProperty { + self.color = Some(Color::new(color)); + self + } +} + +impl Default for RunProperty { + fn default() -> Self { + Self { + sz: None, + color: None, + } + } } impl BuildXML for RunProperty { fn build(&self) -> Vec { let b = XMLBuilder::new(); - let b = b.open_run_property(); - let b = if let Some(sz) = &self.sz { - b.add_child_buffer(&sz.build()) - } else { - b - }; - b.close().build() + b.open_run_property() + .add_optional_child(&self.sz) + .add_optional_child(&self.color) + .close() + .build() } } @@ -40,11 +53,11 @@ mod tests { #[test] fn test_build() { - let c = RunProperty::new().add_sz(Sz::new(10)); + let c = RunProperty::new().add_sz(10).add_color("FFFFFF"); let b = c.build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#""# + r#""# ); } } diff --git a/docx-core/src/documents/elements/run_property_default.rs b/docx-core/src/documents/elements/run_property_default.rs index 4d90e7d..9f509fb 100644 --- a/docx-core/src/documents/elements/run_property_default.rs +++ b/docx-core/src/documents/elements/run_property_default.rs @@ -16,9 +16,8 @@ impl RunPropertyDefault { impl BuildXML for RunPropertyDefault { fn build(&self) -> Vec { let b = XMLBuilder::new(); - let run_property = self.run_property.build(); b.open_run_property_default() - .add_child_buffer(&run_property) + .add_child(&self.run_property) .close() .build() } @@ -28,6 +27,8 @@ impl BuildXML for RunPropertyDefault { mod tests { use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; use std::str; #[test] diff --git a/docx-core/src/documents/elements/style.rs b/docx-core/src/documents/elements/style.rs index 9e1acb6..f59eb4c 100644 --- a/docx-core/src/documents/elements/style.rs +++ b/docx-core/src/documents/elements/style.rs @@ -11,18 +11,14 @@ pub struct Style { paragraph_property: ParagraphProperty, } -impl Style { - pub fn new( - style_id: impl Into, - name: impl Into, - style_type: StyleType, - ) -> Style { - let name = Name::new(name.into()); +impl Default for Style { + fn default() -> Style { + let name = Name::new(""); let rpr = RunProperty::new(); let ppr = ParagraphProperty::new(); Style { - style_id: style_id.into(), - style_type, + style_id: "".to_owned(), + style_type: StyleType::Paragraph, name, run_property: rpr, paragraph_property: ppr, @@ -30,22 +26,34 @@ impl Style { } } +impl Style { + pub fn new( + style_id: impl Into, + name: impl Into, + style_type: StyleType, + ) -> Style { + let name = Name::new(name.into()); + let default = Default::default(); + Style { + style_id: style_id.into(), + style_type, + name, + ..default + } + } +} + impl BuildXML for Style { fn build(&self) -> Vec { let b = XMLBuilder::new(); - let name = self.name.build(); - let rpr = self.run_property.build(); - let ppr = self.paragraph_property.build(); - let based_on = BasedOn::new("Normal").build(); - let next = Next::new("Normal").build(); - let q_format = QFormat::new().build(); + // Set "Normal" as default if you need change these values please fix it b.open_style(self.style_type, &self.style_id) - .add_child_buffer(&name) - .add_child_buffer(&rpr) - .add_child_buffer(&ppr) - .add_child_buffer(&based_on) - .add_child_buffer(&next) - .add_child_buffer(&q_format) + .add_child(&self.name) + .add_child(&self.run_property) + .add_child(&self.paragraph_property) + .add_child(&BasedOn::new("Normal")) + .add_child(&Next::new("Normal")) + .add_child(&QFormat::new()) .close() .build() } diff --git a/docx-core/src/documents/mod.rs b/docx-core/src/documents/mod.rs index 0903d2c..894d7d2 100644 --- a/docx-core/src/documents/mod.rs +++ b/docx-core/src/documents/mod.rs @@ -3,14 +3,17 @@ mod content_types; mod doc_props; mod elements; mod rels; +mod styles; mod xml_document; pub(crate) use build_xml::*; +use crate::xml_builder::*; use content_types::*; use doc_props::*; use elements::*; use rels::*; +use styles::*; pub(crate) struct Document { content_type: ContentTypes, diff --git a/docx-core/src/documents/rels.rs b/docx-core/src/documents/rels.rs index 01508bf..7593718 100644 --- a/docx-core/src/documents/rels.rs +++ b/docx-core/src/documents/rels.rs @@ -39,6 +39,8 @@ mod tests { use super::*; use std::str; + #[cfg(test)] + use pretty_assertions::assert_eq; #[test] fn test_build() { diff --git a/docx-core/src/documents/styles.rs b/docx-core/src/documents/styles.rs new file mode 100644 index 0000000..88d7aa5 --- /dev/null +++ b/docx-core/src/documents/styles.rs @@ -0,0 +1,59 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +use super::{DocDefaults, Style, StyleType}; + +pub struct Styles { + doc_defaults: DocDefaults, + styles: Vec