solidity_abi/
string.rs

1use super::{bytes::Bytes, ABIDecodeError, ABIVariable, LinkedBytes};
2use std::str::from_utf8;
3
4impl ABIVariable for String {
5    const BASIC_TYPE: bool = false;
6    const STATIC_LENGTH: Option<usize> = None;
7
8    fn from_abi(data: &[u8]) -> Result<Self, ABIDecodeError> {
9        let byte_array = Bytes::from_abi(data)?;
10        from_utf8(&byte_array)
11            .and_then(|s| Ok(s.to_string()))
12            .map_err(|_| ABIDecodeError("Utf8 decoding error"))
13    }
14
15    fn to_abi(&self) -> LinkedBytes { self.as_bytes().to_vec().to_abi() }
16
17    fn to_packed_abi(&self) -> LinkedBytes {
18        self.as_bytes().to_vec().to_packed_abi()
19    }
20}