#[derive(Debug, thiserror::Error)]
pub enum FixedStrError {
#[error("exceed max length limit")]
ExceedMaxLengthLimit,
#[error("invalid format")]
InvalidFormat,
#[error("utf8: {0}")]
Utf8(#[from] std::str::Utf8Error),
}
pub fn fixed_str_to_bytes<const MAX_LEN: usize>(
name: &str,
) -> Result<[u8; MAX_LEN], FixedStrError> {
let bytes = name.as_bytes();
if bytes.len() > MAX_LEN {
return Err(FixedStrError::ExceedMaxLengthLimit);
}
let mut buffer = [0; MAX_LEN];
buffer[..bytes.len()].copy_from_slice(bytes);
Ok(buffer)
}
pub fn bytes_to_fixed_str<const MAX_LEN: usize>(
bytes: &[u8; MAX_LEN],
) -> Result<&str, FixedStrError> {
let Some(end) = bytes.iter().position(|&x| x == 0) else {
return Err(FixedStrError::InvalidFormat);
};
let valid_bytes = &bytes[..end];
Ok(std::str::from_utf8(valid_bytes)?)
}