Skip to main content

docx_rs/documents/elements/
fit_text.rs

1use crate::documents::BuildXML;
2use crate::xml_builder::*;
3use serde::Serialize;
4use std::io::Write;
5
6#[derive(Debug, Clone, PartialEq, Serialize)]
7#[serde(rename_all = "camelCase")]
8pub struct FitText {
9    pub val: usize,
10    #[serde(skip_serializing_if = "Option::is_none")]
11    pub id: Option<u32>,
12}
13
14impl FitText {
15    pub fn new(val: usize) -> Self {
16        Self { val, id: None }
17    }
18
19    pub fn id(mut self, id: u32) -> Self {
20        self.id = Some(id);
21        self
22    }
23}
24
25impl BuildXML for FitText {
26    fn build_to<W: Write>(
27        &self,
28        stream: crate::xml::writer::EventWriter<W>,
29    ) -> crate::xml::writer::Result<crate::xml::writer::EventWriter<W>> {
30        XMLBuilder::from(stream)
31            .fit_text(self.val, self.id)?
32            .into_inner()
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_fit_text() {
46        let b = FitText::new(840).id(1266434317).build();
47        assert_eq!(
48            str::from_utf8(&b).unwrap(),
49            r#"<w:fitText w:val="840" w:id="1266434317" />"#
50        );
51    }
52
53    #[test]
54    fn test_fit_text_without_id() {
55        let b = FitText::new(840).build();
56        assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:fitText w:val="840" />"#);
57    }
58}