docx_rust/document/
text.rs

1use hard_xml::{XmlRead, XmlWrite};
2use std::borrow::Cow;
3
4use crate::{__string_enum, __xml_test_suites};
5
6/// Literal Text
7///
8/// A literal text that shall be displayed in the document.
9///
10/// ```rust
11/// use docx_rust::document::{Text, TextSpace};
12///
13/// let text = Text::from("text");
14/// let text = Text::from(String::from("text"));
15/// let text = Text::from(("text", TextSpace::Preserve));
16/// ```
17#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
18#[cfg_attr(test, derive(PartialEq))]
19#[xml(tag = "w:t")]
20pub struct Text<'a> {
21    /// Specifies how to handle whitespace
22    #[xml(attr = "xml:space")]
23    pub space: Option<TextSpace>,
24    /// Specifies a literal text
25    #[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    /// Specifies how to handle whitespace
70    #[xml(attr = "xml:space")]
71    pub space: Option<TextSpace>,
72    /// Specifies a literal text
73    #[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/// Text Space Rules
114///
115/// Specifies how whitespace should be handled in a literal text.
116#[derive(Debug, Clone)]
117#[cfg_attr(test, derive(PartialEq))]
118pub enum TextSpace {
119    /// Default rules
120    Default,
121    /// Using the W3C space preservation rules
122    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);