Skip to content

Commit 9ab7483

Browse files
committed
document the public interface
1 parent 3f98812 commit 9ab7483

File tree

6 files changed

+134
-9
lines changed

6 files changed

+134
-9
lines changed

src/lib.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,42 @@
1+
//! tide tls listener built on async-tls and rustls
2+
//!
3+
//!
4+
//! # Example
5+
//! ```rust
6+
//! # use tide_rustls::TlsListener;
7+
//! fn main() -> tide::Result<()> { async_std::task::block_on(async {
8+
//! let mut app = tide::new();
9+
//! app.at("/").get(|_| async { Ok("Hello tls") });
10+
//! # if false {
11+
//! app.listen(
12+
//! TlsListener::build()
13+
//! .addrs("localhost:4433")
14+
//! .cert(std::env::var("TIDE_CERT_PATH").unwrap())
15+
//! .key(std::env::var("TIDE_KEY_PATH").unwrap()),
16+
//! )
17+
//! .await?;
18+
//! # }
19+
//! Ok(()) }) }
20+
//! ```
21+
#![forbid(unsafe_code, future_incompatible)]
22+
#![deny(
23+
missing_debug_implementations,
24+
nonstandard_style,
25+
missing_docs,
26+
unreachable_pub,
27+
missing_copy_implementations,
28+
unused_qualifications
29+
)]
30+
131
mod tcp_connection;
232
mod tls_listener;
333
mod tls_listener_builder;
434
mod tls_listener_config;
535
mod tls_stream_wrapper;
636

7-
pub use tcp_connection::TcpConnection;
37+
pub(crate) use tcp_connection::TcpConnection;
38+
pub(crate) use tls_listener_config::TlsListenerConfig;
39+
pub(crate) use tls_stream_wrapper::TlsStreamWrapper;
40+
841
pub use tls_listener::TlsListener;
942
pub use tls_listener_builder::TlsListenerBuilder;
10-
pub use tls_listener_config::TlsListenerConfig;
11-
pub use tls_stream_wrapper::TlsStreamWrapper;

src/tcp_connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use async_std::net::{SocketAddr, TcpListener};
22
use std::fmt::{self, Debug, Display, Formatter};
33

44
#[derive(Debug)]
5-
pub enum TcpConnection {
5+
pub(crate) enum TcpConnection {
66
Addrs(Vec<SocketAddr>),
77
Connected(TcpListener),
88
}

src/tls_listener.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::path::Path;
1818
use std::sync::Arc;
1919
use std::time::Duration;
2020

21+
/// The primary type for this crate
2122
#[derive(Debug)]
2223
pub struct TlsListener {
2324
connection: TcpConnection,
@@ -28,6 +29,20 @@ impl TlsListener {
2829
pub(crate) fn new(connection: TcpConnection, config: TlsListenerConfig) -> Self {
2930
Self { connection, config }
3031
}
32+
/// The primary entrypoint to create a TlsListener. See
33+
/// [TlsListenerBuilder](crate::TlsListenerBuilder) for more
34+
/// configuration options.
35+
///
36+
/// # Example
37+
///
38+
/// ```rust
39+
/// # use tide_rustls::TlsListener;
40+
/// let listener = TlsListener::build()
41+
/// .addrs("localhost:4433")
42+
/// .cert("./tls/localhost-4433.cert")
43+
/// .key("./tls/localhost-4433.key")
44+
/// .finish();
45+
/// ```
3146
pub fn build() -> TlsListenerBuilder {
3247
TlsListenerBuilder::new()
3348
}
@@ -120,7 +135,7 @@ impl<State: Clone + Send + Sync + 'static> ToListener<State> for TlsListener {
120135
impl<State: Clone + Send + Sync + 'static> ToListener<State> for TlsListenerBuilder {
121136
type Listener = TlsListener;
122137
fn to_listener(self) -> io::Result<Self::Listener> {
123-
self.build()
138+
self.finish()
124139
}
125140
}
126141

src/tls_listener_builder.rs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,31 @@ use super::{TcpConnection, TlsListener, TlsListenerConfig};
88
use std::net::{SocketAddr, ToSocketAddrs};
99
use std::path::{Path, PathBuf};
1010

11+
/// # A builder for TlsListeners
12+
///
13+
/// This is created with a call to
14+
/// [`TlsListener::build`](crate::TlsListener::build). This also can
15+
/// be passed directly to [`tide::Server::listen`], skipping the
16+
/// [`TlsListenerBuilder::finish`] call.
17+
///
18+
/// # Examples
19+
///
20+
/// ```rust
21+
/// # use tide_rustls::TlsListener;
22+
/// let listener = TlsListener::build()
23+
/// .addrs("localhost:4433")
24+
/// .cert("./tls/localhost-4433.cert")
25+
/// .key("./tls/localhost-4433.key")
26+
/// .finish();
27+
/// ```
28+
///
29+
/// ```rust
30+
/// # use tide_rustls::TlsListener;
31+
/// let listener = TlsListener::build()
32+
/// .tcp(std::net::TcpListener::bind("localhost:4433").unwrap())
33+
/// .config(rustls::ServerConfig::new(rustls::NoClientAuth::new()))
34+
/// .finish();
35+
/// ```
1136
#[derive(Default)]
1237
pub struct TlsListenerBuilder {
1338
key: Option<PathBuf>,
@@ -17,34 +42,88 @@ pub struct TlsListenerBuilder {
1742
addrs: Option<Vec<SocketAddr>>,
1843
}
1944

45+
impl std::fmt::Debug for TlsListenerBuilder {
46+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47+
f.debug_struct("TlsListenerBuilder")
48+
.field("key", &self.key)
49+
.field("cert", &self.cert)
50+
.field(
51+
"config",
52+
&if self.config.is_some() {
53+
"Some(ServerConfig { .. })"
54+
} else {
55+
"None"
56+
},
57+
)
58+
.field("tcp", &self.tcp)
59+
.field("addrs", &self.addrs)
60+
.finish()
61+
}
62+
}
63+
2064
impl TlsListenerBuilder {
2165
pub(crate) fn new() -> Self {
2266
TlsListenerBuilder::default()
2367
}
68+
69+
/// Provide a path to a key file, in either pkcs8 or rsa
70+
/// formats. This is mutually exclusive with providing a server
71+
/// config with [`TlsListenerBuilder::config`], but must be used
72+
/// in conjunction with [`TlsListenerBuilder::cert`]
2473
pub fn key(mut self, path: impl AsRef<Path>) -> Self {
2574
self.key = Some(path.as_ref().into());
2675
self
2776
}
77+
78+
/// Provide a path to a cert file. This is mutually exclusive with
79+
/// providing a server config with [`TlsListenerBuilder::config`],
80+
/// but must be used in conjunction with
81+
/// [`TlsListenerBuilder::key`]
2882
pub fn cert(mut self, path: impl AsRef<Path>) -> Self {
2983
self.cert = Some(path.as_ref().into());
3084
self
3185
}
86+
87+
/// Provide a prebuilt
88+
/// [`rustls::ServerConfig`](::rustls::ServerConfig) with any
89+
/// options. This is mutually exclusive with both
90+
/// [`TlsListenerBuilder::key`] and [`TlsListenerBuilder::cert`],
91+
/// but provides the opportunity for more configuration choices.
3292
pub fn config(mut self, config: ServerConfig) -> Self {
3393
self.config = Some(config);
3494
self
3595
}
96+
97+
/// Provides a bound tcp listener (either async-std or std) to
98+
/// build this tls listener on. This is mutually exclusive with
99+
/// [`TlsListenerBuilder::addrs`], but one of them is mandatory.
36100
pub fn tcp(mut self, tcp: impl Into<TcpListener>) -> Self {
37101
self.tcp = Some(tcp.into());
38102
self
39103
}
104+
105+
/// Provides a [`std::net::ToSocketAddrs`] specification for this
106+
/// tls listener. This is mutually exclusive with
107+
/// [`TlsListenerBuilder::tcp`] but one of them is mandatory.
40108
pub fn addrs(mut self, addrs: impl ToSocketAddrs) -> Self {
41109
if let Ok(socket_addrs) = addrs.to_socket_addrs() {
42110
self.addrs = Some(socket_addrs.collect());
43111
}
44112
self
45113
}
46114

47-
pub(crate) fn build(self) -> io::Result<TlsListener> {
115+
/// finishes building a TlsListener from this TlsListenerBuilder.
116+
///
117+
/// # Errors
118+
///
119+
/// this will return an error unless all of the following conditions are met:
120+
/// * either of these is provided, but not both
121+
/// * [`TlsListenerBuilder::tcp`]
122+
/// * [`TlsListenerBuilder::addrs`]
123+
/// * either of these is provided, but not both
124+
/// * both [`TlsListenerBuilder::cert`] AND [`TlsListenerBuilder::key`]
125+
/// * [`TlsListenerBuilder::config`]
126+
pub fn finish(self) -> io::Result<TlsListener> {
48127
let Self {
49128
key,
50129
cert,

src/tls_listener_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl Default for TlsListenerConfig {
1010
Self::Unconfigured
1111
}
1212
}
13-
pub enum TlsListenerConfig {
13+
pub(crate) enum TlsListenerConfig {
1414
Unconfigured,
1515
Acceptor(TlsAcceptor),
1616
ServerConfig(ServerConfig),

src/tls_stream_wrapper.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use std::pin::Pin;
66
use std::task::{Context, Poll};
77

88
#[derive(Clone)]
9-
pub struct TlsStreamWrapper(Arc<Mutex<TlsStream<TcpStream>>>);
9+
pub(crate) struct TlsStreamWrapper(Arc<Mutex<TlsStream<TcpStream>>>);
1010

1111
impl TlsStreamWrapper {
12-
pub fn new(stream: TlsStream<TcpStream>) -> Self {
12+
pub(crate) fn new(stream: TlsStream<TcpStream>) -> Self {
1313
Self(Arc::new(Mutex::new(stream)))
1414
}
1515
}

0 commit comments

Comments
 (0)