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    /// Ephemeral Nostr relay fallback transport instances.
18    #[serde(default, skip_serializing_if = "is_transport_empty")]
19    pub nostr_relay: TransportInstances<NostrRelayConfig>,
20
21    /// In-memory simulated transport instances.
22    #[cfg(feature = "sim-transport")]
23    #[serde(default, skip_serializing_if = "is_transport_empty")]
24    pub sim: TransportInstances<SimTransportConfig>,
25
26    /// Ethernet transport instances.
27    #[serde(default, skip_serializing_if = "is_transport_empty")]
28    pub ethernet: TransportInstances<EthernetConfig>,
29
30    /// TCP transport instances.
31    #[serde(default, skip_serializing_if = "is_transport_empty")]
32    pub tcp: TransportInstances<TcpConfig>,
33
34    /// Tor transport instances.
35    #[serde(default, skip_serializing_if = "is_transport_empty")]
36    pub tor: TransportInstances<TorConfig>,
37
38    /// WebRTC transport instances.
39    #[serde(default, skip_serializing_if = "is_transport_empty")]
40    pub webrtc: TransportInstances<WebRtcConfig>,
41
42    /// BLE transport instances.
43    #[serde(default, skip_serializing_if = "is_transport_empty")]
44    pub ble: TransportInstances<BleConfig>,
45}
46
47/// Helper for skip_serializing_if on TransportInstances.
48fn is_transport_empty<T>(instances: &TransportInstances<T>) -> bool {
49    instances.is_empty()
50}
51
52impl TransportsConfig {
53    /// Check if any transports are configured.
54    pub fn is_empty(&self) -> bool {
55        self.udp.is_empty()
56            && self.nostr_relay.is_empty()
57            && {
58                #[cfg(feature = "sim-transport")]
59                {
60                    self.sim.is_empty()
61                }
62                #[cfg(not(feature = "sim-transport"))]
63                {
64                    true
65                }
66            }
67            && self.ethernet.is_empty()
68            && self.tcp.is_empty()
69            && self.tor.is_empty()
70            && self.webrtc.is_empty()
71            && self.ble.is_empty()
72    }
73
74    /// Merge another TransportsConfig into this one.
75    ///
76    /// Non-empty transport sections from `other` replace those in `self`.
77    pub fn merge(&mut self, other: TransportsConfig) {
78        if !other.udp.is_empty() {
79            self.udp = other.udp;
80        }
81        if !other.nostr_relay.is_empty() {
82            self.nostr_relay = other.nostr_relay;
83        }
84        #[cfg(feature = "sim-transport")]
85        if !other.sim.is_empty() {
86            self.sim = other.sim;
87        }
88        if !other.ethernet.is_empty() {
89            self.ethernet = other.ethernet;
90        }
91        if !other.tcp.is_empty() {
92            self.tcp = other.tcp;
93        }
94        if !other.tor.is_empty() {
95            self.tor = other.tor;
96        }
97        if !other.webrtc.is_empty() {
98            self.webrtc = other.webrtc;
99        }
100        if !other.ble.is_empty() {
101            self.ble = other.ble;
102        }
103    }
104}