use std::fmt;
use crate::Verb;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PluginError {
UnsupportedVerb(Verb),
InvalidArgs(String),
Other(String),
}
impl PluginError {
pub fn unsupported_verb(verb: Verb) -> Self {
PluginError::UnsupportedVerb(verb)
}
pub fn invalid_args(message: impl Into<String>) -> Self {
PluginError::InvalidArgs(message.into())
}
pub fn other(message: impl Into<String>) -> Self {
PluginError::Other(message.into())
}
pub fn code(&self) -> u32 {
match self {
PluginError::UnsupportedVerb(_) => pmpx_plugin_abi::PMPX_ERR_UNSUPPORTED_VERB,
PluginError::InvalidArgs(_) => pmpx_plugin_abi::PMPX_ERR_INVALID_ARGS,
PluginError::Other(_) => pmpx_plugin_abi::PMPX_ERR_INTERNAL,
}
}
pub fn message(&self) -> String {
match self {
PluginError::UnsupportedVerb(v) => format!("unsupported verb {v}"),
PluginError::InvalidArgs(m) => m.clone(),
PluginError::Other(m) => m.clone(),
}
}
}
impl fmt::Display for PluginError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message())
}
}
impl std::error::Error for PluginError {}
impl From<String> for PluginError {
fn from(message: String) -> Self {
PluginError::Other(message)
}
}
impl From<&str> for PluginError {
fn from(message: &str) -> Self {
PluginError::Other(message.to_string())
}
}
impl From<std::io::Error> for PluginError {
fn from(e: std::io::Error) -> Self {
PluginError::Other(e.to_string())
}
}