use weirdnet_media::waveform::WaveformError;
#[derive(Debug, Clone, PartialEq)]
pub enum PluginError {
InvalidParam { name: String, reason: String },
UnsupportedFormat,
TooLong { max_frames: usize },
Internal(String),
}
impl PluginError {
pub(crate) fn invalid(name: &str, reason: impl Into<String>) -> Self {
PluginError::InvalidParam { name: name.to_string(), reason: reason.into() }
}
}
impl std::fmt::Display for PluginError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PluginError::InvalidParam { name, reason } => write!(f, "parameter {name}: {reason}"),
PluginError::UnsupportedFormat => write!(f, "the plugin does not support this waveform's format"),
PluginError::TooLong { max_frames } => write!(f, "the result would be longer than {max_frames} frames"),
PluginError::Internal(why) => write!(f, "{why}"),
}
}
}
impl std::error::Error for PluginError {}
impl From<WaveformError> for PluginError {
fn from(e: WaveformError) -> Self {
match e {
WaveformError::UnsupportedChannels { .. } => PluginError::UnsupportedFormat,
WaveformError::TooLong { max_frames } => PluginError::TooLong { max_frames },
WaveformError::OutOfRange { what, .. } => PluginError::invalid(what, e.to_string()),
WaveformError::NotFinite(what) => PluginError::invalid(what, e.to_string()),
other => PluginError::Internal(other.to_string()),
}
}
}