Skip to content

Commit

Permalink
Move PluginInstanceError to the plugin module instead of the host module
Browse files Browse the repository at this point in the history
  • Loading branch information
prokopyl committed May 17, 2024
1 parent da1f707 commit 02e262f
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 108 deletions.
2 changes: 1 addition & 1 deletion host/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//! See the [`PluginFactory`]'s type documentation for more detail and examples on how to
//! list plugins.

use crate::host::PluginInstanceError;
use crate::plugin::PluginInstanceError;
use clap_sys::factory::plugin_factory::{clap_plugin_factory, CLAP_PLUGIN_FACTORY_ID};
use clap_sys::host::clap_host;
use clap_sys::plugin::clap_plugin;
Expand Down
4 changes: 2 additions & 2 deletions host/src/host.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![deny(missing_docs)]

//! Core types and traits to implement a Clack host.
//! Core types and traits to implement CLAP host callback handlers.
//!
//! The [`HostHandlers`] trait is the main one required to be implemented for a Clack host. It provides
//! the host's supported extensions and is associated to a main type implementing [`SharedHandler`] ,
Expand Down Expand Up @@ -193,7 +193,7 @@ mod error;
mod extensions;
mod info;

pub use error::{HostError, PluginInstanceError};
pub use error::HostError;
pub use extensions::HostExtensions;
pub use info::HostInfo;

Expand Down
98 changes: 0 additions & 98 deletions host/src/host/error.rs
Original file line number Diff line number Diff line change
@@ -1,105 +1,7 @@
use crate::host::HostHandlers;
use crate::process::ProcessingStartError;
use clap_sys::ext::log::{clap_log_severity, CLAP_LOG_ERROR, CLAP_LOG_PLUGIN_MISBEHAVING};
use core::fmt;
use core::fmt::{Debug, Display, Formatter};
use std::error::Error;

/// All errors that can arise using plugin instances.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PluginInstanceError {
/// The plugin's audio processing could not be started.
StartProcessingFailed,
/// Tried to activate a plugin that was already activated.
AlreadyActivatedPlugin,
/// Tried to deactivate a plugin instance while its audio processor was still alive.
StillActivatedPlugin,
/// Attempted to perform an operation on the plugin instance's audio processor, but it was
/// not activated yet.
DeactivatedPlugin,
/// The plugin instance's audio processor's activation failed.
ActivationFailed,
/// No plugin with a matching ID was found during instantiation.
PluginNotFound,
/// Tried to instantiate a plugin from a bundle which lacks a [`PluginFactory`](crate::factory::PluginFactory).
///
/// This is a sign of a misbehaving plugin implementation.
MissingPluginFactory,
/// The plugin's instantiation failed.
InstantiationFailed,
/// The plugin has already been destroyed.
PluginDestroyed,
/// The plugin's audio processing failed.
ProcessingFailed,
/// Tried to perform or stop processing when the audio processor was not started yet.
ProcessingStopped,
/// Tried to start processing when the processing was already started.
ProcessingStarted,
/// The underlying plugin's `create_plugin` C function was a null pointer.
///
/// This is a sign of a misbehaving plugin implementation.
NullFactoryCreatePluginFunction,
/// The underlying plugin's `process` C function was a null pointer.
///
/// This is a sign of a misbehaving plugin implementation.
NullProcessFunction,
/// The underlying plugin's `activate` C function was a null pointer.
///
/// This is a sign of a misbehaving plugin implementation.
NullActivateFunction,
}

impl PluginInstanceError {
pub(crate) fn msg(&self) -> &'static str {
match self {
Self::StartProcessingFailed => "Could not start processing",
Self::AlreadyActivatedPlugin => "Plugin was already activated",
Self::StillActivatedPlugin => {
"Attempted to deactivate Plugin which still has an active AudioProcessor"
}
Self::DeactivatedPlugin => "Plugin is currently deactivated",
Self::ActivationFailed => "Unable to activate",
Self::PluginNotFound => "Specified plugin was not found",
Self::MissingPluginFactory => "No plugin factory was provided",
Self::InstantiationFailed => "Could not instantiate",
Self::PluginDestroyed => "Plugin was destroyed",
Self::ProcessingFailed => "Could not process",
Self::ProcessingStopped => "Audio Processor is currently stopped",
Self::ProcessingStarted => "Audio Processor is currently started",
Self::NullProcessFunction => "Plugin's process function is null",
Self::NullActivateFunction => "Plugin's activate function is null",
Self::NullFactoryCreatePluginFunction => {
"Plugin Factory's create_plugin function is null"
}
}
}

pub(crate) fn severity(&self) -> clap_log_severity {
match self {
PluginInstanceError::MissingPluginFactory => CLAP_LOG_PLUGIN_MISBEHAVING,
PluginInstanceError::NullFactoryCreatePluginFunction => CLAP_LOG_PLUGIN_MISBEHAVING,
PluginInstanceError::NullProcessFunction => CLAP_LOG_PLUGIN_MISBEHAVING,
PluginInstanceError::NullActivateFunction => CLAP_LOG_PLUGIN_MISBEHAVING,
_ => CLAP_LOG_ERROR,
}
}
}

impl Display for PluginInstanceError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str(self.msg())
}
}

impl Error for PluginInstanceError {}

impl<H: HostHandlers> From<ProcessingStartError<H>> for PluginInstanceError {
#[inline]
fn from(_: ProcessingStartError<H>) -> Self {
Self::StartProcessingFailed
}
}

/// A generic, type-erased error type for host-originating errors.
///
/// Errors are type-erased because the CLAP API does not support extracting error information from
Expand Down
7 changes: 2 additions & 5 deletions host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,6 @@
//! # Ok(()) }
//! ```

extern crate core;

pub mod bundle;
pub mod extensions;
pub mod factory;
Expand All @@ -283,12 +281,11 @@ pub mod prelude {
},
host::{
AudioProcessorHandler, HostError, HostExtensions, HostHandlers, HostInfo,
MainThreadHandler, PluginInstanceError, SharedHandler,
MainThreadHandler, SharedHandler,
},
plugin::PluginInstance,
plugin::{
InitializedPluginHandle, InitializingPluginHandle, PluginAudioProcessorHandle,
PluginMainThreadHandle, PluginSharedHandle,
PluginInstance, PluginInstanceError, PluginMainThreadHandle, PluginSharedHandle,
},
process::{
audio_buffers::{
Expand Down
2 changes: 2 additions & 0 deletions host/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use std::marker::PhantomData;
use std::mem::ManuallyDrop;
use std::sync::Arc;

mod error;
mod handle;
pub(crate) mod instance;

pub use error::PluginInstanceError;
pub use handle::*;
use instance::*;

Expand Down
101 changes: 101 additions & 0 deletions host/src/plugin/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use crate::host::HostHandlers;
use crate::process::ProcessingStartError;
use clap_sys::ext::log::{clap_log_severity, CLAP_LOG_ERROR, CLAP_LOG_PLUGIN_MISBEHAVING};
use core::fmt;
use core::fmt::{Debug, Display, Formatter};
use std::error::Error;

/// All errors that can arise using plugin instances.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PluginInstanceError {
/// The plugin's audio processing could not be started.
StartProcessingFailed,
/// Tried to activate a plugin that was already activated.
AlreadyActivatedPlugin,
/// Tried to deactivate a plugin instance while its audio processor was still alive.
StillActivatedPlugin,
/// Attempted to perform an operation on the plugin instance's audio processor, but it was
/// not activated yet.
DeactivatedPlugin,
/// The plugin instance's audio processor's activation failed.
ActivationFailed,
/// No plugin with a matching ID was found during instantiation.
PluginNotFound,
/// Tried to instantiate a plugin from a bundle which lacks a [`PluginFactory`](crate::factory::PluginFactory).
///
/// This is a sign of a misbehaving plugin implementation.
MissingPluginFactory,
/// The plugin's instantiation failed.
InstantiationFailed,
/// The plugin has already been destroyed.
PluginDestroyed,
/// The plugin's audio processing failed.
ProcessingFailed,
/// Tried to perform or stop processing when the audio processor was not started yet.
ProcessingStopped,
/// Tried to start processing when the processing was already started.
ProcessingStarted,
/// The underlying plugin's `create_plugin` C function was a null pointer.
///
/// This is a sign of a misbehaving plugin implementation.
NullFactoryCreatePluginFunction,
/// The underlying plugin's `process` C function was a null pointer.
///
/// This is a sign of a misbehaving plugin implementation.
NullProcessFunction,
/// The underlying plugin's `activate` C function was a null pointer.
///
/// This is a sign of a misbehaving plugin implementation.
NullActivateFunction,
}

impl PluginInstanceError {
pub(crate) fn msg(&self) -> &'static str {
match self {
Self::StartProcessingFailed => "Could not start processing",
Self::AlreadyActivatedPlugin => "Plugin was already activated",
Self::StillActivatedPlugin => {
"Attempted to deactivate Plugin which still has an active AudioProcessor"
}
Self::DeactivatedPlugin => "Plugin is currently deactivated",
Self::ActivationFailed => "Unable to activate",
Self::PluginNotFound => "Specified plugin was not found",
Self::MissingPluginFactory => "No plugin factory was provided",
Self::InstantiationFailed => "Could not instantiate",
Self::PluginDestroyed => "Plugin was destroyed",
Self::ProcessingFailed => "Could not process",
Self::ProcessingStopped => "Audio Processor is currently stopped",
Self::ProcessingStarted => "Audio Processor is currently started",
Self::NullProcessFunction => "Plugin's process function is null",
Self::NullActivateFunction => "Plugin's activate function is null",
Self::NullFactoryCreatePluginFunction => {
"Plugin Factory's create_plugin function is null"
}
}
}

pub(crate) fn severity(&self) -> clap_log_severity {
match self {
PluginInstanceError::MissingPluginFactory => CLAP_LOG_PLUGIN_MISBEHAVING,
PluginInstanceError::NullFactoryCreatePluginFunction => CLAP_LOG_PLUGIN_MISBEHAVING,
PluginInstanceError::NullProcessFunction => CLAP_LOG_PLUGIN_MISBEHAVING,
PluginInstanceError::NullActivateFunction => CLAP_LOG_PLUGIN_MISBEHAVING,
_ => CLAP_LOG_ERROR,
}
}
}

impl Display for PluginInstanceError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str(self.msg())
}
}

impl Error for PluginInstanceError {}

impl<H: HostHandlers> From<ProcessingStartError<H>> for PluginInstanceError {
#[inline]
fn from(_: ProcessingStartError<H>) -> Self {
Self::StartProcessingFailed
}
}
3 changes: 1 addition & 2 deletions host/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

use self::audio_buffers::InputAudioBuffers;
use crate::host::HostHandlers;
use crate::host::PluginInstanceError;
use crate::plugin::{PluginAudioProcessorHandle, PluginSharedHandle};
use crate::plugin::{PluginAudioProcessorHandle, PluginInstanceError, PluginSharedHandle};
use crate::prelude::{OutputAudioBuffers, PluginInstance};
use crate::process::PluginAudioProcessor::*;
use clack_common::events::event_types::TransportEvent;
Expand Down

0 comments on commit 02e262f

Please sign in to comment.