sim-lib-plugin-lv2 0.1.2

LV2-shaped plugin adapters for SIM audio graph processors.
Documentation
//! Deterministic cookbook builders for LV2 plugin recipes.

use sim_kernel::{Expr, NumberLiteral, Symbol};

use crate::lv2_gain_lv2_descriptor;

/// Build the modeled LV2 gain descriptor used by the cookbook recipe.
pub fn lv2_gain_demo() -> Expr {
    let descriptor = lv2_gain_lv2_descriptor().expect("valid LV2 gain descriptor");
    let plugin = descriptor
        .to_plugin_descriptor()
        .expect("LV2 gain lowers to plugin-core descriptor");
    Expr::Map(vec![
        (field("kind"), sym("plugin-lv2", "descriptor")),
        (field("uri"), Expr::String(descriptor.uri)),
        (field("name"), Expr::String(descriptor.name)),
        (field("ports"), number(plugin.ports.len())),
        (field("parameters"), number(plugin.parameters.len())),
        (
            field("format"),
            sym("plugin-format", plugin.id.format.as_str()),
        ),
    ])
}

fn field(name: &str) -> Expr {
    Expr::Symbol(Symbol::qualified("plugin-lv2", name))
}

fn sym(namespace: &str, name: &str) -> Expr {
    Expr::Symbol(Symbol::qualified(namespace, name))
}

fn number(value: impl ToString) -> Expr {
    Expr::Number(NumberLiteral {
        domain: Symbol::qualified("numbers", "i64"),
        canonical: value.to_string(),
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn lv2_gain_demo_lowers_to_core_plugin_descriptor() {
        let Expr::Map(entries) = lv2_gain_demo() else {
            panic!("LV2 demo is a map")
        };
        assert!(entries.iter().any(|(_, value)| {
            matches!(value, Expr::Symbol(symbol) if symbol.as_qualified_str() == "plugin-format/lv2")
        }));
    }
}