use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
NotEnrolled,
PeerNotFound(String),
StreamFailed(String),
EnrollmentFailed(String),
CoordinatorRequired(&'static str),
ListenerUnavailable,
ListenerTaken,
InvalidConfig(String),
Io(std::io::Error),
Internal(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotEnrolled => write!(
f,
"no persisted identity; run enroll() first or pass api_key credentials"
),
Self::PeerNotFound(host) => write!(f, "no peer matches host {host}"),
Self::StreamFailed(msg) => write!(f, "stream failed: {msg}"),
Self::EnrollmentFailed(msg) => write!(f, "enrollment failed: {msg}"),
Self::CoordinatorRequired(op) => {
write!(f, "{op} requires the coordinator process")
}
Self::ListenerUnavailable => {
write!(
f,
"stream listener is not available in coordinator client mode"
)
}
Self::ListenerTaken => write!(f, "stream listener already taken"),
Self::InvalidConfig(msg) => write!(f, "invalid config: {msg}"),
Self::Io(e) => write!(f, "io error: {e}"),
Self::Internal(msg) => write!(f, "{msg}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Self::Io(value)
}
}
impl Error {
pub(crate) fn from_anyhow(e: impl fmt::Display) -> Self {
Self::Internal(e.to_string())
}
pub(crate) fn enrollment(e: impl fmt::Display) -> Self {
Self::EnrollmentFailed(e.to_string())
}
pub(crate) fn stream(e: impl fmt::Display) -> Self {
Self::StreamFailed(e.to_string())
}
}