use crate::plugin::{PluginId, PluginInfo};
mod encoding;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PluginState {
pub(crate) id: PluginId,
pub(crate) name: String,
pub(crate) vendor: String,
pub(crate) version: String,
pub(crate) component: Vec<u8>,
pub(crate) controller: Option<Vec<u8>>,
}
pub const MAX_STATE_BYTES: usize = 128 << 20;
impl PluginState {
#[doc(hidden)]
#[must_use]
pub fn new(info: &PluginInfo, component: Vec<u8>, controller: Option<Vec<u8>>) -> PluginState {
PluginState {
id: info.id.clone(),
name: info.name.clone(),
vendor: info.vendor.clone(),
version: info.version.clone(),
component,
controller,
}
}
pub fn id(&self) -> &PluginId {
&self.id
}
pub fn name(&self) -> &str {
&self.name
}
pub fn vendor(&self) -> &str {
&self.vendor
}
pub fn version(&self) -> &str {
&self.version
}
#[doc(hidden)]
pub fn component(&self) -> &[u8] {
&self.component
}
#[doc(hidden)]
pub fn controller(&self) -> Option<&[u8]> {
self.controller.as_deref()
}
pub fn validate(&self) -> Result<(), crate::InputError> {
let controller = self.controller.as_ref().map_or(0, Vec::len);
if self.component.len().saturating_add(controller) > MAX_STATE_BYTES {
return Err(crate::InputError::StateSize);
}
Ok(())
}
pub fn to_bytes(&self) -> Vec<u8> {
encoding::encode(self)
}
pub fn from_bytes(bytes: &[u8]) -> Result<PluginState, crate::InputError> {
encoding::decode(bytes)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum StatePurpose {
#[default]
Project,
Preset,
Duplicate,
}
pub const MAX_PRESET_BYTES: usize = 64 << 20;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PresetMetadata {
Vst3 {
info: Vec<u8>,
},
AudioUnit {
name: Option<String>,
},
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PresetInfo {
pub class_id: String,
pub metadata: PresetMetadata,
}
impl PresetInfo {
#[must_use]
pub fn new(class_id: impl Into<String>, metadata: PresetMetadata) -> PresetInfo {
PresetInfo {
class_id: class_id.into(),
metadata,
}
}
}