plughost-core 0.2.6

Shared types and offline render rules for plughost
Documentation
use crate::InputError;

/// Identity of the application hosting the plugins. VST3 receives the name; CLAP receives all
/// three fields. Audio Units have no equivalent identity callback in the current host API.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct HostIdentity {
    /// Non-empty, at most 127 UTF-16 code units (the VST3 String128 limit).
    pub name: String,
    /// Application vendor; must not contain NUL.
    pub vendor: String,
    /// Application version; must not contain NUL.
    pub version: String,
}

impl HostIdentity {
    /// The host name, vendor and version plugins are told; validated when it is used.
    #[must_use]
    pub fn new(
        name: impl Into<String>,
        vendor: impl Into<String>,
        version: impl Into<String>,
    ) -> HostIdentity {
        HostIdentity {
            name: name.into(),
            vendor: vendor.into(),
            version: version.into(),
        }
    }

    /// Validates the common identity without truncating strings at a format boundary.
    pub fn validate(&self) -> Result<(), InputError> {
        if self.name.is_empty()
            || self.name.encode_utf16().count() > 127
            || [&self.name, &self.vendor, &self.version]
                .iter()
                .any(|s| s.contains('\0'))
        {
            return Err(InputError::HostIdentity);
        }
        Ok(())
    }
}

/// Identity for standalone plughost tools and tests. Applications should supply their own.
impl Default for HostIdentity {
    fn default() -> Self {
        Self {
            name: "plughost".to_owned(),
            vendor: "plughost".to_owned(),
            version: env!("CARGO_PKG_VERSION").to_owned(),
        }
    }
}