wasmcloud_host/wasmbus/
experimental.rs

1use tracing::warn;
2
3/// Feature flags to enable experimental functionality in the host. Flags are disabled
4/// by default and must be explicitly enabled.
5#[derive(Copy, Clone, Debug, Default)]
6pub struct Features {
7    /// Enable the built-in HTTP client capability provider
8    /// that can be started with the reference wasmcloud+builtin://http-client
9    pub(crate) builtin_http_client: bool,
10    /// Enable the built-in HTTP server capability provider
11    /// that can be started with the reference wasmcloud+builtin://http-server
12    pub(crate) builtin_http_server: bool,
13    /// Enable the built-in NATS Messaging capability provider
14    /// that can be started with the reference wasmcloud+builtin://messaging-nats
15    pub(crate) builtin_messaging_nats: bool,
16    /// Enable the wasmcloud:messaging@v3 interface support in the host
17    pub(crate) wasmcloud_messaging_v3: bool,
18    /// Enable workload identity in the host that will be used for authenticating
19    /// into NATS
20    pub(crate) workload_identity_auth: bool,
21    /// Enable the wasmcloud:identity interface support in the runtime
22    pub(crate) workload_identity_interface: bool,
23    /// Enable the wrpc:rpc interface support in the runtime
24    pub(crate) rpc_interface: bool,
25}
26
27impl Features {
28    /// Create a new set of feature flags with all features disabled
29    pub fn new() -> Self {
30        Self::default()
31    }
32
33    /// Enable the built-in HTTP client capability provider
34    pub fn enable_builtin_http_client(mut self) -> Self {
35        self.builtin_http_client = true;
36        self
37    }
38
39    /// Enable the built-in HTTP server capability provider
40    pub fn enable_builtin_http_server(mut self) -> Self {
41        self.builtin_http_server = true;
42        self
43    }
44
45    /// Enable the built-in NATS messaging capability provider
46    pub fn enable_builtin_messaging_nats(mut self) -> Self {
47        self.builtin_messaging_nats = true;
48        self
49    }
50
51    /// Enable the wasmcloud:messaging@v3 interface support in the host
52    pub fn enable_wasmcloud_messaging_v3(mut self) -> Self {
53        self.wasmcloud_messaging_v3 = true;
54        self
55    }
56
57    /// Enable using workload identity for authenticating with NATS in the host
58    pub fn enable_workload_identity_auth(mut self) -> Self {
59        self.workload_identity_auth = true;
60        self
61    }
62
63    /// Enable wasmcloud:identity interface support in the runtime
64    pub fn enable_workload_identity_interface(mut self) -> Self {
65        self.workload_identity_interface = true;
66        self
67    }
68
69    /// Enable wrpc:rpc interface support in the runtime
70    pub fn enable_rpc_interface(mut self) -> Self {
71        self.rpc_interface = true;
72        self
73    }
74
75    /// Check if the built-in HTTP server capability provider is enabled
76    pub fn builtin_http_server_enabled(&self) -> bool {
77        self.builtin_http_server
78    }
79
80    /// Check if the built-in NATS messaging capability provider is enabled
81    pub fn builtin_messaging_nats_enabled(&self) -> bool {
82        self.builtin_messaging_nats
83    }
84
85    /// Check if the wasmcloud:messaging@v3 interface support is enabled
86    pub fn wasmcloud_messaging_v3_enabled(&self) -> bool {
87        self.wasmcloud_messaging_v3
88    }
89
90    /// Check if workload identity authentication is enabled
91    pub fn workload_identity_auth_enabled(&self) -> bool {
92        self.workload_identity_auth
93    }
94
95    /// Check if the wasmcloud:identity interface support is enabled
96    pub fn workload_identity_interface_enabled(&self) -> bool {
97        self.workload_identity_interface
98    }
99
100    /// Check if the wrpc:rpc interface support is enabled
101    pub fn rpc_interface_enabled(&self) -> bool {
102        self.rpc_interface
103    }
104}
105
106/// This enables unioning feature flags together
107impl std::ops::BitOr for Features {
108    type Output = Self;
109
110    fn bitor(self, rhs: Self) -> Self::Output {
111        Self {
112            builtin_http_client: self.builtin_http_client || rhs.builtin_http_client,
113            builtin_http_server: self.builtin_http_server || rhs.builtin_http_server,
114            builtin_messaging_nats: self.builtin_messaging_nats || rhs.builtin_messaging_nats,
115            wasmcloud_messaging_v3: self.wasmcloud_messaging_v3 || rhs.wasmcloud_messaging_v3,
116            workload_identity_auth: self.workload_identity_auth || rhs.workload_identity_auth,
117            workload_identity_interface: self.workload_identity_interface
118                || rhs.workload_identity_interface,
119            rpc_interface: self.rpc_interface || rhs.rpc_interface,
120        }
121    }
122}
123
124/// Allow for summing over a collection of feature flags
125impl std::iter::Sum for Features {
126    fn sum<I: Iterator<Item = Self>>(mut iter: I) -> Self {
127        // Grab the first set of flags, fall back on defaults (all disabled)
128        let first = iter.next().unwrap_or_default();
129        iter.fold(first, |a, b| a | b)
130    }
131}
132
133/// Parse a feature flag from a string, enabling the feature if the string matches
134impl From<&str> for Features {
135    fn from(s: &str) -> Self {
136        match &*s.to_ascii_lowercase() {
137            "builtin-http-client" | "builtin_http_client" => {
138                Self::new().enable_builtin_http_client()
139            }
140            "builtin-http-server" | "builtin_http_server" => {
141                Self::new().enable_builtin_http_server()
142            }
143            "builtin-messaging-nats" | "builtin_messaging_nats" => {
144                Self::new().enable_builtin_messaging_nats()
145            }
146            "wasmcloud-messaging-v3" | "wasmcloud_messaging_v3" => {
147                Self::new().enable_wasmcloud_messaging_v3()
148            }
149            "workload-identity-auth" | "workload_identity_auth" => {
150                Self::new().enable_workload_identity_auth()
151            }
152            "workload-identity-interface" | "workload_identity_interface" => {
153                Self::new().enable_workload_identity_interface()
154            }
155            "rpc-interface" | "rpc_interface" => Self::new().enable_rpc_interface(),
156            _ => {
157                warn!(%s, "unknown feature flag");
158                Self::new()
159            }
160        }
161    }
162}
163
164/// Convert the host feature flags to the runtime feature flags
165impl From<Features> for wasmcloud_runtime::experimental::Features {
166    fn from(f: Features) -> wasmcloud_runtime::experimental::Features {
167        wasmcloud_runtime::experimental::Features {
168            wasmcloud_messaging_v3: f.wasmcloud_messaging_v3,
169            workload_identity_interface: f.workload_identity_interface,
170            rpc_interface: f.rpc_interface,
171        }
172    }
173}