docx_rs/documents/elements/
fld_char.rs

1use serde::Serialize;
2use std::io::Write;
3
4use crate::documents::*;
5use crate::types::*;
6use crate::xml_builder::*;
7
8#[derive(Serialize, Debug, Clone, PartialEq)]
9#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
10#[cfg_attr(feature = "wasm", ts(export))]
11#[serde(rename_all = "camelCase")]
12pub struct FieldChar {
13    pub field_char_type: FieldCharType,
14    pub dirty: bool,
15}
16
17impl FieldChar {
18    pub fn new(t: FieldCharType) -> Self {
19        Self {
20            field_char_type: t,
21            dirty: false,
22        }
23    }
24
25    pub fn dirty(mut self) -> Self {
26        self.dirty = true;
27        self
28    }
29}
30
31impl BuildXML for FieldChar {
32    fn build_to<W: Write>(
33        &self,
34        stream: xml::writer::EventWriter<W>,
35    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
36        XMLBuilder::from(stream)
37            .field_character(
38                &format!("{}", self.field_char_type),
39                &format!("{}", &self.dirty),
40            )?
41            .into_inner()
42    }
43}
44
45#[cfg(test)]
46mod tests {
47
48    use super::*;
49    #[cfg(test)]
50    use pretty_assertions::assert_eq;
51    use std::str;
52
53    #[test]
54    fn test_field_character() {
55        let b = FieldChar::new(FieldCharType::Begin).dirty().build();
56        assert_eq!(
57            str::from_utf8(&b).unwrap(),
58            r#"<w:fldChar w:fldCharType="begin" w:dirty="true" />"#
59        );
60    }
61}