use std::{error, fmt};
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
SessionStreamConsumed,
InitialEvaluationIncomplete { operation: &'static str },
SessionStillEvaluating,
EvaluationFailed { message: String },
RuntimeUnavailable { message: String },
Internal { message: String },
}
pub type Result<T> = std::result::Result<T, Error>;
impl Error {
pub(crate) fn internal(err: anyhow::Error) -> Self {
Self::Internal {
message: err.to_string(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::SessionStreamConsumed => {
write!(f, "session stream has already been consumed")
},
Self::InitialEvaluationIncomplete { operation } => {
write!(
f,
"Session::{operation} requires a completed initial evaluation"
)
},
Self::SessionStillEvaluating => write!(f, "session is still evaluating"),
Self::EvaluationFailed { message }
| Self::RuntimeUnavailable { message }
| Self::Internal { message } => f.write_str(message),
}
}
}
impl error::Error for Error {}
impl From<anyhow::Error> for Error {
fn from(err: anyhow::Error) -> Self {
Self::internal(err)
}
}