dharitri_sc_codec/impl_for_types/
impl_string.rs

1use crate::{
2    DecodeError, DecodeErrorHandler, EncodeErrorHandler, NestedDecode, NestedDecodeInput,
3    NestedEncode, NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput,
4};
5use alloc::{boxed::Box, string::String, vec::Vec};
6
7impl TopEncode for String {
8    #[inline]
9    fn top_encode_or_handle_err<O, H>(&self, output: O, h: H) -> Result<(), H::HandledErr>
10    where
11        O: TopEncodeOutput,
12        H: EncodeErrorHandler,
13    {
14        self.as_bytes().top_encode_or_handle_err(output, h)
15    }
16}
17
18impl TopEncode for &str {
19    fn top_encode_or_handle_err<O, H>(&self, output: O, _h: H) -> Result<(), H::HandledErr>
20    where
21        O: TopEncodeOutput,
22        H: EncodeErrorHandler,
23    {
24        output.set_slice_u8(self.as_bytes());
25        Ok(())
26    }
27}
28
29impl TopEncode for Box<str> {
30    #[inline]
31    fn top_encode_or_handle_err<O, H>(&self, output: O, h: H) -> Result<(), H::HandledErr>
32    where
33        O: TopEncodeOutput,
34        H: EncodeErrorHandler,
35    {
36        self.as_ref().as_bytes().top_encode_or_handle_err(output, h)
37    }
38}
39
40impl TopDecode for String {
41    fn top_decode_or_handle_err<I, H>(input: I, h: H) -> Result<Self, H::HandledErr>
42    where
43        I: TopDecodeInput,
44        H: DecodeErrorHandler,
45    {
46        let raw = Vec::<u8>::top_decode_or_handle_err(input, h)?;
47        match String::from_utf8(raw) {
48            Ok(s) => Ok(s),
49            Err(_) => Err(h.handle_error(DecodeError::UTF8_DECODE_ERROR)),
50        }
51    }
52}
53
54impl TopDecode for Box<str> {
55    fn top_decode_or_handle_err<I, H>(input: I, h: H) -> Result<Self, H::HandledErr>
56    where
57        I: TopDecodeInput,
58        H: DecodeErrorHandler,
59    {
60        Ok(String::top_decode_or_handle_err(input, h)?.into_boxed_str())
61    }
62}
63
64impl NestedEncode for String {
65    #[inline]
66    fn dep_encode_or_handle_err<O, H>(&self, dest: &mut O, h: H) -> Result<(), H::HandledErr>
67    where
68        O: NestedEncodeOutput,
69        H: EncodeErrorHandler,
70    {
71        self.as_bytes().dep_encode_or_handle_err(dest, h)
72    }
73}
74
75impl NestedEncode for &str {
76    fn dep_encode_or_handle_err<O, H>(&self, dest: &mut O, h: H) -> Result<(), H::HandledErr>
77    where
78        O: NestedEncodeOutput,
79        H: EncodeErrorHandler,
80    {
81        self.as_bytes().dep_encode_or_handle_err(dest, h)
82    }
83}
84
85impl NestedEncode for Box<str> {
86    #[inline]
87    fn dep_encode_or_handle_err<O, H>(&self, dest: &mut O, h: H) -> Result<(), H::HandledErr>
88    where
89        O: NestedEncodeOutput,
90        H: EncodeErrorHandler,
91    {
92        self.as_ref().as_bytes().dep_encode_or_handle_err(dest, h)
93    }
94}
95
96impl NestedDecode for String {
97    fn dep_decode_or_handle_err<I, H>(input: &mut I, h: H) -> Result<Self, H::HandledErr>
98    where
99        I: NestedDecodeInput,
100        H: DecodeErrorHandler,
101    {
102        let raw = Vec::<u8>::dep_decode_or_handle_err(input, h)?;
103        match String::from_utf8(raw) {
104            Ok(s) => Ok(s),
105            Err(_) => Err(h.handle_error(DecodeError::UTF8_DECODE_ERROR)),
106        }
107    }
108}
109
110impl NestedDecode for Box<str> {
111    #[inline]
112    fn dep_decode_or_handle_err<I, H>(input: &mut I, h: H) -> Result<Self, H::HandledErr>
113    where
114        I: NestedDecodeInput,
115        H: DecodeErrorHandler,
116    {
117        Ok(String::dep_decode_or_handle_err(input, h)?.into_boxed_str())
118    }
119}
120
121#[cfg(test)]
122mod tests {
123    use crate::test_util::{check_dep_encode_decode, check_top_encode_decode};
124    use alloc::string::String;
125
126    #[test]
127    fn test_top() {
128        let s = "abc";
129        check_top_encode_decode(String::from(s), b"abc");
130        check_top_encode_decode(String::from(s).into_boxed_str(), b"abc");
131    }
132
133    #[test]
134    fn test_dep() {
135        let s = "abc";
136        check_dep_encode_decode(String::from(s), &[0, 0, 0, 3, b'a', b'b', b'c']);
137        check_dep_encode_decode(
138            String::from(s).into_boxed_str(),
139            &[0, 0, 0, 3, b'a', b'b', b'c'],
140        );
141    }
142}