use alloc::string::String;
use alloc::borrow::ToOwned;
use crate::{
codec::{Decode, Encode},
decoder::BufDecoder,
encoder::BufEncoder,
error::DecodeError
};
impl Encode for String {
fn encode(&self, enc: &mut BufEncoder) {
let b = self.as_bytes();
enc.write_u32(u32::try_from(b.len()).expect("String byte length exceeds u32::MAX"));
enc.write_bytes(b);
}
}
impl Decode for String {
fn decode(dec: &mut BufDecoder<'_>) -> Result<Self, DecodeError> {
let len = dec.read_u32()?;
let limit = dec.collection_limit();
if len > limit {
return Err(DecodeError::CollectionTooLarge { len, limit });
}
let bytes = dec.read_bytes(len as usize)?;
core::str::from_utf8(bytes)
.map(|s| s.to_owned())
.map_err(|_| DecodeError::InvalidUtf8)
}
}
impl Encode for str {
fn encode(&self, enc: &mut BufEncoder) {
let b = self.as_bytes();
enc.write_u32(b.len() as u32);
enc.write_bytes(b);
}
}
impl Encode for &str {
#[inline]
fn encode(&self, enc: &mut BufEncoder) {
(**self).encode(enc);
}
}