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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use core::fmt;

use serde::Deserialize;
use tokio_util::either::Either::{Left, Right};
use either::Either;

use crate::{Ignite, Rocket};
use crate::listener::{Bind, Endpoint, tcp::TcpListener};

#[cfg(unix)] use crate::listener::unix::UnixListener;
#[cfg(feature = "tls")] use crate::tls::{TlsListener, TlsConfig};

mod private {
    use super::*;
    use tokio_util::either::Either;

    #[cfg(feature = "tls")] type TlsListener<T> = super::TlsListener<T>;
    #[cfg(not(feature = "tls"))] type TlsListener<T> = T;
    #[cfg(unix)] type UnixListener = super::UnixListener;
    #[cfg(not(unix))] type UnixListener = TcpListener;

    pub type Listener = Either<
        Either<TlsListener<TcpListener>, TlsListener<UnixListener>>,
        Either<TcpListener, UnixListener>,
    >;

    /// The default connection listener.
    ///
    /// # Configuration
    ///
    /// Reads the following optional configuration parameters:
    ///
    /// | parameter   | type              | default               |
    /// | ----------- | ----------------- | --------------------- |
    /// | `address`   | [`Endpoint`]      | `tcp:127.0.0.1:8000`  |
    /// | `tls`       | [`TlsConfig`]     | None                  |
    /// | `reuse`     | boolean           | `true`                |
    ///
    /// # Listener
    ///
    /// Based on the above configuration, this listener defers to one of the
    /// following existing listeners:
    ///
    /// | listener                      | `address` type     | `tls` enabled |
    /// |-------------------------------|--------------------|---------------|
    /// | [`TcpListener`]               | [`Endpoint::Tcp`]  | no            |
    /// | [`UnixListener`]              | [`Endpoint::Unix`] | no            |
    /// | [`TlsListener<TcpListener>`]  | [`Endpoint::Tcp`]  | yes           |
    /// | [`TlsListener<UnixListener>`] | [`Endpoint::Unix`] | yes           |
    ///
    /// [`UnixListener`]: crate::listener::unix::UnixListener
    /// [`TlsListener<TcpListener>`]: crate::tls::TlsListener
    /// [`TlsListener<UnixListener>`]: crate::tls::TlsListener
    ///
    ///  * **address type** is the variant the `address` parameter parses as.
    ///  * **`tls` enabled** is `yes` when the `tls` feature is enabled _and_ a
    ///    `tls` configuration is provided.
    #[cfg(doc)]
    pub struct DefaultListener(());
}

#[derive(Deserialize)]
struct Config {
    #[serde(default)]
    address: Endpoint,
    #[cfg(feature = "tls")]
    tls: Option<TlsConfig>,
}

#[cfg(doc)]
pub use private::DefaultListener;

#[cfg(doc)]
type Connection = crate::listener::tcp::TcpStream;

#[cfg(doc)]
impl Bind for DefaultListener {
    type Error = Error;
    async fn bind(_: &Rocket<Ignite>) -> Result<Self, Error>  { unreachable!() }
    fn bind_endpoint(_: &Rocket<Ignite>) -> Result<Endpoint, Error> { unreachable!() }
}

#[cfg(doc)]
impl super::Listener for DefaultListener {
    #[doc(hidden)] type Accept = Connection;
    #[doc(hidden)] type Connection = Connection;
    #[doc(hidden)]
    async fn accept(&self) -> std::io::Result<Connection>  { unreachable!() }
    #[doc(hidden)]
    async fn connect(&self, _: Self::Accept) -> std::io::Result<Connection>  { unreachable!() }
    #[doc(hidden)]
    fn endpoint(&self) -> std::io::Result<Endpoint> { unreachable!() }
}

#[cfg(not(doc))]
pub type DefaultListener = private::Listener;

#[cfg(not(doc))]
impl Bind for DefaultListener {
    type Error = Error;

    async fn bind(rocket: &Rocket<Ignite>) -> Result<Self, Self::Error> {
        let config: Config = rocket.figment().extract()?;
        match config.address {
            #[cfg(feature = "tls")]
            Endpoint::Tcp(_) if config.tls.is_some() => {
                let listener = <TlsListener<TcpListener> as Bind>::bind(rocket).await?;
                Ok(Left(Left(listener)))
            }
            Endpoint::Tcp(_) => {
                let listener = <TcpListener as Bind>::bind(rocket).await?;
                Ok(Right(Left(listener)))
            }
            #[cfg(all(unix, feature = "tls"))]
            Endpoint::Unix(_) if config.tls.is_some() => {
                let listener = <TlsListener<UnixListener> as Bind>::bind(rocket).await?;
                Ok(Left(Right(listener)))
            }
            #[cfg(unix)]
            Endpoint::Unix(_) => {
                let listener = <UnixListener as Bind>::bind(rocket).await?;
                Ok(Right(Right(listener)))
            }
            endpoint => Err(Error::Unsupported(endpoint)),
        }
    }

    fn bind_endpoint(rocket: &Rocket<Ignite>) -> Result<Endpoint, Self::Error> {
        let config: Config = rocket.figment().extract()?;
        Ok(config.address)
    }
}

#[derive(Debug)]
pub enum Error {
    Config(figment::Error),
    Io(std::io::Error),
    Unsupported(Endpoint),
    #[cfg(feature = "tls")]
    Tls(crate::tls::Error),
}

impl From<figment::Error> for Error {
    fn from(value: figment::Error) -> Self {
        Error::Config(value)
    }
}

impl From<std::io::Error> for Error {
    fn from(value: std::io::Error) -> Self {
        Error::Io(value)
    }
}

#[cfg(feature = "tls")]
impl From<crate::tls::Error> for Error {
    fn from(value: crate::tls::Error) -> Self {
        Error::Tls(value)
    }
}

impl From<Either<figment::Error, std::io::Error>> for Error {
    fn from(value: Either<figment::Error, std::io::Error>) -> Self {
        value.either(Error::Config, Error::Io)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Config(e) => e.fmt(f),
            Error::Io(e) => e.fmt(f),
            Error::Unsupported(e) => write!(f, "unsupported endpoint: {e:?}"),
            #[cfg(feature = "tls")]
            Error::Tls(error) => error.fmt(f),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Config(e) => Some(e),
            Error::Io(e) => Some(e),
            Error::Unsupported(_) => None,
            #[cfg(feature = "tls")]
            Error::Tls(e) => Some(e),
        }
    }
}