docx_rs/documents/elements/
justification.rs

1use serde::{Serialize, Serializer};
2use std::io::Write;
3
4use crate::documents::BuildXML;
5use crate::xml_builder::*;
6
7// 22.1.2.51
8// jc (Justification)
9// This element specifies justification of the math paragraph (a series of adjacent instances of mathematical text
10// within the same paragraph). A math paragraph can be Left Justified, Right Justified, Centered, or Centered as
11// Group. If this element is omitted, the math paragraph is Centered as Group. Whether the element is absent or
12// present without the val attribute, the default of the val attribute is centerGroup . This means that the instances
13// of mathematical text can be aligned with respect to each other, but the entire group of mathematical text is
14// centered as a whole.
15#[derive(Debug, Clone, PartialEq)]
16pub struct Justification {
17    pub val: String,
18}
19
20impl Justification {
21    pub fn new(val: impl Into<String>) -> Justification {
22        Justification { val: val.into() }
23    }
24}
25
26impl BuildXML for Justification {
27    fn build_to<W: Write>(
28        &self,
29        stream: xml::writer::EventWriter<W>,
30    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
31        XMLBuilder::from(stream)
32            .justification(&self.val)?
33            .into_inner()
34    }
35}
36
37impl Serialize for Justification {
38    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
39    where
40        S: Serializer,
41    {
42        serializer.serialize_str(&self.val)
43    }
44}
45
46#[cfg(test)]
47mod tests {
48
49    use super::*;
50    #[cfg(test)]
51    use pretty_assertions::assert_eq;
52    use std::str;
53
54    #[test]
55    fn test_justification() {
56        let c = Justification::new("start");
57        let b = c.build();
58        assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:jc w:val="start" />"#);
59    }
60}