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    /// Enable the wrpc:rpc interface support in the runtime
12    pub rpc_interface: bool,
13}
14
15impl Features {
16    /// Create a new set of feature flags with all features disabled
17    #[must_use]
18    pub fn new() -> Self {
19        Self::default()
20    }
21
22    /// Enable the wasmcloud:messaging@v3 interface support in the runtime
23    #[must_use]
24    pub fn enable_wasmcloud_messaging_v3(mut self) -> Self {
25        self.wasmcloud_messaging_v3 = true;
26        self
27    }
28
29    /// Enable `wasmcloud:identity` interface support in the runtime
30    #[must_use]
31    pub fn enable_workload_identity_interface(mut self) -> Self {
32        self.workload_identity_interface = true;
33        self
34    }
35
36    /// Enable `wrpc:rpc` interface support in the runtime
37    #[must_use]
38    pub fn enable_rpc_interface(mut self) -> Self {
39        self.rpc_interface = true;
40        self
41    }
42}
43
44/// This enables unioning feature flags together
45impl std::ops::BitOr for Features {
46    type Output = Self;
47
48    fn bitor(self, rhs: Self) -> Self::Output {
49        Self {
50            wasmcloud_messaging_v3: self.wasmcloud_messaging_v3 || rhs.wasmcloud_messaging_v3,
51            workload_identity_interface: self.workload_identity_interface
52                || rhs.workload_identity_interface,
53            rpc_interface: self.rpc_interface || rhs.rpc_interface,
54        }
55    }
56}
57
58/// Allow for summing over a collection of feature flags
59impl std::iter::Sum for Features {
60    fn sum<I: Iterator<Item = Self>>(mut iter: I) -> Self {
61        // Grab the first set of flags, fall back on defaults (all disabled)
62        let first = iter.next().unwrap_or_default();
63        iter.fold(first, |a, b| a | b)
64    }
65}
66
67/// Parse a feature flag from a string, enabling the feature if the string matches
68impl From<&str> for Features {
69    fn from(s: &str) -> Self {
70        match &*s.to_ascii_lowercase() {
71            "wasmcloud-messaging-v3" | "wasmcloud_messaging_v3" => {
72                Self::new().enable_wasmcloud_messaging_v3()
73            }
74            "workload-identity-interface" | "workload_identity_interface" => {
75                Self::new().enable_workload_identity_interface()
76            }
77            "rpc-interface" | "rpc_interface" => Self::new().enable_rpc_interface(),
78            _ => {
79                warn!(%s, "unknown feature flag");
80                Self::new()
81            }
82        }
83    }
84}