Skip to main content

activitystreams/primitives/
xsd_string.rs

1/*
2 * This file is part of ActivityStreams.
3 *
4 * Copyright © 2020 Riley Trautman
5 *
6 * ActivityStreams is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ActivityStreams is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with ActivityStreams.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/// A string type that conforms to the xsd:string specification.
21///
22/// TODO: Escape `<` and `&` when converting
23///
24/// The type xsd:string represents a character string that may contain any Unicode character
25/// allowed by XML. Certain characters, namely the "less than" symbol (<) and the ampersand (&),
26/// must be escaped (using the entities &lt; and &amp;, respectively) when used in strings in XML
27/// instances.
28///
29/// The xsd:string type has a whiteSpace facet of preserve, which means that all whitespace
30/// characters (spaces, tabs, carriage returns, and line feeds) are preserved by the processor.
31/// This is in contrast to two types derived from it: normalizedString, and token.
32#[derive(
33    Clone,
34    Debug,
35    Default,
36    Eq,
37    Hash,
38    Ord,
39    PartialEq,
40    PartialOrd,
41    serde::Deserialize,
42    serde::Serialize,
43)]
44#[serde(transparent)]
45pub struct XsdString(String);
46
47impl XsdString {
48    /// Get an XsdString from a String
49    pub fn from_string(s: String) -> Self {
50        s.into()
51    }
52
53    /// Borrow an &str from an XsdString
54    pub fn as_str(&self) -> &str {
55        self.as_ref()
56    }
57
58    /// Consume the XsdString and get a String
59    pub fn into_string(self) -> String {
60        self.into()
61    }
62}
63
64impl std::str::FromStr for XsdString {
65    type Err = std::convert::Infallible;
66
67    fn from_str(s: &str) -> Result<Self, Self::Err> {
68        Ok(s.into())
69    }
70}
71
72impl From<String> for XsdString {
73    fn from(s: String) -> Self {
74        XsdString(s)
75    }
76}
77
78impl From<&str> for XsdString {
79    fn from(s: &str) -> Self {
80        XsdString(s.to_owned())
81    }
82}
83
84impl From<&mut str> for XsdString {
85    fn from(s: &mut str) -> Self {
86        XsdString(s.to_owned())
87    }
88}
89
90impl From<XsdString> for String {
91    fn from(s: XsdString) -> Self {
92        s.0
93    }
94}
95
96impl AsRef<str> for XsdString {
97    fn as_ref(&self) -> &str {
98        &self.0
99    }
100}
101
102impl AsRef<String> for XsdString {
103    fn as_ref(&self) -> &String {
104        &self.0
105    }
106}
107
108impl AsMut<str> for XsdString {
109    fn as_mut(&mut self) -> &mut str {
110        &mut self.0
111    }
112}
113
114impl AsMut<String> for XsdString {
115    fn as_mut(&mut self) -> &mut String {
116        &mut self.0
117    }
118}
119
120impl std::fmt::Display for XsdString {
121    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
122        std::fmt::Display::fmt(&self.0, f)
123    }
124}