docx_rs/documents/elements/
numbering_property.rs

1use serde::ser::{SerializeStruct, Serializer};
2use serde::Serialize;
3use std::io::Write;
4
5use super::{IndentLevel, NumberingId};
6use crate::documents::BuildXML;
7use crate::xml_builder::*;
8
9#[derive(Debug, Clone, PartialEq, Default)]
10pub struct NumberingProperty {
11    pub id: Option<NumberingId>,
12    pub level: Option<IndentLevel>,
13}
14
15impl NumberingProperty {
16    pub fn new() -> NumberingProperty {
17        Default::default()
18    }
19
20    pub fn id(mut self, id: NumberingId) -> NumberingProperty {
21        self.id = Some(id);
22        self
23    }
24
25    pub fn add_num(mut self, id: NumberingId, level: IndentLevel) -> NumberingProperty {
26        self.id = Some(id);
27        self.level = Some(level);
28        self
29    }
30}
31
32impl BuildXML for NumberingProperty {
33    fn build_to<W: Write>(
34        &self,
35        stream: xml::writer::EventWriter<W>,
36    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
37        XMLBuilder::from(stream)
38            .open_numbering_property()?
39            .add_optional_child(&self.id)?
40            .add_optional_child(&self.level)?
41            .close()?
42            .into_inner()
43    }
44}
45
46impl Serialize for NumberingProperty {
47    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
48    where
49        S: Serializer,
50    {
51        let mut t = serializer.serialize_struct("NumberProperty", 2)?;
52        let mut id: Option<usize> = None;
53        if let Some(n) = &self.id {
54            id = Some(n.id);
55        }
56        t.serialize_field("id", &id)?;
57
58        let mut level: Option<usize> = None;
59        if let Some(n) = &self.level {
60            level = Some(n.val);
61        }
62        t.serialize_field("level", &level)?;
63        t.end()
64    }
65}
66
67#[cfg(test)]
68mod tests {
69
70    use super::*;
71    #[cfg(test)]
72    use pretty_assertions::assert_eq;
73    use std::str;
74
75    #[test]
76    fn test_num_property() {
77        let c = NumberingProperty::new().add_num(NumberingId::new(0), IndentLevel::new(3));
78        let b = c.build();
79        assert_eq!(
80            str::from_utf8(&b).unwrap(),
81            r#"<w:numPr><w:numId w:val="0" /><w:ilvl w:val="3" /></w:numPr>"#
82        );
83    }
84
85    #[test]
86    fn test_empty_num_property() {
87        let c = NumberingProperty::new();
88        let b = c.build();
89        assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:numPr />"#);
90    }
91
92    #[test]
93    fn test_num_property_json() {
94        let c = NumberingProperty::new().add_num(NumberingId::new(0), IndentLevel::new(3));
95        assert_eq!(serde_json::to_string(&c).unwrap(), r#"{"id":0,"level":3}"#);
96    }
97
98    #[test]
99    fn test_empty_num_property_json() {
100        let c = NumberingProperty::new();
101        assert_eq!(
102            serde_json::to_string(&c).unwrap(),
103            r#"{"id":null,"level":null}"#
104        );
105    }
106}