1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use core::fmt;
use core::str::from_utf8;

use munge::munge;

use crate::{
    decode, encode, Decode, Decoder, Encoder, Slot, TakeFrom, WireOptionalVector, WireString,
    WireVector,
};

/// An optional FIDL string
#[derive(Default)]
#[repr(transparent)]
pub struct WireOptionalString<'buf> {
    vec: WireOptionalVector<'buf, u8>,
}

impl<'buf> WireOptionalString<'buf> {
    /// Encodes that a string is present in a slot.
    pub fn encode_present(slot: Slot<'_, Self>, len: u64) {
        munge!(let Self { vec } = slot);
        WireOptionalVector::encode_present(vec, len);
    }

    /// Encodes that a string is absent in a slot.
    pub fn encode_absent(slot: Slot<'_, Self>) {
        munge!(let Self { vec } = slot);
        WireOptionalVector::encode_absent(vec);
    }

    /// Returns whether the optional string is present.
    pub fn is_some(&self) -> bool {
        self.vec.is_some()
    }

    /// Returns whether the optional string is absent.
    pub fn is_none(&self) -> bool {
        self.vec.is_none()
    }

    /// Takes the string out of the option, if any.
    pub fn take(&mut self) -> Option<WireString<'buf>> {
        self.vec.take().map(|vec| unsafe { WireString::new_unchecked(vec) })
    }

    /// Returns a reference to the underlying string, if any.
    pub fn as_ref(&self) -> Option<&WireString<'buf>> {
        self.vec.as_ref().map(|vec| unsafe { &*(vec as *const WireVector<'buf, u8>).cast() })
    }

    /// Returns a mutable reference to the underlying string, if any.
    pub fn as_mut(&mut self) -> Option<&mut WireString<'buf>> {
        self.vec.as_mut().map(|vec| unsafe { &mut *(vec as *mut WireVector<'buf, u8>).cast() })
    }
}

impl fmt::Debug for WireOptionalString<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_ref().fmt(f)
    }
}

unsafe impl<'buf, D: Decoder<'buf> + ?Sized> Decode<D> for WireOptionalString<'buf> {
    fn decode(slot: Slot<'_, Self>, decoder: &mut D) -> Result<(), decode::DecodeError> {
        munge!(let Self { mut vec } = slot);

        WireOptionalVector::decode(vec.as_mut(), decoder)?;
        let vec = unsafe { vec.deref_unchecked() };
        if let Some(bytes) = vec.as_ref() {
            from_utf8(bytes)?;
        }

        Ok(())
    }
}

impl encode::EncodableOption for String {
    type EncodedOption<'buf> = WireOptionalString<'buf>;
}

impl<E: Encoder + ?Sized> encode::EncodeOption<E> for String {
    fn encode_option(
        this: Option<&mut Self>,
        encoder: &mut E,
        slot: Slot<'_, Self::EncodedOption<'_>>,
    ) -> Result<(), encode::EncodeError> {
        if let Some(string) = this {
            encoder.write(string.as_bytes());
            WireOptionalString::encode_present(slot, string.len() as u64);
        } else {
            WireOptionalString::encode_absent(slot);
        }

        Ok(())
    }
}

impl TakeFrom<WireOptionalString<'_>> for Option<String> {
    fn take_from(from: &mut WireOptionalString<'_>) -> Self {
        from.as_mut().map(String::take_from)
    }
}