docx_rs/documents/elements/
stretch.rs

1use crate::documents::BuildXML;
2use crate::xml_builder::*;
3use std::io::Write;
4
5use serde::*;
6
7#[derive(Debug, Clone, Deserialize, PartialEq)]
8pub struct Stretch {
9    value: i32,
10}
11
12impl Stretch {
13    pub fn new(s: i32) -> Stretch {
14        Self { value: s }
15    }
16}
17
18impl BuildXML for Stretch {
19    fn build_to<W: Write>(
20        &self,
21        stream: xml::writer::EventWriter<W>,
22    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
23        XMLBuilder::from(stream).w(self.value)?.into_inner()
24    }
25}
26
27impl Serialize for Stretch {
28    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
29    where
30        S: Serializer,
31    {
32        serializer.serialize_i32(self.value)
33    }
34}
35
36#[cfg(test)]
37mod tests {
38
39    use super::*;
40    #[cfg(test)]
41    use pretty_assertions::assert_eq;
42    use std::str;
43
44    #[test]
45    fn test_stretch() {
46        let b = Stretch::new(200).build();
47        assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:w w:val="200" />"#);
48    }
49
50    #[test]
51    fn test_stretch_json() {
52        let s = Stretch { value: 100 };
53        assert_eq!(serde_json::to_string(&s).unwrap(), r#"100"#);
54    }
55}