use core::fmt;
pub type Result<T> = core::result::Result<T, CrafterError>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CrafterError {
BufferTooShort {
context: &'static str,
required: usize,
available: usize,
},
InvalidMacAddress {
input: String,
reason: &'static str,
},
InvalidFieldValue {
field: &'static str,
reason: &'static str,
},
}
impl CrafterError {
pub fn buffer_too_short(context: &'static str, required: usize, available: usize) -> Self {
Self::BufferTooShort {
context,
required,
available,
}
}
pub fn invalid_mac_address(input: impl Into<String>, reason: &'static str) -> Self {
Self::InvalidMacAddress {
input: input.into(),
reason,
}
}
pub const fn invalid_field_value(field: &'static str, reason: &'static str) -> Self {
Self::InvalidFieldValue { field, reason }
}
}
impl fmt::Display for CrafterError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BufferTooShort {
context,
required,
available,
} => write!(
f,
"{context} requires {required} bytes, but only {available} bytes are available"
),
Self::InvalidMacAddress { input, reason } => {
write!(f, "invalid MAC address '{input}': {reason}")
}
Self::InvalidFieldValue { field, reason } => {
write!(f, "invalid value for {field}: {reason}")
}
}
}
}
impl std::error::Error for CrafterError {}