use super::matter_code::MatterCode;
use super::sealed::Sealed;
use crate::core::matter::error::ValidationError;
use crate::core::matter::sizage::Sizage;
#[cfg(feature = "alloc")]
use crate::core::matter::sizage::SizeType;
#[cfg(feature = "alloc")]
#[allow(
unused_imports,
reason = "alloc prelude items; used only by the `placeholder` default method"
)]
use alloc::string::{String, ToString};
pub const DUMMY_CHAR: char = '#';
#[allow(
private_bounds,
reason = "Sealed trait pattern restricts implementors to this crate"
)]
pub trait CesrCode: Sealed + Copy + Eq + core::fmt::Debug {
fn to_matter_code(&self) -> MatterCode;
fn as_str(&self) -> &'static str;
fn get_sizage(&self) -> Sizage {
self.to_matter_code().get_sizage()
}
fn raw_size(&self) -> Result<usize, ValidationError> {
self.to_matter_code().raw_size()
}
#[cfg(feature = "alloc")]
fn placeholder(&self) -> Result<String, ValidationError> {
match self.get_sizage().fs {
SizeType::Fixed(n) => Ok(core::iter::repeat_n(DUMMY_CHAR, usize::from(n)).collect()),
SizeType::Small | SizeType::Large => Err(ValidationError::InvalidSizingOperation(
self.to_matter_code().to_string(),
)),
}
}
}