Skip to main content

fips_core/config/transport/
aggregate.rs

1use super::*;
2
3// ============================================================================
4// TransportsConfig
5// ============================================================================
6
7/// Transports configuration section.
8///
9/// Each transport type can have either a single instance (config directly
10/// under the type name) or multiple named instances.
11#[derive(Debug, Clone, Default, Serialize, Deserialize)]
12pub struct TransportsConfig {
13    /// UDP transport instances.
14    #[serde(default, skip_serializing_if = "is_transport_empty")]
15    pub udp: TransportInstances<UdpConfig>,
16
17    /// In-memory simulated transport instances.
18    #[cfg(feature = "sim-transport")]
19    #[serde(default, skip_serializing_if = "is_transport_empty")]
20    pub sim: TransportInstances<SimTransportConfig>,
21
22    /// Ethernet transport instances.
23    #[serde(default, skip_serializing_if = "is_transport_empty")]
24    pub ethernet: TransportInstances<EthernetConfig>,
25
26    /// TCP transport instances.
27    #[serde(default, skip_serializing_if = "is_transport_empty")]
28    pub tcp: TransportInstances<TcpConfig>,
29
30    /// Tor transport instances.
31    #[serde(default, skip_serializing_if = "is_transport_empty")]
32    pub tor: TransportInstances<TorConfig>,
33
34    /// WebRTC transport instances.
35    #[serde(default, skip_serializing_if = "is_transport_empty")]
36    pub webrtc: TransportInstances<WebRtcConfig>,
37
38    /// BLE transport instances.
39    #[serde(default, skip_serializing_if = "is_transport_empty")]
40    pub ble: TransportInstances<BleConfig>,
41}
42
43/// Helper for skip_serializing_if on TransportInstances.
44fn is_transport_empty<T>(instances: &TransportInstances<T>) -> bool {
45    instances.is_empty()
46}
47
48impl TransportsConfig {
49    /// Check if any transports are configured.
50    pub fn is_empty(&self) -> bool {
51        self.udp.is_empty()
52            && {
53                #[cfg(feature = "sim-transport")]
54                {
55                    self.sim.is_empty()
56                }
57                #[cfg(not(feature = "sim-transport"))]
58                {
59                    true
60                }
61            }
62            && self.ethernet.is_empty()
63            && self.tcp.is_empty()
64            && self.tor.is_empty()
65            && self.webrtc.is_empty()
66            && self.ble.is_empty()
67    }
68
69    /// Merge another TransportsConfig into this one.
70    ///
71    /// Non-empty transport sections from `other` replace those in `self`.
72    pub fn merge(&mut self, other: TransportsConfig) {
73        if !other.udp.is_empty() {
74            self.udp = other.udp;
75        }
76        #[cfg(feature = "sim-transport")]
77        if !other.sim.is_empty() {
78            self.sim = other.sim;
79        }
80        if !other.ethernet.is_empty() {
81            self.ethernet = other.ethernet;
82        }
83        if !other.tcp.is_empty() {
84            self.tcp = other.tcp;
85        }
86        if !other.tor.is_empty() {
87            self.tor = other.tor;
88        }
89        if !other.webrtc.is_empty() {
90            self.webrtc = other.webrtc;
91        }
92        if !other.ble.is_empty() {
93            self.ble = other.ble;
94        }
95    }
96}