use core::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum SessionError {
TooLong {
len: usize,
limit: usize,
},
SpaceExhausted {
needed: u64,
available: u64,
},
}
impl fmt::Display for SessionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TooLong { len, limit } => write!(
f,
"entry too long: {len} bytes exceeds the session limit of {limit} bytes"
),
Self::SpaceExhausted { needed, available } => write!(
f,
"session source space exhausted: entry needs {needed} bytes, \
{available} remain"
),
}
}
}
impl core::error::Error for SessionError {}
#[cfg(test)]
mod tests {
use alloc::string::ToString;
use super::*;
#[test]
fn test_display_too_long_names_both_sizes() {
let text = SessionError::TooLong { len: 12, limit: 8 }.to_string();
assert_eq!(
text,
"entry too long: 12 bytes exceeds the session limit of 8 bytes"
);
}
#[test]
fn test_display_space_exhausted_names_both_sizes() {
let text = SessionError::SpaceExhausted {
needed: 9,
available: 2,
}
.to_string();
assert_eq!(
text,
"session source space exhausted: entry needs 9 bytes, 2 remain"
);
}
#[test]
fn test_error_trait_is_implemented() {
fn assert_error<E: core::error::Error + Send + Sync + 'static>() {}
assert_error::<SessionError>();
}
}