@@ -8,6 +8,31 @@ use super::{TcpConnection, TlsListener, TlsListenerConfig};
88use std:: net:: { SocketAddr , ToSocketAddrs } ;
99use 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 ) ]
1237pub 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+
2064impl 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,
0 commit comments