docx_rs/documents/elements/
wp_anchor.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 WpAnchor {
11    pub children: Vec<AGraphic>,
12}
13
14/*
15  20.4.2.3
16  anchor (WpAnchor for Floating DrawingML Object)
17  This element specifies that the DrawingML object located at this position in the document is a floating object.
18  Within a WordprocessingML document, drawing objects can exist in two states:
19  - Inline - The drawing object is in line with the text, and affects the line height and layout of its line (like a
20  - character glyph of similar size).
21  Floating - The drawing object is anchored within the text, but can be absolutely positioned in the
22  document relative to the page.
23  When this element encapsulates the DrawingML object's i
24*/
25impl WpAnchor {
26    pub fn new() -> WpAnchor {
27        Default::default()
28    }
29
30    pub fn add_graphic(mut self, g: AGraphic) -> WpAnchor {
31        self.children.push(g);
32        self
33    }
34}
35
36impl BuildXML for WpAnchor {
37    fn build_to<W: Write>(
38        &self,
39        stream: xml::writer::EventWriter<W>,
40    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
41        XMLBuilder::from(stream)
42            .open_anchor()?
43            .add_children(&self.children)?
44            .close()?
45            .into_inner()
46    }
47}
48
49#[cfg(test)]
50mod tests {
51
52    use super::*;
53    #[cfg(test)]
54    use pretty_assertions::assert_eq;
55    use std::str;
56
57    #[test]
58    fn test_anchor_build() {
59        let b = WpAnchor::new().build();
60        assert_eq!(str::from_utf8(&b).unwrap(), r#"<wp:anchor />"#);
61    }
62}