1use alloc::borrow::{Cow, ToOwned};
2use alloc::string::{String, ToString};
3use core::borrow::Borrow;
4use core::fmt;
5use core::mem;
6use core::ops::Deref;
7use core::str::FromStr;
8
9#[cfg(feature = "serde")]
10use serde::{Deserialize, Serialize};
11
12use crate::{domain_check, node_check, resource_check};
13use crate::{BareJid, Error, Jid};
14
15macro_rules! def_part_parse_doc {
16 ($name:ident, $other:ident, $more:expr) => {
17 concat!(
18 "Parse a [`",
19 stringify!($name),
20 "`] from a `",
21 stringify!($other),
22 "`, copying its contents.\n",
23 "\n",
24 "If the given `",
25 stringify!($other),
26 "` does not conform to the restrictions imposed by `",
27 stringify!($name),
28 "`, an error is returned.\n",
29 $more,
30 )
31 };
32}
33
34macro_rules! def_part_into_inner_doc {
35 ($name:ident, $other:ident, $more:expr) => {
36 concat!(
37 "Consume the `",
38 stringify!($name),
39 "` and return the inner `",
40 stringify!($other),
41 "`.\n",
42 $more,
43 )
44 };
45}
46
47macro_rules! def_part_types {
48 (
49 $(#[$mainmeta:meta])*
50 pub struct $name:ident(String) use $check_fn:ident();
51
52 $(#[$refmeta:meta])*
53 pub struct ref $borrowed:ident(str);
54 ) => {
55 $(#[$mainmeta])*
56 #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
57 #[repr(transparent)]
58 pub struct $name(pub(crate) String);
59
60 impl $name {
61 #[doc = def_part_parse_doc!($name, str, "Depending on whether the contents are changed by normalisation operations, this function either returns a copy or a reference to the original data.")]
62 pub fn new(s: &str) -> Result<Cow<'_, $borrowed>, Error> {
63 let part = $check_fn(s)?;
64 match part {
65 Cow::Borrowed(v) => Ok(Cow::Borrowed($borrowed::from_str_unchecked(v))),
66 Cow::Owned(v) => Ok(Cow::Owned(Self(v))),
67 }
68 }
69
70 #[doc = def_part_into_inner_doc!($name, String, "")]
71 pub fn into_inner(self) -> String {
72 self.0
73 }
74 }
75
76 impl FromStr for $name {
77 type Err = Error;
78
79 fn from_str(s: &str) -> Result<Self, Error> {
80 Ok(Self::new(s)?.into_owned())
81 }
82 }
83
84 impl fmt::Display for $name {
85 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86 <$borrowed as fmt::Display>::fmt(Borrow::<$borrowed>::borrow(self), f)
87 }
88 }
89
90 impl Deref for $name {
91 type Target = $borrowed;
92
93 fn deref(&self) -> &Self::Target {
94 Borrow::<$borrowed>::borrow(self)
95 }
96 }
97
98 impl AsRef<$borrowed> for $name {
99 fn as_ref(&self) -> &$borrowed {
100 Borrow::<$borrowed>::borrow(self)
101 }
102 }
103
104 impl AsRef<String> for $name {
105 fn as_ref(&self) -> &String {
106 &self.0
107 }
108 }
109
110 impl Borrow<$borrowed> for $name {
111 fn borrow(&self) -> &$borrowed {
112 $borrowed::from_str_unchecked(self.0.as_str())
113 }
114 }
115
116 impl Borrow<String> for $name {
118 fn borrow(&self) -> &String {
119 &self.0
120 }
121 }
122
123 impl Borrow<str> for $name {
125 fn borrow(&self) -> &str {
126 self.0.as_str()
127 }
128 }
129
130 impl<'x> TryFrom<&'x str> for $name {
131 type Error = Error;
132
133 fn try_from(s: &str) -> Result<Self, Error> {
134 Self::from_str(s)
135 }
136 }
137
138 impl From<&$borrowed> for $name {
139 fn from(other: &$borrowed) -> Self {
140 other.to_owned()
141 }
142 }
143
144 impl<'x> From<Cow<'x, $borrowed>> for $name {
145 fn from(other: Cow<'x, $borrowed>) -> Self {
146 other.into_owned()
147 }
148 }
149
150 $(#[$refmeta])*
151 #[repr(transparent)]
152 #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
153 pub struct $borrowed(pub(crate) str);
154
155 impl $borrowed {
156 pub(crate) fn from_str_unchecked(s: &str) -> &Self {
157 unsafe { mem::transmute(s) }
160 }
161
162 pub fn as_str(&self) -> &str {
164 &self.0
165 }
166 }
167
168 impl Deref for $borrowed {
169 type Target = str;
170
171 fn deref(&self) -> &Self::Target {
172 &self.0
173 }
174 }
175
176 impl ToOwned for $borrowed {
177 type Owned = $name;
178
179 fn to_owned(&self) -> Self::Owned {
180 $name(self.0.to_string())
181 }
182 }
183
184 impl AsRef<str> for $borrowed {
185 fn as_ref(&self) -> &str {
186 &self.0
187 }
188 }
189
190 impl fmt::Display for $borrowed {
191 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
192 write!(f, "{}", &self.0)
193 }
194 }
195 }
196}
197
198def_part_types! {
199 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
205 pub struct NodePart(String) use node_check();
206
207 #[cfg_attr(feature = "serde", derive(Serialize))]
211 pub struct ref NodeRef(str);
212}
213
214def_part_types! {
215 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
219 pub struct DomainPart(String) use domain_check();
220
221 #[cfg_attr(feature = "serde", derive(Serialize))]
225 pub struct ref DomainRef(str);
226}
227
228def_part_types! {
229 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
232 pub struct ResourcePart(String) use resource_check();
233
234 #[cfg_attr(feature = "serde", derive(Serialize))]
239 pub struct ref ResourceRef(str);
240}
241
242impl DomainRef {
243 pub fn with_node(&self, node: &NodeRef) -> BareJid {
246 BareJid::from_parts(Some(node), self)
247 }
248}
249
250impl From<DomainPart> for BareJid {
251 fn from(other: DomainPart) -> Self {
252 BareJid {
253 inner: other.into(),
254 }
255 }
256}
257
258impl From<DomainPart> for Jid {
259 fn from(other: DomainPart) -> Self {
260 Jid {
261 normalized: other.0,
262 at: None,
263 slash: None,
264 }
265 }
266}
267
268impl<'x> From<&'x DomainRef> for BareJid {
269 fn from(other: &'x DomainRef) -> Self {
270 Self::from_parts(None, other)
271 }
272}
273
274impl NodeRef {
275 pub fn with_domain(&self, domain: &DomainRef) -> BareJid {
278 BareJid::from_parts(Some(self), domain)
279 }
280}
281
282#[cfg(test)]
283mod tests {
284 use super::*;
285
286 #[test]
287 fn nodepart_comparison() {
288 let n1 = NodePart::new("foo").unwrap();
289 let n2 = NodePart::new("bar").unwrap();
290 let n3 = NodePart::new("foo").unwrap();
291 assert_eq!(n1, n3);
292 assert_ne!(n1, n2);
293 }
294}