docx_rs/documents/elements/
run_style.rs

1use serde::{Serialize, Serializer};
2use std::io::Write;
3
4use crate::documents::BuildXML;
5use crate::escape::escape;
6use crate::xml_builder::*;
7
8#[derive(Debug, Clone, PartialEq)]
9pub struct RunStyle {
10    pub val: String,
11}
12
13impl Default for RunStyle {
14    fn default() -> Self {
15        RunStyle {
16            val: "Normal".to_owned(),
17        }
18    }
19}
20
21impl RunStyle {
22    pub fn new(val: impl Into<String>) -> RunStyle {
23        RunStyle {
24            val: escape(&val.into()),
25        }
26    }
27}
28
29impl BuildXML for RunStyle {
30    fn build_to<W: Write>(
31        &self,
32        stream: xml::writer::EventWriter<W>,
33    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
34        XMLBuilder::from(stream).run_style(&self.val)?.into_inner()
35    }
36}
37
38impl Serialize for RunStyle {
39    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
40    where
41        S: Serializer,
42    {
43        serializer.serialize_str(&self.val)
44    }
45}
46
47#[cfg(test)]
48mod tests {
49
50    use super::*;
51    #[cfg(test)]
52    use pretty_assertions::assert_eq;
53    use std::str;
54
55    #[test]
56    fn test_r_style() {
57        let c = RunStyle::new("Heading");
58        let b = c.build();
59        assert_eq!(
60            str::from_utf8(&b).unwrap(),
61            r#"<w:rStyle w:val="Heading" />"#
62        );
63    }
64}