docx_rs/documents/elements/
outline_lvl.rs

1use crate::documents::BuildXML;
2use crate::xml_builder::*;
3use std::io::Write;
4
5use serde::*;
6
7#[derive(Debug, Clone, PartialEq)]
8pub struct OutlineLvl {
9    pub v: usize,
10}
11
12impl OutlineLvl {
13    pub fn new(v: usize) -> OutlineLvl {
14        assert!(v < 10, "outline level should be less than 10");
15        OutlineLvl { v }
16    }
17}
18
19impl BuildXML for OutlineLvl {
20    fn build_to<W: Write>(
21        &self,
22        stream: xml::writer::EventWriter<W>,
23    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
24        XMLBuilder::from(stream)
25            .outline_lvl(self.v)?
26            // .close()?
27            .into_inner()
28    }
29}
30
31impl Serialize for OutlineLvl {
32    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
33    where
34        S: Serializer,
35    {
36        serializer.serialize_u32(self.v as u32)
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use crate::{BuildXML, OutlineLvl};
43
44    #[test]
45    fn test_outline_lvl_build() {
46        let bytes = OutlineLvl::new(1).build();
47        assert_eq!(
48            std::str::from_utf8(&bytes).unwrap(),
49            r#"<w:outlineLvl w:val="1" />"#
50        );
51    }
52}