docx_rust/document/
sdt.rs

1#![allow(unused_must_use)]
2use std::borrow::Cow;
3
4use hard_xml::{XmlRead, XmlWrite};
5
6use crate::{__setter, __xml_test_suites};
7
8use super::BodyContent;
9
10/// SDT
11///
12// /// ```rust
13// /// use docx_rust::document::*;
14// /// use docx_rust::formatting::*;
15// ///
16// /// let tbl = SDT::default()
17// ///     .property(SDTProperty::default())
18// ///     .push_grid(vec![1, 2, 3])
19// ///     .push_grid(SDTGrid::default())
20// ///     .push_row(SDTRow::default());
21// /// ```
22#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
23#[cfg_attr(test, derive(PartialEq))]
24#[xml(tag = "w:sdt")]
25pub struct SDT<'a> {
26    #[xml(child = "w:sdtPr")]
27    pub property: Option<SDTProperty<'a>>,
28    #[xml(child = "w:sdtEndPr")]
29    pub end_property: Option<SDTEndProperty>,
30    #[xml(child = "w:sdtContent")]
31    pub content: Option<SDTContent<'a>>,
32}
33
34impl<'a> SDT<'a> {
35    __setter!(property: Option<SDTProperty<'a>>);
36    __setter!(end_property: Option<SDTEndProperty>);
37    __setter!(content: Option<SDTContent<'a>>);
38
39    pub fn iter_text(&self) -> Box<dyn Iterator<Item = &Cow<'a, str>> + '_> {
40        Box::new(
41            self.content
42                .as_ref()
43                .map(|content| content.iter_text())
44                .into_iter()
45                .flatten(),
46        )
47    }
48
49    pub fn text(&self) -> String {
50        self.iter_text()
51            .map(|c| c.to_string())
52            .collect::<Vec<_>>()
53            .join("")
54    }
55}
56
57/// Section Property
58///
59#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
60#[cfg_attr(test, derive(PartialEq))]
61#[xml(tag = "w:sdtPr")]
62pub struct SDTProperty<'a> {
63    #[xml(child = "w:id")]
64    pub id: Option<STDId>,
65    #[xml(child = "w:docPartObj")]
66    pub doc_part_obj: Option<DocPartObj<'a>>,
67}
68
69#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
70#[cfg_attr(test, derive(PartialEq))]
71#[xml(tag = "w:id")]
72pub struct STDId {
73    #[xml(attr = "w:val")]
74    pub id: Option<isize>,
75}
76
77#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
78#[cfg_attr(test, derive(PartialEq))]
79#[xml(tag = "w:docPartObj")]
80pub struct DocPartObj<'a> {
81    #[xml(child = "w:docPartGallery")]
82    pub doc_part_gallery: Option<DocPartGallery<'a>>,
83    #[xml(child = "w:docPartUnique")]
84    pub doc_part_unique: Option<DocPartUnique>,
85}
86
87#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
88#[cfg_attr(test, derive(PartialEq))]
89#[xml(tag = "w:docPartGallery")]
90pub struct DocPartGallery<'a> {
91    #[xml(attr = "w:val")]
92    pub name: Option<Cow<'a, str>>,
93}
94
95#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
96#[cfg_attr(test, derive(PartialEq))]
97#[xml(tag = "w:docPartUnique")]
98pub struct DocPartUnique {}
99
100#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
101#[cfg_attr(test, derive(PartialEq))]
102#[xml(tag = "w:sdtEndPr")]
103pub struct SDTEndProperty {}
104
105#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
106#[cfg_attr(test, derive(PartialEq))]
107#[xml(tag = "w:sdtContent")]
108pub struct SDTContent<'a> {
109    #[xml(
110        child = "w:p",
111        child = "w:tc",
112        child = "w:tbl",
113        child = "w:sectPr",
114        child = "w:sdt",
115        child = "w:r"
116    )]
117    pub content: Vec<BodyContent<'a>>,
118}
119
120impl<'a> SDTContent<'a> {
121    pub fn text(&self) -> String {
122        self.iter_text()
123            .map(|c| c.to_string())
124            .collect::<Vec<_>>()
125            .join("")
126    }
127
128    pub fn iter_text(&self) -> Box<dyn Iterator<Item = &Cow<'a, str>> + '_> {
129        Box::new(
130            self.content
131                .iter()
132                .filter_map(|content| match content {
133                    BodyContent::Paragraph(para) => Some(para.iter_text()),
134                    BodyContent::Table(_) => None,
135                    BodyContent::SectionProperty(_) => None,
136                    BodyContent::Sdt(sdt) => Some(sdt.iter_text()),
137                    BodyContent::TableCell(_) => None,
138                    BodyContent::Run(run) => Some(run.iter_text()),
139                })
140                .flatten(),
141        )
142    }
143}
144
145__xml_test_suites!(SDT, SDT::default(), "<w:sdt/>",);