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
76/// This enables unioning feature flags together
77impl std::ops::BitOr for Features {
78    type Output = Self;
79
80    fn bitor(self, rhs: Self) -> Self::Output {
81        Self {
82            builtin_http_client: self.builtin_http_client || rhs.builtin_http_client,
83            builtin_http_server: self.builtin_http_server || rhs.builtin_http_server,
84            builtin_messaging_nats: self.builtin_messaging_nats || rhs.builtin_messaging_nats,
85            wasmcloud_messaging_v3: self.wasmcloud_messaging_v3 || rhs.wasmcloud_messaging_v3,
86            workload_identity_auth: self.workload_identity_auth || rhs.workload_identity_auth,
87            workload_identity_interface: self.workload_identity_interface
88                || rhs.workload_identity_interface,
89            rpc_interface: self.rpc_interface || rhs.rpc_interface,
90        }
91    }
92}
93
94/// Allow for summing over a collection of feature flags
95impl std::iter::Sum for Features {
96    fn sum<I: Iterator<Item = Self>>(mut iter: I) -> Self {
97        // Grab the first set of flags, fall back on defaults (all disabled)
98        let first = iter.next().unwrap_or_default();
99        iter.fold(first, |a, b| a | b)
100    }
101}
102
103/// Parse a feature flag from a string, enabling the feature if the string matches
104impl From<&str> for Features {
105    fn from(s: &str) -> Self {
106        match &*s.to_ascii_lowercase() {
107            "builtin-http-client" | "builtin_http_client" => {
108                Self::new().enable_builtin_http_client()
109            }
110            "builtin-http-server" | "builtin_http_server" => {
111                Self::new().enable_builtin_http_server()
112            }
113            "builtin-messaging-nats" | "builtin_messaging_nats" => {
114                Self::new().enable_builtin_messaging_nats()
115            }
116            "wasmcloud-messaging-v3" | "wasmcloud_messaging_v3" => {
117                Self::new().enable_wasmcloud_messaging_v3()
118            }
119            "workload-identity-auth" | "workload_identity_auth" => {
120                Self::new().enable_workload_identity_auth()
121            }
122            "workload-identity-interface" | "workload_identity_interface" => {
123                Self::new().enable_workload_identity_interface()
124            }
125            "rpc-interface" | "rpc_interface" => Self::new().enable_rpc_interface(),
126            _ => {
127                warn!(%s, "unknown feature flag");
128                Self::new()
129            }
130        }
131    }
132}
133
134/// Convert the host feature flags to the runtime feature flags
135impl From<Features> for wasmcloud_runtime::experimental::Features {
136    fn from(f: Features) -> wasmcloud_runtime::experimental::Features {
137        wasmcloud_runtime::experimental::Features {
138            wasmcloud_messaging_v3: f.wasmcloud_messaging_v3,
139            workload_identity_interface: f.workload_identity_interface,
140            rpc_interface: f.rpc_interface,
141        }
142    }
143}