use clap_sys::ext::log::{
CLAP_LOG_ERROR, CLAP_LOG_HOST_MISBEHAVING, CLAP_LOG_PLUGIN_MISBEHAVING, clap_log_severity,
};
use std::error::Error;
use std::fmt::{Display, Formatter};
#[derive(Debug)]
#[non_exhaustive]
pub enum FactoryWrapperError {
NullFactoryInstance,
NulPtr(&'static str),
Panic,
Error(clap_log_severity, Box<dyn Error>),
}
impl FactoryWrapperError {
pub fn severity(&self) -> clap_log_severity {
match self {
FactoryWrapperError::Panic => CLAP_LOG_PLUGIN_MISBEHAVING,
FactoryWrapperError::Error(s, _) => *s,
_ => CLAP_LOG_HOST_MISBEHAVING,
}
}
#[inline]
pub fn with_severity<E: 'static + Error>(
severity: clap_log_severity,
) -> impl Fn(E) -> FactoryWrapperError {
move |e| FactoryWrapperError::Error(severity, Box::new(e))
}
}
impl Display for FactoryWrapperError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
use FactoryWrapperError::*;
match self {
NullFactoryInstance => f.write_str(
"Plugin factory method was called with null clap_plugin_factory pointer",
),
NulPtr(ptr_name) => {
write!(
f,
"Plugin factory method was called with null {ptr_name} pointer"
)
}
Error(_, e) => std::fmt::Display::fmt(e, f),
Panic => f.write_str("Plugin factory panicked"),
}
}
}
impl<E: Error + 'static> From<E> for FactoryWrapperError {
fn from(value: E) -> Self {
Self::Error(CLAP_LOG_ERROR, Box::new(value))
}
}