wasmcloud_host/wasmbus/
experimental.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use tracing::warn;

/// Feature flags to enable experimental functionality in the host. Flags are disabled
/// by default and must be explicitly enabled.
#[derive(Copy, Clone, Debug, Default)]
pub struct Features {
    /// Enable the built-in HTTP server capability provider
    /// that can be started with the reference wasmcloud+builtin://http-server
    pub(crate) builtin_http_server: bool,
    /// Enable the built-in NATS Messaging capability provider
    /// that can be started with the reference wasmcloud+builtin://messaging-nats
    pub(crate) builtin_messaging_nats: bool,
    /// Enable the wasmcloud:messaging@v3 interface support in the host
    pub(crate) wasmcloud_messaging_v3: bool,
}

impl Features {
    /// Create a new set of feature flags with all features disabled
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable the built-in HTTP server capability provider
    pub fn enable_builtin_http_server(mut self) -> Self {
        self.builtin_http_server = true;
        self
    }

    /// Enable the built-in NATS messaging capability provider
    pub fn enable_builtin_messaging_nats(mut self) -> Self {
        self.builtin_messaging_nats = true;
        self
    }

    /// Enable the wasmcloud:messaging@v3 interface support in the host
    pub fn enable_wasmcloud_messaging_v3(mut self) -> Self {
        self.wasmcloud_messaging_v3 = true;
        self
    }
}

/// This enables unioning feature flags together
impl std::ops::BitOr for Features {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        Self {
            builtin_http_server: self.builtin_http_server || rhs.builtin_http_server,
            builtin_messaging_nats: self.builtin_messaging_nats || rhs.builtin_messaging_nats,
            wasmcloud_messaging_v3: self.wasmcloud_messaging_v3 || rhs.wasmcloud_messaging_v3,
        }
    }
}

/// Allow for summing over a collection of feature flags
impl std::iter::Sum for Features {
    fn sum<I: Iterator<Item = Self>>(mut iter: I) -> Self {
        // Grab the first set of flags, fall back on defaults (all disabled)
        let first = iter.next().unwrap_or_default();
        iter.fold(first, |a, b| a | b)
    }
}

/// Parse a feature flag from a string, enabling the feature if the string matches
impl From<&str> for Features {
    fn from(s: &str) -> Self {
        match &*s.to_ascii_lowercase() {
            "builtin-http-server" | "builtin_http_server" => {
                Self::new().enable_builtin_http_server()
            }
            "builtin-messaging-nats" | "builtin_messaging_nats" => {
                Self::new().enable_builtin_messaging_nats()
            }
            "wasmcloud-messaging-v3" | "wasmcloud_messaging_v3" => {
                Self::new().enable_wasmcloud_messaging_v3()
            }
            _ => {
                warn!(%s, "unknown feature flag");
                Self::new()
            }
        }
    }
}

/// Convert the host feature flags to the runtime feature flags
impl From<Features> for wasmcloud_runtime::experimental::Features {
    fn from(f: Features) -> wasmcloud_runtime::experimental::Features {
        wasmcloud_runtime::experimental::Features {
            wasmcloud_messaging_v3: f.wasmcloud_messaging_v3,
        }
    }
}