use std::fmt;
use crate::{Kind, OrbitalId};
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
Stale(OrbitalId),
KindMismatch {
id: OrbitalId,
expected: Kind,
},
UnknownKind(u8),
Malformed(Box<str>),
Full {
capacity: usize,
},
EpochExhausted,
Io(std::io::Error),
}
impl fmt::Display for Error {
fn fmt(
&self,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
match self {
Self::Stale(id) => write!(f, "orbital {id} is not in this table"),
Self::KindMismatch { id, expected } => {
write!(f, "orbital {id} is not {expected}")
}
Self::UnknownKind(kind) => {
write!(f, "unknown orbital kind byte {kind:#04x}")
}
Self::Malformed(text) => write!(f, "not an orbital id: {text:?}"),
Self::Full { capacity } => {
write!(f, "orbital table is full: capacity={capacity}")
}
Self::EpochExhausted => {
f.write_str("orbital table epoch is exhausted")
}
Self::Io(error) => write!(f, "orbital table io error: {error}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(error) => Some(error),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Self::Io(value)
}
}