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 ).
45use crate::TinyAsciiStr;
6use crate::TinyStrError;
7use core::fmt;
89/// 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]);
1819impl<const N: usize> fmt::Debug for UnvalidatedTinyAsciiStr<N> {
20fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21// Debug as a string if possible
22match self.try_into_tinystr() {
23Ok(s) => fmt::Debug::fmt(&s, f),
24Err(_) => fmt::Debug::fmt(&self.0, f),
25 }
26 }
27}
2829impl<const N: usize> UnvalidatedTinyAsciiStr<N> {
30#[inline]
31// Converts into a [`TinyAsciiStr`]. Fails if the bytes are not valid ASCII.
32pub fn try_into_tinystr(&self) -> Result<TinyAsciiStr<N>, TinyStrError> {
33 TinyAsciiStr::try_from_raw(self.0)
34 }
3536#[doc(hidden)]
37pub const fn from_bytes_unchecked(bytes: [u8; N]) -> Self {
38Self(bytes)
39 }
40}
4142impl<const N: usize> TinyAsciiStr<N> {
43#[inline]
44// Converts into a [`UnvalidatedTinyAsciiStr`]
45pub const fn to_unvalidated(self) -> UnvalidatedTinyAsciiStr<N> {
46 UnvalidatedTinyAsciiStr(*self.all_bytes())
47 }
48}
4950impl<const N: usize> From<TinyAsciiStr<N>> for UnvalidatedTinyAsciiStr<N> {
51fn from(other: TinyAsciiStr<N>) -> Self {
52 other.to_unvalidated()
53 }
54}
5556#[cfg(feature = "serde")]
57impl<const N: usize> serde::Serialize for UnvalidatedTinyAsciiStr<N> {
58fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
59where
60S: serde::Serializer,
61 {
62use serde::ser::Error;
63self.try_into_tinystr()
64 .map_err(|_| S::Error::custom("invalid ascii in UnvalidatedTinyAsciiStr"))?
65.serialize(serializer)
66 }
67}
6869macro_rules! deserialize {
70 ($size:literal) => {
71#[cfg(feature = "serde")]
72impl<'de, 'a> serde::Deserialize<'de> for UnvalidatedTinyAsciiStr<$size>
73where
74'de: 'a,
75 {
76fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
77where
78D: serde::Deserializer<'de>,
79 {
80if deserializer.is_human_readable() {
81Ok(TinyAsciiStr::deserialize(deserializer)?.to_unvalidated())
82 } else {
83Ok(Self(<[u8; $size]>::deserialize(deserializer)?))
84 }
85 }
86 }
87 };
88}
8990deserialize!(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);