use std::fmt;
use std::path::PathBuf;
#[derive(Debug)]
pub enum LoadError {
Open {
path: PathBuf,
message: String,
},
NoEntrySymbol {
path: PathBuf,
symbol: &'static str,
},
Major {
path: PathBuf,
found: u32,
expected: u32,
},
MissingCapability {
path: PathBuf,
capability: &'static str,
},
ShortTable {
path: PathBuf,
capability: &'static str,
found: usize,
expected: usize,
},
}
impl fmt::Display for LoadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LoadError::Open { path, message } => {
write!(f, "cannot load the plugin at {}: {message}", path.display())
}
LoadError::NoEntrySymbol { path, symbol } => write!(
f,
"{} exports no {symbol}: it was built against a different version of the plugin \
contract, so it has to be rebuilt",
path.display()
),
LoadError::Major {
path,
found,
expected,
} => write!(
f,
"the plugin at {} speaks ABI major version {found}, this pmpx speaks {expected}; \
one of the two has to be updated",
path.display()
),
LoadError::MissingCapability { path, capability } => write!(
f,
"the plugin at {} provides no `{capability}` capability, which this pmpx needs",
path.display()
),
LoadError::ShortTable {
path,
capability,
found,
expected,
} => write!(
f,
"the plugin at {} says its `{capability}` table is {found} bytes, but {expected} \
are needed to read it safely",
path.display()
),
}
}
}
impl std::error::Error for LoadError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CallError {
UnsupportedVerb,
InvalidArgs,
Internal,
Unknown(u32),
ShortCommand {
found: usize,
expected: usize,
},
}
impl CallError {
pub fn code(&self) -> Option<u32> {
match self {
CallError::Unknown(code) => Some(*code),
_ => None,
}
}
}
impl fmt::Display for CallError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CallError::UnsupportedVerb => f.write_str("it does not support that verb"),
CallError::InvalidArgs => f.write_str("it thinks the arguments are invalid"),
CallError::Internal => {
f.write_str("it failed internally or panicked (details on its own stderr)")
}
CallError::Unknown(code) => write!(f, "it answered with unknown code {code}"),
CallError::ShortCommand { found, expected } => write!(
f,
"it answered with a {found}-byte command structure, but {expected} bytes are \
needed to read it safely"
),
}
}
}
impl std::error::Error for CallError {}