use core::{fmt, result};
pub type LadataResult<N> = result::Result<N, LadataError>;
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LadataError {
NotEnoughElements(usize),
NotEnoughSpace(Option<usize>),
KeyAlreadyExists,
IndexOutOfBounds(usize),
Indices2dOutOfBounds(usize, usize),
ChunkIndices2dOutOfBounds(usize, usize, usize),
Overflow,
Underflow,
DimensionMismatch,
EmptyNode,
}
#[cfg(feature = "std")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "std")))]
impl std::error::Error for LadataError {}
impl fmt::Display for LadataError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LadataError::NotEnoughElements(n) => {
write!(f, "Not enough elements. Needs at least `{n}` elements.")
}
LadataError::NotEnoughSpace(n) => {
if let Some(n) = n {
write!(
f,
"Not enough space. Needs at least `{n}` free space for elements."
)
} else {
write!(f, "Not enough space.")
}
}
LadataError::KeyAlreadyExists => write!(f, "The key already exists."),
LadataError::IndexOutOfBounds(i) => write!(f, "Index {i} is out of bounds."),
LadataError::Indices2dOutOfBounds(i, j) => {
write!(f, "Indices 2d: {i}, {j} are out of bounds.")
}
LadataError::ChunkIndices2dOutOfBounds(i, j, k) => write!(
f,
"Indices 2d {i}, {j} are out of bounds for a chunk of length {k}."
),
LadataError::Overflow => write!(f, "Overflow."),
LadataError::Underflow => write!(f, "Underflow."),
LadataError::DimensionMismatch => write!(f, "Dimension Mismatch."),
LadataError::EmptyNode => write!(f, "The node is empty."),
}
}
}