datetime_string/rfc3339/full_time/
owned.rs

1//! RFC 3339 [`full-time`] owned string type.
2//!
3//! [`full-time`]: https://tools.ietf.org/html/rfc3339#section-5.6
4#![cfg(feature = "alloc")]
5
6use core::{convert::TryFrom, fmt, ops, str};
7
8use alloc::{string::String, vec::Vec};
9
10#[cfg(feature = "serde")]
11use serde::Serialize;
12
13use crate::{ConversionError, Error};
14
15use super::{validate_bytes, FullTimeStr};
16
17/// Owned string for a time in RFC 3339 [`full-time`] format, such as `12:34:56.7890-23:12`.
18///
19/// Available when `alloc` feature is enabled.
20///
21/// # Examples
22///
23/// ```
24/// # use datetime_string::rfc3339::FullTimeString;
25/// use datetime_string::rfc3339::FullTimeStr;
26/// use std::convert::TryFrom;
27///
28/// let try_from = FullTimeString::try_from("12:34:56.7890-23:12")?;
29///
30/// let parse = "12:34:56.7890-23:12".parse::<FullTimeString>()?;
31/// let parse2: FullTimeString = "12:34:56.7890-23:12".parse()?;
32///
33/// let to_owned = FullTimeStr::from_str("12:34:56.7890-23:12")?.to_owned();
34/// let into: FullTimeString = FullTimeStr::from_str("12:34:56.7890-23:12")?.into();
35/// # Ok::<_, datetime_string::Error>(())
36/// ```
37///
38/// [`full-time`]: https://tools.ietf.org/html/rfc3339#section-5.6
39#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
40#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
41#[repr(transparent)]
42// Note that `derive(Serialize)` cannot used here, because it encodes this as
43// `Vec<u8>` rather than as a string.
44//
45// Comparisons implemented for the type are consistent (at least it is intended to be so).
46// See <https://github.com/rust-lang/rust-clippy/issues/2025>.
47// Note that `clippy::derive_ord_xor_partial_ord` would be introduced since Rust 1.47.0.
48#[allow(clippy::derive_hash_xor_eq)]
49#[allow(unknown_lints, clippy::derive_ord_xor_partial_ord)]
50pub struct FullTimeString(Vec<u8>);
51
52impl FullTimeString {
53    /// Creates a `FullTimeString` from the given bytes.
54    ///
55    /// This performs assertion in debug build, but not in release build.
56    ///
57    /// # Safety
58    ///
59    /// `validate_bytes(&s)` should return `Ok(())`.
60    #[inline]
61    #[must_use]
62    unsafe fn from_bytes_maybe_unchecked(s: Vec<u8>) -> Self {
63        debug_assert_ok!(validate_bytes(&s));
64        Self(s)
65    }
66
67    /// Returns a `&FullTimeStr` for the string.
68    ///
69    /// # Examples
70    ///
71    /// ```
72    /// # use datetime_string::rfc3339::FullTimeString;
73    /// use datetime_string::rfc3339::FullTimeStr;
74    ///
75    /// let time = "12:34:56.7890-23:12".parse::<FullTimeString>()?;
76    ///
77    /// // Usually you don't need to call `as_deref()` explicitly, because
78    /// // `Deref<Target = FullTimeStr>` trait is implemented.
79    /// let _: &FullTimeStr = time.as_deref();
80    /// # Ok::<_, datetime_string::Error>(())
81    /// ```
82    #[inline]
83    #[must_use]
84    pub fn as_deref(&self) -> &FullTimeStr {
85        unsafe {
86            // This is safe because `self.0` is valid `full-time` string.
87            debug_assert_safe_version_ok!(FullTimeStr::from_bytes(&self.0));
88            FullTimeStr::from_bytes_maybe_unchecked(&self.0)
89        }
90    }
91
92    /// Returns a `&mut FullTimeStr` for the string.
93    ///
94    /// # Examples
95    ///
96    /// ```
97    /// # use datetime_string::rfc3339::FullTimeString;
98    /// use datetime_string::rfc3339::FullTimeStr;
99    ///
100    /// let mut time = "12:34:56.7890-23:12".parse::<FullTimeString>()?;
101    ///
102    /// // Usually you don't need to call `as_deref_mut()` explicitly, because
103    /// // `DerefMut` trait is implemented.
104    /// let _: &mut FullTimeStr = time.as_deref_mut();
105    /// # Ok::<_, datetime_string::Error>(())
106    /// ```
107    #[inline]
108    #[must_use]
109    pub fn as_deref_mut(&mut self) -> &mut FullTimeStr {
110        unsafe {
111            debug_assert_ok!(FullTimeStr::from_bytes(&self.0));
112            // This is safe because `self.0` is valid, and `FullTimeStr` ensures
113            // that the underlying bytes are ASCII string after modification.
114            FullTimeStr::from_bytes_maybe_unchecked_mut(&mut self.0)
115        }
116    }
117}
118
119impl core::borrow::Borrow<FullTimeStr> for FullTimeString {
120    #[inline]
121    fn borrow(&self) -> &FullTimeStr {
122        self.as_deref()
123    }
124}
125
126impl core::borrow::BorrowMut<FullTimeStr> for FullTimeString {
127    #[inline]
128    fn borrow_mut(&mut self) -> &mut FullTimeStr {
129        self.as_deref_mut()
130    }
131}
132
133impl alloc::borrow::ToOwned for FullTimeStr {
134    type Owned = FullTimeString;
135
136    #[inline]
137    fn to_owned(&self) -> Self::Owned {
138        self.into()
139    }
140}
141
142impl AsRef<[u8]> for FullTimeString {
143    #[inline]
144    fn as_ref(&self) -> &[u8] {
145        self.as_bytes()
146    }
147}
148
149impl AsRef<str> for FullTimeString {
150    #[inline]
151    fn as_ref(&self) -> &str {
152        self.as_str()
153    }
154}
155
156impl AsRef<FullTimeStr> for FullTimeString {
157    #[inline]
158    fn as_ref(&self) -> &FullTimeStr {
159        self
160    }
161}
162
163impl AsMut<FullTimeStr> for FullTimeString {
164    #[inline]
165    fn as_mut(&mut self) -> &mut FullTimeStr {
166        self
167    }
168}
169
170impl From<FullTimeString> for Vec<u8> {
171    #[inline]
172    fn from(v: FullTimeString) -> Vec<u8> {
173        v.0
174    }
175}
176
177impl From<FullTimeString> for String {
178    #[inline]
179    fn from(v: FullTimeString) -> String {
180        unsafe {
181            // This is safe because a valid `full-time` string is an ASCII string.
182            debug_assert_ok!(str::from_utf8(&v.0));
183            String::from_utf8_unchecked(v.0)
184        }
185    }
186}
187
188impl From<&FullTimeStr> for FullTimeString {
189    fn from(v: &FullTimeStr) -> Self {
190        unsafe {
191            // This is safe because the value is already validated.
192            Self::from_bytes_maybe_unchecked(v.0.into())
193        }
194    }
195}
196
197impl TryFrom<&[u8]> for FullTimeString {
198    type Error = Error;
199
200    #[inline]
201    fn try_from(v: &[u8]) -> Result<Self, Self::Error> {
202        FullTimeStr::from_bytes(v).map(Into::into)
203    }
204}
205
206impl TryFrom<&str> for FullTimeString {
207    type Error = Error;
208
209    #[inline]
210    fn try_from(v: &str) -> Result<Self, Self::Error> {
211        FullTimeStr::from_str(v).map(Into::into)
212    }
213}
214
215impl TryFrom<Vec<u8>> for FullTimeString {
216    type Error = ConversionError<Vec<u8>>;
217
218    #[inline]
219    fn try_from(v: Vec<u8>) -> Result<Self, Self::Error> {
220        match validate_bytes(&v) {
221            Ok(_) => Ok(unsafe {
222                // This is safe because the value is successfully validated.
223                Self::from_bytes_maybe_unchecked(v)
224            }),
225            Err(e) => Err(ConversionError::new(v, e)),
226        }
227    }
228}
229
230impl TryFrom<String> for FullTimeString {
231    type Error = ConversionError<String>;
232
233    #[inline]
234    fn try_from(v: String) -> Result<Self, Self::Error> {
235        match validate_bytes(v.as_bytes()) {
236            Ok(_) => Ok(unsafe {
237                // This is safe because the value is successfully validated.
238                Self::from_bytes_maybe_unchecked(v.into_bytes())
239            }),
240            Err(e) => Err(ConversionError::new(v, e)),
241        }
242    }
243}
244
245impl fmt::Display for FullTimeString {
246    #[inline]
247    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
248        self.as_deref().fmt(f)
249    }
250}
251
252impl ops::Deref for FullTimeString {
253    type Target = FullTimeStr;
254
255    #[inline]
256    fn deref(&self) -> &Self::Target {
257        self.as_deref()
258    }
259}
260
261impl ops::DerefMut for FullTimeString {
262    #[inline]
263    fn deref_mut(&mut self) -> &mut Self::Target {
264        self.as_deref_mut()
265    }
266}
267
268impl str::FromStr for FullTimeString {
269    type Err = Error;
270
271    #[inline]
272    fn from_str(s: &str) -> Result<Self, Self::Err> {
273        Self::try_from(s)
274    }
275}
276
277impl_cmp_symmetric!(FullTimeStr, FullTimeString, &FullTimeString);
278impl_cmp_symmetric!(FullTimeStr, FullTimeString, FullTimeStr);
279impl_cmp_symmetric!(FullTimeStr, FullTimeString, &FullTimeStr);
280impl_cmp_symmetric!(str, FullTimeString, str);
281impl_cmp_symmetric!(str, FullTimeString, &str);
282impl_cmp_symmetric!(str, &FullTimeString, str);
283impl_cmp_symmetric!([u8], FullTimeString, [u8]);
284impl_cmp_symmetric!([u8], FullTimeString, &[u8]);
285impl_cmp_symmetric!([u8], &FullTimeString, [u8]);
286
287#[cfg(feature = "serde")]
288#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
289impl Serialize for FullTimeString {
290    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
291    where
292        S: serde::Serializer,
293    {
294        serializer.serialize_str(self.as_str())
295    }
296}
297
298/// Items for serde support.
299#[cfg(feature = "serde")]
300mod serde_ {
301    use super::*;
302
303    use serde::de::{Deserialize, Deserializer, Visitor};
304
305    /// Visitor for `FullTimeString`.
306    struct StringVisitor;
307
308    impl<'de> Visitor<'de> for StringVisitor {
309        type Value = FullTimeString;
310
311        #[inline]
312        fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
313            f.write_str("RFC 3339 full-time string")
314        }
315
316        #[inline]
317        fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
318        where
319            E: serde::de::Error,
320        {
321            Self::Value::try_from(v).map_err(E::custom)
322        }
323
324        #[inline]
325        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
326        where
327            E: serde::de::Error,
328        {
329            Self::Value::try_from(v).map_err(E::custom)
330        }
331    }
332
333    impl<'de> Deserialize<'de> for FullTimeString {
334        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
335        where
336            D: Deserializer<'de>,
337        {
338            deserializer.deserialize_any(StringVisitor)
339        }
340    }
341}
342
343#[cfg(feature = "serde")]
344#[cfg(test)]
345mod tests {
346    use super::*;
347
348    use serde_test::{assert_de_tokens, assert_tokens, Token};
349
350    #[test]
351    fn ser_de_string() {
352        let raw: &'static str = "12:34:56.7890-23:12";
353        assert_tokens(&FullTimeString::try_from(raw).unwrap(), &[Token::Str(raw)]);
354    }
355
356    #[test]
357    fn de_bytes() {
358        let raw: &'static [u8] = b"12:34:56.7890-23:12";
359        assert_de_tokens(
360            &FullTimeString::try_from(raw).unwrap(),
361            &[Token::Bytes(raw)],
362        );
363    }
364}