docx_rust/document/
text.rs1use hard_xml::{XmlRead, XmlWrite};
2use std::borrow::Cow;
3
4use crate::{__string_enum, __xml_test_suites};
5
6#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
18#[cfg_attr(test, derive(PartialEq))]
19#[xml(tag = "w:t")]
20pub struct Text<'a> {
21 #[xml(attr = "xml:space")]
23 pub space: Option<TextSpace>,
24 #[xml(text)]
26 pub text: Cow<'a, str>,
27}
28
29impl From<String> for Text<'_> {
30 fn from(val: String) -> Self {
31 Text {
32 text: val.into(),
33 space: None,
34 }
35 }
36}
37
38impl<'a> From<&'a str> for Text<'a> {
39 fn from(val: &'a str) -> Self {
40 Text {
41 text: val.into(),
42 space: None,
43 }
44 }
45}
46
47impl From<(String, TextSpace)> for Text<'_> {
48 fn from(val: (String, TextSpace)) -> Self {
49 Text {
50 text: val.0.into(),
51 space: Some(val.1),
52 }
53 }
54}
55
56impl<'a> From<(&'a str, TextSpace)> for Text<'a> {
57 fn from(val: (&'a str, TextSpace)) -> Self {
58 Text {
59 text: val.0.into(),
60 space: Some(val.1),
61 }
62 }
63}
64
65#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
66#[cfg_attr(test, derive(PartialEq))]
67#[xml(tag = "w:delText")]
68pub struct DelText<'a> {
69 #[xml(attr = "xml:space")]
71 pub space: Option<TextSpace>,
72 #[xml(text)]
74 pub text: Cow<'a, str>,
75}
76
77impl From<String> for DelText<'_> {
78 fn from(val: String) -> Self {
79 DelText {
80 text: val.into(),
81 space: None,
82 }
83 }
84}
85
86impl<'a> From<&'a str> for DelText<'a> {
87 fn from(val: &'a str) -> Self {
88 DelText {
89 text: val.into(),
90 space: None,
91 }
92 }
93}
94
95impl From<(String, TextSpace)> for DelText<'_> {
96 fn from(val: (String, TextSpace)) -> Self {
97 DelText {
98 text: val.0.into(),
99 space: Some(val.1),
100 }
101 }
102}
103
104impl<'a> From<(&'a str, TextSpace)> for DelText<'a> {
105 fn from(val: (&'a str, TextSpace)) -> Self {
106 DelText {
107 text: val.0.into(),
108 space: Some(val.1),
109 }
110 }
111}
112
113#[derive(Debug, Clone)]
117#[cfg_attr(test, derive(PartialEq))]
118pub enum TextSpace {
119 Default,
121 Preserve,
123}
124
125__string_enum! {
126 TextSpace {
127 Default = "default",
128 Preserve = "preserve",
129 }
130}
131
132__xml_test_suites!(
133 Text,
134 Text::from("text"),
135 "<w:t>text</w:t>",
136 Text::from(String::from("text")),
137 "<w:t>text</w:t>",
138 Text::from(("text", TextSpace::Preserve)),
139 r#"<w:t xml:space="preserve">text</w:t>"#,
140 Text::from((String::from("text"), TextSpace::Default)),
141 r#"<w:t xml:space="default">text</w:t>"#,
142);