Skip to main content

weirdnet_audio_plugin/
error.rs

1use weirdnet_media::waveform::WaveformError;
2
3/// Why a plugin could not process a waveform.
4#[derive(Debug, Clone, PartialEq)]
5pub enum PluginError {
6    /// A parameter is unknown, has the wrong type, or is outside its range.
7    InvalidParam { name: String, reason: String },
8    /// The plugin does not work on this waveform's format, for example pan on a mono waveform.
9    UnsupportedFormat,
10    /// The result would be longer than the limit.
11    TooLong { max_frames: usize },
12    /// Anything else.
13    Internal(String),
14}
15
16impl PluginError {
17    pub(crate) fn invalid(name: &str, reason: impl Into<String>) -> Self {
18        PluginError::InvalidParam { name: name.to_string(), reason: reason.into() }
19    }
20}
21
22impl std::fmt::Display for PluginError {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        match self {
25            PluginError::InvalidParam { name, reason } => write!(f, "parameter {name}: {reason}"),
26            PluginError::UnsupportedFormat => write!(f, "the plugin does not support this waveform's format"),
27            PluginError::TooLong { max_frames } => write!(f, "the result would be longer than {max_frames} frames"),
28            PluginError::Internal(why) => write!(f, "{why}"),
29        }
30    }
31}
32
33impl std::error::Error for PluginError {}
34
35impl From<WaveformError> for PluginError {
36    fn from(e: WaveformError) -> Self {
37        match e {
38            WaveformError::UnsupportedChannels { .. } => PluginError::UnsupportedFormat,
39            WaveformError::TooLong { max_frames } => PluginError::TooLong { max_frames },
40            WaveformError::OutOfRange { what, .. } => PluginError::invalid(what, e.to_string()),
41            WaveformError::NotFinite(what) => PluginError::invalid(what, e.to_string()),
42            other => PluginError::Internal(other.to_string()),
43        }
44    }
45}