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::ParseError;
6use crate::TinyAsciiStr;
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>, ParseError> {
33        TinyAsciiStr::try_from_raw(self.0)
34    }
35
36    #[inline]
37    /// Unsafely converts into a [`TinyAsciiStr`].
38    pub const fn from_utf8_unchecked(bytes: [u8; N]) -> Self {
39        Self(bytes)
40    }
41}
42
43impl<const N: usize> TinyAsciiStr<N> {
44    #[inline]
45    // Converts into a [`UnvalidatedTinyAsciiStr`]
46    pub const fn to_unvalidated(self) -> UnvalidatedTinyAsciiStr<N> {
47        UnvalidatedTinyAsciiStr(*self.all_bytes())
48    }
49}
50
51impl<const N: usize> From<TinyAsciiStr<N>> for UnvalidatedTinyAsciiStr<N> {
52    fn from(other: TinyAsciiStr<N>) -> Self {
53        other.to_unvalidated()
54    }
55}
56
57#[cfg(feature = "serde")]
58impl<const N: usize> serde::Serialize for UnvalidatedTinyAsciiStr<N> {
59    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
60    where
61        S: serde::Serializer,
62    {
63        use serde::ser::Error;
64        self.try_into_tinystr()
65            .map_err(|_| S::Error::custom("invalid ascii in UnvalidatedTinyAsciiStr"))?
66            .serialize(serializer)
67    }
68}
69
70macro_rules! deserialize {
71    ($size:literal) => {
72        #[cfg(feature = "serde")]
73        impl<'de, 'a> serde::Deserialize<'de> for UnvalidatedTinyAsciiStr<$size>
74        where
75            'de: 'a,
76        {
77            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
78            where
79                D: serde::Deserializer<'de>,
80            {
81                if deserializer.is_human_readable() {
82                    Ok(TinyAsciiStr::deserialize(deserializer)?.to_unvalidated())
83                } else {
84                    Ok(Self(<[u8; $size]>::deserialize(deserializer)?))
85                }
86            }
87        }
88    };
89}
90
91deserialize!(1);
92deserialize!(2);
93deserialize!(3);
94deserialize!(4);
95deserialize!(5);
96deserialize!(6);
97deserialize!(7);
98deserialize!(8);
99deserialize!(9);
100deserialize!(10);
101deserialize!(11);
102deserialize!(12);
103deserialize!(13);
104deserialize!(14);
105deserialize!(15);
106deserialize!(16);
107deserialize!(17);
108deserialize!(18);
109deserialize!(19);
110deserialize!(20);
111deserialize!(21);
112deserialize!(22);
113deserialize!(23);
114deserialize!(24);
115deserialize!(25);
116deserialize!(26);
117deserialize!(27);
118deserialize!(28);
119deserialize!(29);
120deserialize!(30);
121deserialize!(31);
122deserialize!(32);