tinystr/
unvalidated.rs

1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5use crate::TinyAsciiStr;
6use crate::TinyStrError;
7use core::fmt;
8
9/// A fixed-length bytes array that is expected to be an ASCII string but does not enforce that invariant.
10///
11/// Use this type instead of `TinyAsciiStr` if you don't need to enforce ASCII during deserialization. For
12/// example, strings that are keys of a map don't need to ever be reified as `TinyAsciiStr`s.
13///
14/// The main advantage of this type over `[u8; N]` is that it serializes as a string in
15/// human-readable formats like JSON.
16#[derive(PartialEq, PartialOrd, Eq, Ord, Clone, Copy)]
17pub struct UnvalidatedTinyAsciiStr<const N: usize>(pub(crate) [u8; N]);
18
19impl<const N: usize> fmt::Debug for UnvalidatedTinyAsciiStr<N> {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        // Debug as a string if possible
22        match self.try_into_tinystr() {
23            Ok(s) => fmt::Debug::fmt(&s, f),
24            Err(_) => fmt::Debug::fmt(&self.0, f),
25        }
26    }
27}
28
29impl<const N: usize> UnvalidatedTinyAsciiStr<N> {
30    #[inline]
31    // Converts into a [`TinyAsciiStr`]. Fails if the bytes are not valid ASCII.
32    pub fn try_into_tinystr(&self) -> Result<TinyAsciiStr<N>, TinyStrError> {
33        TinyAsciiStr::try_from_raw(self.0)
34    }
35
36    #[doc(hidden)]
37    pub const fn from_bytes_unchecked(bytes: [u8; N]) -> Self {
38        Self(bytes)
39    }
40}
41
42impl<const N: usize> TinyAsciiStr<N> {
43    #[inline]
44    // Converts into a [`UnvalidatedTinyAsciiStr`]
45    pub const fn to_unvalidated(self) -> UnvalidatedTinyAsciiStr<N> {
46        UnvalidatedTinyAsciiStr(*self.all_bytes())
47    }
48}
49
50impl<const N: usize> From<TinyAsciiStr<N>> for UnvalidatedTinyAsciiStr<N> {
51    fn from(other: TinyAsciiStr<N>) -> Self {
52        other.to_unvalidated()
53    }
54}
55
56#[cfg(feature = "serde")]
57impl<const N: usize> serde::Serialize for UnvalidatedTinyAsciiStr<N> {
58    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
59    where
60        S: serde::Serializer,
61    {
62        use serde::ser::Error;
63        self.try_into_tinystr()
64            .map_err(|_| S::Error::custom("invalid ascii in UnvalidatedTinyAsciiStr"))?
65            .serialize(serializer)
66    }
67}
68
69macro_rules! deserialize {
70    ($size:literal) => {
71        #[cfg(feature = "serde")]
72        impl<'de, 'a> serde::Deserialize<'de> for UnvalidatedTinyAsciiStr<$size>
73        where
74            'de: 'a,
75        {
76            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
77            where
78                D: serde::Deserializer<'de>,
79            {
80                if deserializer.is_human_readable() {
81                    Ok(TinyAsciiStr::deserialize(deserializer)?.to_unvalidated())
82                } else {
83                    Ok(Self(<[u8; $size]>::deserialize(deserializer)?))
84                }
85            }
86        }
87    };
88}
89
90deserialize!(1);
91deserialize!(2);
92deserialize!(3);
93deserialize!(4);
94deserialize!(5);
95deserialize!(6);
96deserialize!(7);
97deserialize!(8);
98deserialize!(9);
99deserialize!(10);
100deserialize!(11);
101deserialize!(12);
102deserialize!(13);
103deserialize!(14);
104deserialize!(15);
105deserialize!(16);
106deserialize!(17);
107deserialize!(18);
108deserialize!(19);
109deserialize!(20);
110deserialize!(21);
111deserialize!(22);
112deserialize!(23);
113deserialize!(24);
114deserialize!(25);
115deserialize!(26);
116deserialize!(27);
117deserialize!(28);
118deserialize!(29);
119deserialize!(30);
120deserialize!(31);
121deserialize!(32);