@@ -149,6 +149,106 @@ cfg_sync!(
149149 pub use sync:: Connection ;
150150) ;
151151
152+ #[ derive( Debug ) ]
153+ /// A connection builder for easily building connections
154+ ///
155+ /// ## Example (sync)
156+ /// ```no_run
157+ /// let con =
158+ /// ConnectionBuilder::new()
159+ /// .set_host("127.0.0.1")
160+ /// .set_port(2003)
161+ /// .get_connection()
162+ /// .unwrap();
163+ /// ```
164+ ///
165+ /// ## Example (async)
166+ /// ```no_run
167+ /// let con =
168+ /// ConnectionBuilder::new()
169+ /// .set_host("127.0.0.1")
170+ /// .set_port(2003)
171+ /// .get_async_connection()
172+ /// .unwrap();
173+ /// ```
174+ pub struct ConnectionBuilder < ' a > {
175+ port : Option < u16 > ,
176+ host : Option < & ' a str > ,
177+ }
178+
179+ impl < ' a > Default for ConnectionBuilder < ' a > {
180+ fn default ( ) -> Self {
181+ Self :: new ( )
182+ }
183+ }
184+
185+ pub type ConnectionBuilderResult < T > = Result < T , error:: Error > ;
186+
187+ impl < ' a > ConnectionBuilder < ' a > {
188+ /// Create an empty connection builder
189+ pub fn new ( ) -> Self {
190+ Self {
191+ port : None ,
192+ host : None ,
193+ }
194+ }
195+ /// Set the port
196+ pub fn set_port ( mut self , port : u16 ) -> Self {
197+ self . port = Some ( port) ;
198+ self
199+ }
200+ /// Set the host
201+ pub fn set_host ( mut self , host : & ' a str ) -> Self {
202+ self . host = Some ( host) ;
203+ self
204+ }
205+ cfg_sync ! {
206+ /// Get a [sync connection](sync::Connection) to the database
207+ pub fn get_connection( & self ) -> ConnectionBuilderResult <sync:: Connection > {
208+ let con =
209+ sync:: Connection :: new( self . host. unwrap_or( "127.0.0.1" ) , self . port. unwrap_or( 2003 ) ) ?;
210+ Ok ( con)
211+ }
212+ cfg_sync_ssl_any! {
213+ /// Get a [sync TLS connection](sync::TlsConnection) to the database
214+ pub fn get_tls_connection(
215+ & self ,
216+ sslcert: String ,
217+ ) -> ConnectionBuilderResult <sync:: TlsConnection > {
218+ let con = sync:: TlsConnection :: new(
219+ self . host. unwrap_or( "127.0.0.1" ) ,
220+ self . port. unwrap_or( 2003 ) ,
221+ & sslcert,
222+ ) ?;
223+ Ok ( con)
224+ }
225+ }
226+ }
227+ cfg_async ! {
228+ /// Get an [async connection](aio::Connection) to the database
229+ pub async fn get_async_connection( & self ) -> ConnectionBuilderResult <aio:: Connection > {
230+ let con = aio:: Connection :: new( self . host. unwrap_or( "127.0.0.1" ) , self . port. unwrap_or( 2003 ) )
231+ . await ?;
232+ Ok ( con)
233+ }
234+ cfg_async_ssl_any! {
235+ /// Get an [async TLS connection](aio::TlsConnection) to the database
236+ pub async fn get_async_tls_connection(
237+ & self ,
238+ sslcert: String ,
239+ ) -> ConnectionBuilderResult <aio:: TlsConnection > {
240+ let con = aio:: TlsConnection :: new(
241+ self . host. unwrap_or( "127.0.0.1" ) ,
242+ self . port. unwrap_or( 2003 ) ,
243+ & sslcert,
244+ )
245+ . await ?;
246+ Ok ( con)
247+ }
248+ }
249+ }
250+ }
251+
152252#[ macro_export]
153253/// A macro that can be used to easily create queries with _almost_ variadic properties.
154254/// Where you'd normally create queries like this:
@@ -438,4 +538,69 @@ pub mod error {
438538 }
439539 }
440540 ) ;
541+ #[ derive( Debug ) ]
542+ /// An error originating from the Skyhash protocol
543+ pub enum SkyhashError {
544+ /// The server sent an invalid response
545+ InvalidResponse ,
546+ /// The server sent a response but it could not be parsed
547+ ParseError ,
548+ /// The server sent a data type not supported by this client version
549+ UnsupportedDataType ,
550+ }
551+
552+ #[ derive( Debug ) ]
553+ /// A standard error type for the client driver
554+ pub enum Error {
555+ /// An I/O error occurred
556+ IoError ( std:: io:: Error ) ,
557+ #[ cfg( all( feature = "sync" , any( feature = "ssl" , feature = "sslv" ) ) ) ]
558+ #[ cfg_attr(
559+ docsrs,
560+ doc( cfg( all( feature = "sync" , any( feature = "ssl" , feature = "sslv" ) ) ) )
561+ ) ]
562+ /// An SSL error occurred
563+ SslError ( openssl:: ssl:: Error ) ,
564+ /// A Skyhash error occurred
565+ SkyError ( SkyhashError ) ,
566+ /// An application level parse error occurred
567+ ParseError ,
568+ }
569+
570+ #[ cfg( all( feature = "sync" , any( feature = "ssl" , feature = "sslv" ) ) ) ]
571+ #[ cfg_attr(
572+ docsrs,
573+ doc( cfg( all( feature = "sync" , any( feature = "ssl" , feature = "sslv" ) ) ) )
574+ ) ]
575+ impl From < openssl:: ssl:: Error > for Error {
576+ fn from ( err : openssl:: ssl:: Error ) -> Self {
577+ Self :: SslError ( err)
578+ }
579+ }
580+
581+ impl From < std:: io:: Error > for Error {
582+ fn from ( err : std:: io:: Error ) -> Self {
583+ Self :: IoError ( err)
584+ }
585+ }
586+
587+ #[ cfg( all( feature = "sync" , any( feature = "ssl" , feature = "sslv" ) ) ) ]
588+ #[ cfg_attr(
589+ docsrs,
590+ doc( cfg( all( feature = "sync" , any( feature = "ssl" , feature = "sslv" ) ) ) )
591+ ) ]
592+ impl From < SslError > for Error {
593+ fn from ( err : SslError ) -> Self {
594+ match err {
595+ SslError :: IoError ( ioerr) => Self :: IoError ( ioerr) ,
596+ SslError :: SslError ( sslerr) => Self :: SslError ( sslerr) ,
597+ }
598+ }
599+ }
600+
601+ impl From < SkyhashError > for Error {
602+ fn from ( err : SkyhashError ) -> Self {
603+ Self :: SkyError ( err)
604+ }
605+ }
441606}
0 commit comments