wasmcloud_runtime/
experimental.rs

1use tracing::warn;
2
3/// Feature flags to enable experimental functionality in the runtime. Flags are disabled
4/// by default and must be explicitly enabled.
5#[derive(Copy, Clone, Debug, Default)]
6pub struct Features {
7    /// Enable the wasmcloud:messaging@v3 interface support in the runtime
8    pub wasmcloud_messaging_v3: bool,
9    /// Enable the wasmcloud:identity interface support in the runtime
10    pub workload_identity_interface: bool,
11}
12
13impl Features {
14    /// Create a new set of feature flags with all features disabled
15    #[must_use]
16    pub fn new() -> Self {
17        Self::default()
18    }
19
20    /// Enable the wasmcloud:messaging@v3 interface support in the runtime
21    #[must_use]
22    pub fn enable_wasmcloud_messaging_v3(mut self) -> Self {
23        self.wasmcloud_messaging_v3 = true;
24        self
25    }
26
27    /// Enable asmcloud:identity interface support in the runtime
28    #[must_use]
29    pub fn enable_workload_identity_interface(mut self) -> Self {
30        self.workload_identity_interface = true;
31        self
32    }
33}
34
35/// This enables unioning feature flags together
36impl std::ops::BitOr for Features {
37    type Output = Self;
38
39    fn bitor(self, rhs: Self) -> Self::Output {
40        Self {
41            wasmcloud_messaging_v3: self.wasmcloud_messaging_v3 || rhs.wasmcloud_messaging_v3,
42            workload_identity_interface: self.workload_identity_interface
43                || rhs.workload_identity_interface,
44        }
45    }
46}
47
48/// Allow for summing over a collection of feature flags
49impl std::iter::Sum for Features {
50    fn sum<I: Iterator<Item = Self>>(mut iter: I) -> Self {
51        // Grab the first set of flags, fall back on defaults (all disabled)
52        let first = iter.next().unwrap_or_default();
53        iter.fold(first, |a, b| a | b)
54    }
55}
56
57/// Parse a feature flag from a string, enabling the feature if the string matches
58impl From<&str> for Features {
59    fn from(s: &str) -> Self {
60        match &*s.to_ascii_lowercase() {
61            "wasmcloud-messaging-v3" | "wasmcloud_messaging_v3" => {
62                Self::new().enable_wasmcloud_messaging_v3()
63            }
64            "workload-identity-interface" | "workload_identity_interface" => {
65                Self::new().enable_workload_identity_interface()
66            }
67            _ => {
68                warn!(%s, "unknown feature flag");
69                Self::new()
70            }
71        }
72    }
73}