Skip to main content

flare_core/common/
features.rs

1//! Compile-time feature capability matrix.
2//!
3//! This module intentionally describes `flare-core` transport capabilities, not
4//! IM/Social product behavior. It gives applications and diagnostics a stable
5//! way to inspect what this crate was built to expose.
6
7/// Capabilities enabled for the current build.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub struct FeatureSet {
10    /// Native target build.
11    pub native: bool,
12    /// WASM target build.
13    pub wasm: bool,
14    /// Client-side transport stack is enabled.
15    pub client: bool,
16    /// Server-side transport stack is enabled.
17    pub server: bool,
18    /// WebSocket transport is enabled for this target.
19    pub websocket_transport: bool,
20    /// QUIC transport is enabled for this target.
21    pub quic_transport: bool,
22    /// Hybrid transport racing is available.
23    pub hybrid_transport: bool,
24    /// Built-in Gzip compression is enabled.
25    pub gzip_compression: bool,
26    /// Built-in AES-256-GCM encryption is enabled.
27    pub aes_256_gcm_encryption: bool,
28    /// Raw TCP transport is enabled (native only).
29    pub tcp_transport: bool,
30}
31
32impl FeatureSet {
33    /// Returns the capabilities for the current crate build.
34    pub const fn current() -> Self {
35        let native = cfg!(not(target_arch = "wasm32"));
36        let wasm = cfg!(target_arch = "wasm32");
37        let client = cfg!(feature = "client");
38        let server = cfg!(all(feature = "server", not(target_arch = "wasm32")));
39        let websocket_transport = cfg!(feature = "websocket");
40        let quic_transport = cfg!(all(feature = "quic", not(target_arch = "wasm32")));
41        let tcp_transport = cfg!(all(feature = "tcp", not(target_arch = "wasm32")));
42        let hybrid_transport = cfg!(all(
43            feature = "websocket",
44            feature = "quic",
45            not(target_arch = "wasm32")
46        ));
47
48        Self {
49            native,
50            wasm,
51            client,
52            server,
53            websocket_transport,
54            quic_transport,
55            hybrid_transport,
56            gzip_compression: cfg!(feature = "compression-gzip"),
57            aes_256_gcm_encryption: cfg!(feature = "encryption-aes-gcm"),
58            tcp_transport,
59        }
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::FeatureSet;
66
67    #[test]
68    fn current_feature_set_matches_compile_time_cfg() {
69        let features = FeatureSet::current();
70
71        assert_eq!(features.native, cfg!(not(target_arch = "wasm32")));
72        assert_eq!(features.wasm, cfg!(target_arch = "wasm32"));
73        assert_eq!(features.client, cfg!(feature = "client"));
74        assert_eq!(
75            features.server,
76            cfg!(all(feature = "server", not(target_arch = "wasm32")))
77        );
78        assert_eq!(features.websocket_transport, cfg!(feature = "websocket"));
79        assert_eq!(
80            features.quic_transport,
81            cfg!(all(feature = "quic", not(target_arch = "wasm32")))
82        );
83        assert_eq!(
84            features.hybrid_transport,
85            cfg!(all(
86                feature = "websocket",
87                feature = "quic",
88                not(target_arch = "wasm32")
89            ))
90        );
91        assert_eq!(
92            features.gzip_compression,
93            cfg!(feature = "compression-gzip")
94        );
95        assert_eq!(
96            features.aes_256_gcm_encryption,
97            cfg!(feature = "encryption-aes-gcm")
98        );
99        assert_eq!(
100            features.tcp_transport,
101            cfg!(all(feature = "tcp", not(target_arch = "wasm32")))
102        );
103    }
104}