docx_rust/styles/
mod.rs

1//! Style Definitions
2//!
3//! The corresponding ZIP item is `/word/styles.xml`.
4
5mod default_style;
6mod latent_style;
7mod latent_styles;
8mod priority;
9mod semi_hidden;
10mod style;
11mod unhidden_when_used;
12
13use self::latent_styles::LatentStyles;
14pub use self::{default_style::*, style::*};
15
16use hard_xml::{XmlRead, XmlResult, XmlWrite, XmlWriter};
17use std::io::Write;
18
19use crate::schema::{SCHEMA_MAIN, SCHEMA_XML};
20use crate::{__xml_test_suites, write_attr};
21
22/// Styles of the document
23///
24/// Styles are predefined sets of properties which can be applied to text.
25///
26/// ```rust
27/// use docx_rust::styles::*;
28///
29/// let style = Styles::new()
30///     .default(DefaultStyle::default())
31///     .push(Style::new(StyleType::Paragraph, "style_id"));
32/// ```
33#[derive(Debug, Default, XmlRead, Clone)]
34#[cfg_attr(test, derive(PartialEq))]
35#[xml(tag = "w:styles")]
36pub struct Styles<'a> {
37    /// Specifies the default set of properties.
38    #[xml(child = "w:docDefaults")]
39    pub default: Option<DefaultStyle<'a>>,
40    #[xml(child = "w:latentStyles")]
41    pub latent_styles: Option<LatentStyles<'a>>,
42    /// Specifies a set of properties.
43    #[xml(child = "w:style")]
44    pub styles: Vec<Style<'a>>,
45}
46
47impl<'a> XmlWrite for Styles<'a> {
48    fn to_writer<W: Write>(&self, writer: &mut XmlWriter<W>) -> XmlResult<()> {
49        let Styles {
50            default,
51            latent_styles,
52            styles,
53        } = self;
54
55        log::debug!("[Styles] Started writing.");
56
57        let _ = write!(writer.inner, "{}", SCHEMA_XML);
58        writer.write_element_start("w:styles")?;
59
60        writer.write_attribute("xmlns:w", SCHEMA_MAIN)?;
61
62        writer.write_element_end_open()?;
63
64        write_attr(default, writer)?;
65        write_attr(latent_styles, writer)?;
66
67        for ele in styles {
68            ele.to_writer(writer)?;
69        }
70
71        writer.write_element_end_close("w:styles")?;
72
73        log::debug!("[Styles] Finished writing.");
74
75        Ok(())
76    }
77}
78
79impl<'a> Styles<'a> {
80    pub fn new() -> Self {
81        <Styles as Default>::default()
82    }
83
84    pub fn default(&mut self, style: DefaultStyle<'a>) -> &mut Self {
85        self.default = Some(style);
86        self
87    }
88
89    pub fn push(&mut self, style: Style<'a>) -> &mut Self {
90        self.styles.push(style);
91        self
92    }
93}
94
95__xml_test_suites!(
96    Styles,
97    Styles::new(),
98    format!(
99        r#"{}<w:styles xmlns:w="{}"></w:styles>"#,
100        SCHEMA_XML, SCHEMA_MAIN
101    )
102    .as_str(),
103    Styles {
104        styles: vec![Style::new(crate::styles::StyleType::Paragraph, "id")],
105        ..Default::default()
106    },
107    format!(
108        r#"{}<w:styles xmlns:w="{}"><w:style w:type="paragraph" w:styleId="id"/></w:styles>"#,
109        SCHEMA_XML, SCHEMA_MAIN
110    )
111    .as_str(),
112);