use std::fmt;
use crate::Need;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Missing {
pub need: Need,
pub detail: String,
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Refused {
backend: &'static str,
missing: Vec<Missing>,
},
NotDeclared(Need),
NotFound(String),
Backend(String),
Corrupt(String),
KindMismatch {
expected: &'static str,
found: String,
},
SchemaUnsupported {
kind: &'static str,
found: u32,
expected: u32,
},
UpgradeFailed {
kind: &'static str,
from: u32,
expected: u32,
detail: String,
},
Encode(String),
Injected(String),
Panicked(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Refused { backend, missing } => {
write!(f, "backend `{backend}` refused:")?;
for m in missing {
write!(f, " [{:?}: {}]", m.need, m.detail)?;
}
Ok(())
}
Error::NotDeclared(need) => write!(f, "{need:?} was not declared at open"),
Error::NotFound(key) => write!(f, "no value at key `{key}`"),
Error::Backend(msg) => write!(f, "backend error: {msg}"),
Error::Corrupt(msg) => write!(f, "stored record is corrupt: {msg}"),
Error::KindMismatch { expected, found } => {
write!(f, "record kind `{found}` found, `{expected}` expected")
}
Error::SchemaUnsupported {
kind,
found,
expected,
} => write!(
f,
"record `{kind}` has schema {found}; this program writes {expected} and has no way to read {found}"
),
Error::UpgradeFailed {
kind,
from,
expected,
detail,
} => write!(
f,
"record `{kind}` upgraded from schema {from} does not fit schema {expected}: {detail}"
),
Error::Encode(msg) => write!(f, "cannot encode record: {msg}"),
Error::Injected(msg) => write!(f, "injected fault: {msg}"),
Error::Panicked(msg) => write!(f, "operation panicked: {msg}"),
}
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;