1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::borrow::Cow;
use strong_xml::{XmlRead, XmlWrite};

use crate::{__string_enum, __xml_test_suites};

/// Literal Text
///
/// A literal text that shall be displayed in the document.
///
/// ```rust
/// use docx::document::{Text, TextSpace};
///
/// let text = Text::from("text");
/// let text = Text::from(String::from("text"));
/// let text = Text::from(("text", TextSpace::Preserve));
/// ```
#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[xml(tag = "w:t")]
pub struct Text<'a> {
    /// Specifies how to handle whitespace
    #[xml(attr = "xml:space")]
    pub space: Option<TextSpace>,
    /// Specifies a literal text
    #[xml(text)]
    pub text: Cow<'a, str>,
}

impl From<String> for Text<'_> {
    fn from(val: String) -> Self {
        Text {
            text: val.into(),
            space: None,
        }
    }
}

impl<'a> From<&'a str> for Text<'a> {
    fn from(val: &'a str) -> Self {
        Text {
            text: val.into(),
            space: None,
        }
    }
}

impl From<(String, TextSpace)> for Text<'_> {
    fn from(val: (String, TextSpace)) -> Self {
        Text {
            text: val.0.into(),
            space: Some(val.1),
        }
    }
}

impl<'a> From<(&'a str, TextSpace)> for Text<'a> {
    fn from(val: (&'a str, TextSpace)) -> Self {
        Text {
            text: val.0.into(),
            space: Some(val.1),
        }
    }
}

/// Text Space Rules
///
/// Specifies how whitespace should be handled in a literal text.
#[derive(Debug, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub enum TextSpace {
    /// Default rules
    Default,
    /// Using the W3C space preservation rules
    Preserve,
}

__string_enum! {
    TextSpace {
        Default = "default",
        Preserve = "preserve",
    }
}

__xml_test_suites!(
    Text,
    Text::from("text"),
    "<w:t>text</w:t>",
    Text::from(String::from("text")),
    "<w:t>text</w:t>",
    Text::from(("text", TextSpace::Preserve)),
    r#"<w:t xml:space="preserve">text</w:t>"#,
    Text::from((String::from("text"), TextSpace::Default)),
    r#"<w:t xml:space="default">text</w:t>"#,
);