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::ParseError;
6use crate::TinyAsciiStr;
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>, ParseError> {
33 TinyAsciiStr::try_from_raw(self.0)
34 }
3536#[inline]
37/// Unsafely converts into a [`TinyAsciiStr`].
38pub const fn from_utf8_unchecked(bytes: [u8; N]) -> Self {
39Self(bytes)
40 }
41}
4243impl<const N: usize> TinyAsciiStr<N> {
44#[inline]
45// Converts into a [`UnvalidatedTinyAsciiStr`]
46pub const fn to_unvalidated(self) -> UnvalidatedTinyAsciiStr<N> {
47 UnvalidatedTinyAsciiStr(*self.all_bytes())
48 }
49}
5051impl<const N: usize> From<TinyAsciiStr<N>> for UnvalidatedTinyAsciiStr<N> {
52fn from(other: TinyAsciiStr<N>) -> Self {
53 other.to_unvalidated()
54 }
55}
5657#[cfg(feature = "serde")]
58impl<const N: usize> serde::Serialize for UnvalidatedTinyAsciiStr<N> {
59fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
60where
61S: serde::Serializer,
62 {
63use serde::ser::Error;
64self.try_into_tinystr()
65 .map_err(|_| S::Error::custom("invalid ascii in UnvalidatedTinyAsciiStr"))?
66.serialize(serializer)
67 }
68}
6970macro_rules! deserialize {
71 ($size:literal) => {
72#[cfg(feature = "serde")]
73impl<'de, 'a> serde::Deserialize<'de> for UnvalidatedTinyAsciiStr<$size>
74where
75'de: 'a,
76 {
77fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
78where
79D: serde::Deserializer<'de>,
80 {
81if deserializer.is_human_readable() {
82Ok(TinyAsciiStr::deserialize(deserializer)?.to_unvalidated())
83 } else {
84Ok(Self(<[u8; $size]>::deserialize(deserializer)?))
85 }
86 }
87 }
88 };
89}
9091deserialize!(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);