mwc_libp2p_core/transport/
optional.rs

1// Copyright 2019 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21use crate::transport::{Transport, TransportError};
22use multiaddr::Multiaddr;
23
24/// Transport that is possibly disabled.
25///
26/// An `OptionalTransport<T>` is a wrapper around an `Option<T>`. If it is disabled (read: contains
27/// `None`), then any attempt to dial or listen will return `MultiaddrNotSupported`. If it is
28/// enabled (read: contains `Some`), then dialing and listening will be handled by the inner
29/// transport.
30#[derive(Debug, Copy, Clone)]
31pub struct OptionalTransport<T>(Option<T>);
32
33impl<T> OptionalTransport<T> {
34    /// Builds an `OptionalTransport` with the given transport in an enabled
35    /// state.
36    pub fn some(inner: T) -> OptionalTransport<T> {
37        OptionalTransport(Some(inner))
38    }
39
40    /// Builds a disabled `OptionalTransport`.
41    pub fn none() -> OptionalTransport<T> {
42        OptionalTransport(None)
43    }
44}
45
46impl<T> From<T> for OptionalTransport<T> {
47    fn from(inner: T) -> Self {
48        OptionalTransport(Some(inner))
49    }
50}
51
52impl<T> Transport for OptionalTransport<T>
53where
54    T: Transport,
55{
56    type Output = T::Output;
57    type Error = T::Error;
58    type Listener = T::Listener;
59    type ListenerUpgrade = T::ListenerUpgrade;
60    type Dial = T::Dial;
61
62    fn listen_on(self, addr: Multiaddr) -> Result<Self::Listener, TransportError<Self::Error>> {
63        if let Some(inner) = self.0 {
64            inner.listen_on(addr)
65        } else {
66            Err(TransportError::MultiaddrNotSupported(addr))
67        }
68    }
69
70    fn dial(self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
71        if let Some(inner) = self.0 {
72            inner.dial(addr)
73        } else {
74            Err(TransportError::MultiaddrNotSupported(addr))
75        }
76    }
77
78    fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
79        if let Some(inner) = &self.0 {
80            inner.address_translation(server, observed)
81        } else {
82            None
83        }
84    }
85}