docx_rs/documents/elements/
a_graphic.rs

1use super::*;
2use serde::Serialize;
3use std::io::Write;
4
5use crate::documents::BuildXML;
6use crate::xml_builder::*;
7
8#[derive(Debug, Clone, Serialize, PartialEq, Default)]
9#[serde(rename_all = "camelCase")]
10pub struct AGraphic {
11    pub children: Vec<AGraphicData>,
12}
13
14impl AGraphic {
15    pub fn new() -> AGraphic {
16        Default::default()
17    }
18
19    pub fn add_graphic_data(mut self, g: AGraphicData) -> Self {
20        self.children.push(g);
21        self
22    }
23}
24
25impl BuildXML for AGraphic {
26    fn build_to<W: Write>(
27        &self,
28        stream: xml::writer::EventWriter<W>,
29    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
30        XMLBuilder::from(stream)
31            .open_graphic("http://schemas.openxmlformats.org/drawingml/2006/main")?
32            .add_children(&self.children)?
33            .close()?
34            .into_inner()
35    }
36}
37
38#[cfg(test)]
39mod tests {
40
41    use super::*;
42    #[cfg(test)]
43    use pretty_assertions::assert_eq;
44
45    #[test]
46    fn test_a_graphic_with_textbox_json() {
47        let graphic =
48            AGraphic::new().add_graphic_data(
49                AGraphicData::new(GraphicDataType::WpShape).add_shape(
50                    WpsShape::new().add_text_box(WpsTextBox::new().add_content(
51                        TextBoxContent::new().add_paragraph(
52                            Paragraph::new().add_run(Run::new().add_text("pattern1")),
53                        ),
54                    )),
55                ),
56            );
57        assert_eq!(
58            serde_json::to_string(&graphic).unwrap(),
59            r#"{"children":[{"dataType":"wpShape","children":[{"type":"shape","data":{"children":[{"type":"textbox","data":{"children":[{"children":[{"type":"paragraph","data":{"id":"12345678","children":[{"type":"run","data":{"runProperty":{},"children":[{"type":"text","data":{"preserveSpace":true,"text":"pattern1"}}]}}],"property":{"runProperty":{},"tabs":[]},"hasNumbering":false}}],"has_numbering":false}],"hasNumbering":false}}]}}]}]}"#,
60        );
61    }
62}