gmsol_store/utils/
fixed_str.rs

1use anchor_lang::{error, Result};
2use gmsol_utils::fixed_str::{self, FixedStrError};
3
4use crate::CoreError;
5
6/// Fixed size string to bytes.
7pub fn fixed_str_to_bytes<const MAX_LEN: usize>(name: &str) -> Result<[u8; MAX_LEN]> {
8    fixed_str::fixed_str_to_bytes(name)
9        .map_err(CoreError::from)
10        .map_err(|err| error!(err))
11}
12
13/// Bytes to fixed size string.
14pub fn bytes_to_fixed_str<const MAX_LEN: usize>(bytes: &[u8; MAX_LEN]) -> Result<&str> {
15    fixed_str::bytes_to_fixed_str(bytes)
16        .map_err(CoreError::from)
17        .map_err(|err| error!(err))
18}
19
20impl From<FixedStrError> for CoreError {
21    fn from(err: FixedStrError) -> Self {
22        anchor_lang::prelude::msg!("Fixed Str Error: {}", err);
23        match err {
24            FixedStrError::ExceedMaxLengthLimit => Self::ExceedMaxLengthLimit,
25            FixedStrError::InvalidFormat | FixedStrError::Utf8(_) => Self::InvalidArgument,
26        }
27    }
28}