Skip to main content

sim_lib_plugin_core/
capability.rs

1use std::collections::BTreeSet;
2
3use sim_kernel::{CapabilityName, Error, Result};
4
5/// Privileged audio plugin operations exposed by host adapters.
6#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub enum AudioPluginCapability {
8    /// Loading WebAssembly audio plugins.
9    WasmPlugin,
10    /// Loading native audio plugins.
11    NativePlugin,
12}
13
14impl AudioPluginCapability {
15    /// Returns the stable kernel capability name for this plugin operation.
16    pub fn as_capability_name(self) -> CapabilityName {
17        match self {
18            Self::WasmPlugin => CapabilityName::new("plugin.audio.wasm"),
19            Self::NativePlugin => CapabilityName::new("plugin.audio.native"),
20        }
21    }
22}
23
24/// Granted audio plugin capabilities for loader entry points.
25#[derive(Clone, Debug, Default, PartialEq, Eq)]
26pub struct CapabilitySet {
27    granted: BTreeSet<AudioPluginCapability>,
28}
29
30impl CapabilitySet {
31    /// Builds an empty plugin capability set.
32    pub fn empty() -> Self {
33        Self::default()
34    }
35
36    /// Builds a plugin capability set containing one capability.
37    pub fn with(capability: AudioPluginCapability) -> Self {
38        Self::empty().grant(capability)
39    }
40
41    /// Returns this set with `capability` granted.
42    pub fn grant(mut self, capability: AudioPluginCapability) -> Self {
43        self.granted.insert(capability);
44        self
45    }
46
47    /// Grants `capability` in place.
48    pub fn insert(&mut self, capability: AudioPluginCapability) {
49        self.granted.insert(capability);
50    }
51
52    /// Reports whether `capability` is granted.
53    pub fn contains(&self, capability: AudioPluginCapability) -> bool {
54        self.granted.contains(&capability)
55    }
56
57    /// Requires `capability`, returning a structured capability error when absent.
58    pub fn require(&self, capability: AudioPluginCapability) -> Result<()> {
59        if self.contains(capability) {
60            Ok(())
61        } else {
62            Err(Error::CapabilityDenied {
63                capability: capability.as_capability_name(),
64            })
65        }
66    }
67}