Skip to main content

docx_rs/documents/elements/
zoom.rs

1use crate::documents::BuildXML;
2use crate::xml_builder::*;
3use std::io::Write;
4
5use serde::{Serialize, Serializer};
6
7#[derive(Debug, Clone, PartialEq)]
8pub struct Zoom {
9    val: usize,
10}
11
12impl Zoom {
13    pub fn new(val: usize) -> Zoom {
14        Zoom { val }
15    }
16}
17
18impl BuildXML for Zoom {
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)
24            .zoom(&format!("{}", self.val))?
25            .into_inner()
26    }
27}
28
29impl Serialize for Zoom {
30    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
31    where
32        S: Serializer,
33    {
34        serializer.serialize_u64(self.val as u64)
35    }
36}
37
38#[cfg(test)]
39mod tests {
40
41    use super::*;
42    #[cfg(test)]
43    use pretty_assertions::assert_eq;
44    use std::str;
45
46    #[test]
47    fn test_zoom() {
48        let c = Zoom::new(20);
49        let b = c.build();
50        assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:zoom w:percent="20" />"#);
51    }
52}