fidl_next_codec/wire/string/
optional.rs1use core::fmt;
6use core::mem::MaybeUninit;
7use core::str::from_utf8;
8
9use munge::munge;
10
11use crate::{
12    Constrained, Decode, DecodeError, Decoder, EncodeError, EncodeOption, Encoder, FromWireOption,
13    FromWireOptionRef, IntoNatural, Slot, ValidationError, Wire, WireOptionalVector, WireString,
14    WireVector,
15};
16
17#[repr(transparent)]
19pub struct WireOptionalString<'de> {
20    vec: WireOptionalVector<'de, u8>,
21}
22
23unsafe impl Wire for WireOptionalString<'static> {
24    type Owned<'de> = WireOptionalString<'de>;
25
26    #[inline]
27    fn zero_padding(out: &mut MaybeUninit<Self>) {
28        munge!(let Self { vec } = out);
29        WireOptionalVector::<u8>::zero_padding(vec);
30    }
31}
32
33impl WireOptionalString<'_> {
34    #[inline]
36    pub fn encode_present(out: &mut MaybeUninit<Self>, len: u64) {
37        munge!(let Self { vec } = out);
38        WireOptionalVector::encode_present(vec, len);
39    }
40
41    #[inline]
43    pub fn encode_absent(out: &mut MaybeUninit<Self>) {
44        munge!(let Self { vec } = out);
45        WireOptionalVector::encode_absent(vec);
46    }
47
48    #[inline]
50    pub fn is_some(&self) -> bool {
51        self.vec.is_some()
52    }
53
54    #[inline]
56    pub fn is_none(&self) -> bool {
57        self.vec.is_none()
58    }
59
60    #[inline]
62    pub fn as_ref(&self) -> Option<&WireString<'_>> {
63        self.vec.as_ref().map(|vec| unsafe { &*(vec as *const WireVector<'_, u8>).cast() })
64    }
65
66    fn validate_max_len(slot: Slot<'_, Self>, limit: u64) -> Result<(), crate::ValidationError> {
68        munge!(let Self { vec } = slot);
69        match WireOptionalVector::validate_max_len(vec, limit) {
70            Ok(()) => Ok(()),
71            Err(ValidationError::VectorTooLong { count, limit }) => {
72                Err(ValidationError::StringTooLong { count, limit })
73            }
74            Err(e) => Err(e),
75        }
76    }
77}
78
79impl Constrained for WireOptionalString<'_> {
80    type Constraint = u64;
81
82    fn validate(slot: Slot<'_, Self>, constraint: Self::Constraint) -> Result<(), ValidationError> {
83        Self::validate_max_len(slot, constraint)
84    }
85}
86
87impl fmt::Debug for WireOptionalString<'_> {
88    #[inline]
89    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90        self.as_ref().fmt(f)
91    }
92}
93
94unsafe impl<D: Decoder + ?Sized> Decode<D> for WireOptionalString<'static> {
95    #[inline]
96    fn decode(slot: Slot<'_, Self>, decoder: &mut D, constraint: u64) -> Result<(), DecodeError> {
97        munge!(let Self { mut vec } = slot);
98
99        let result = unsafe { WireOptionalVector::decode_raw(vec.as_mut(), decoder, constraint) };
100        match result {
101            Ok(()) => (),
102            Err(DecodeError::Validation(ValidationError::VectorTooLong { count, limit })) => {
103                return Err(DecodeError::Validation(ValidationError::StringTooLong {
104                    count,
105                    limit,
106                }));
107            }
108            Err(e) => return Err(e),
109        }
110        let vec = unsafe { vec.deref_unchecked() };
111        if let Some(bytes) = vec.as_ref() {
112            if !bytes.as_slice().is_ascii() {
114                let _ = from_utf8(bytes)?;
117            }
118        }
119
120        Ok(())
121    }
122}
123
124unsafe impl<E: Encoder + ?Sized> EncodeOption<WireOptionalString<'static>, E> for String {
125    #[inline]
126    fn encode_option(
127        this: Option<Self>,
128        encoder: &mut E,
129        out: &mut MaybeUninit<WireOptionalString<'static>>,
130        constraint: u64,
131    ) -> Result<(), EncodeError> {
132        <&str>::encode_option(this.as_deref(), encoder, out, constraint)
133    }
134}
135
136unsafe impl<E: Encoder + ?Sized> EncodeOption<WireOptionalString<'static>, E> for &String {
137    #[inline]
138    fn encode_option(
139        this: Option<Self>,
140        encoder: &mut E,
141        out: &mut MaybeUninit<WireOptionalString<'static>>,
142        constraint: u64,
143    ) -> Result<(), EncodeError> {
144        <&str>::encode_option(this.map(String::as_str), encoder, out, constraint)
145    }
146}
147
148unsafe impl<E: Encoder + ?Sized> EncodeOption<WireOptionalString<'static>, E> for &str {
149    #[inline]
150    fn encode_option(
151        this: Option<Self>,
152        encoder: &mut E,
153        out: &mut MaybeUninit<WireOptionalString<'static>>,
154        _constraint: u64,
155    ) -> Result<(), EncodeError> {
156        if let Some(string) = this {
157            encoder.write(string.as_bytes());
158            WireOptionalString::encode_present(out, string.len() as u64);
159        } else {
160            WireOptionalString::encode_absent(out);
161        }
162        Ok(())
163    }
164}
165
166impl FromWireOption<WireOptionalString<'_>> for String {
167    #[inline]
168    fn from_wire_option(wire: WireOptionalString<'_>) -> Option<Self> {
169        Vec::from_wire_option(wire.vec).map(|vec| unsafe { String::from_utf8_unchecked(vec) })
170    }
171}
172
173impl IntoNatural for WireOptionalString<'_> {
174    type Natural = Option<String>;
175}
176
177impl FromWireOptionRef<WireOptionalString<'_>> for String {
178    #[inline]
179    fn from_wire_option_ref(wire: &WireOptionalString<'_>) -> Option<Self> {
180        Vec::from_wire_option_ref(&wire.vec).map(|vec| unsafe { String::from_utf8_unchecked(vec) })
181    }
182}