@@ -53,8 +53,8 @@ pub struct Connection {
5353
5454impl Connection {
5555 pub ( crate ) async fn new ( info : & ConnectionInfo ) -> Result < Self > {
56- let mut connection = Self :: prepare ( info) . await ?;
57- let hello = info. to_hello ( connection. version ) ;
56+ let mut connection = Self :: prepare ( & info. prepare ) . await ?;
57+ let hello = info. init . to_hello ( connection. version ) ;
5858 connection. hello ( hello) . await ?;
5959 Ok ( connection)
6060 }
@@ -63,14 +63,14 @@ impl Connection {
6363 self . version
6464 }
6565
66- pub ( crate ) async fn prepare ( info : & ConnectionInfo ) -> Result < Self > {
67- let mut stream = match & info . host {
68- Host :: Domain ( domain) => TcpStream :: connect ( ( & * * domain, info . port ) ) . await ?,
69- Host :: Ipv4 ( ip) => TcpStream :: connect ( ( * ip, info . port ) ) . await ?,
70- Host :: Ipv6 ( ip) => TcpStream :: connect ( ( * ip, info . port ) ) . await ?,
66+ pub ( crate ) async fn prepare ( opts : & PrepareOpts ) -> Result < Self > {
67+ let mut stream = match & opts . host {
68+ Host :: Domain ( domain) => TcpStream :: connect ( ( & * * domain, opts . port ) ) . await ?,
69+ Host :: Ipv4 ( ip) => TcpStream :: connect ( ( * ip, opts . port ) ) . await ?,
70+ Host :: Ipv6 ( ip) => TcpStream :: connect ( ( * ip, opts . port ) ) . await ?,
7171 } ;
7272
73- Ok ( match & info . encryption {
73+ Ok ( match & opts . encryption {
7474 Some ( ( connector, domain) ) => {
7575 let mut stream = connector. connect ( domain. clone ( ) , stream) . await ?;
7676 let version = Self :: init ( & mut stream) . await ?;
@@ -269,7 +269,7 @@ impl Connection {
269269#[ derive( Debug , Clone , PartialEq , Eq ) ]
270270pub ( crate ) enum Routing {
271271 No ,
272- Yes ( Vec < ( BoltString , BoltString ) > ) ,
272+ Yes ( Arc < [ ( BoltString , BoltString ) ] > ) ,
273273}
274274
275275impl From < Routing > for Option < BoltMap > {
@@ -278,8 +278,8 @@ impl From<Routing> for Option<BoltMap> {
278278 Routing :: No => None ,
279279 Routing :: Yes ( routing) => Some (
280280 routing
281- . into_iter ( )
282- . map ( |( k, v) | ( k, BoltType :: String ( v) ) )
281+ . iter ( )
282+ . map ( |( k, v) | ( k. clone ( ) , BoltType :: String ( v. clone ( ) ) ) )
283283 . collect ( ) ,
284284 ) ,
285285 }
@@ -302,24 +302,78 @@ impl Display for Routing {
302302 }
303303}
304304
305+ #[ derive( Clone ) ]
306+ pub ( crate ) struct PrepareOpts {
307+ pub ( crate ) host : Host < Arc < str > > ,
308+ pub ( crate ) port : u16 ,
309+ pub ( crate ) encryption : Option < ( TlsConnector , ServerName < ' static > ) > ,
310+ }
311+
312+ impl Debug for PrepareOpts {
313+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
314+ f. debug_struct ( "PrepareOpts" )
315+ . field ( "host" , & self . host )
316+ . field ( "port" , & self . port )
317+ . field ( "encryption" , & self . encryption . is_some ( ) )
318+ . finish ( )
319+ }
320+ }
321+
322+ #[ derive( Clone ) ]
323+ pub ( crate ) struct InitOpts {
324+ pub ( crate ) user : Arc < str > ,
325+ pub ( crate ) password : Arc < str > ,
326+ pub ( crate ) routing : Routing ,
327+ }
328+
329+ impl Debug for InitOpts {
330+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
331+ f. debug_struct ( "InitOpts" )
332+ . field ( "user" , & self . user )
333+ . field ( "password" , & "***" )
334+ . field ( "routing" , & self . routing )
335+ . finish ( )
336+ }
337+ }
338+
339+ impl InitOpts {
340+ #[ cfg( not( feature = "unstable-bolt-protocol-impl-v2" ) ) ]
341+ pub ( crate ) fn to_hello ( & self , version : Version ) -> BoltRequest {
342+ HelloBuilder :: new ( & * self . user , & * self . password )
343+ . with_routing ( self . routing . clone ( ) )
344+ . build ( version)
345+ }
346+
347+ #[ cfg( feature = "unstable-bolt-protocol-impl-v2" ) ]
348+ pub ( crate ) fn to_hello ( & self , version : Version ) -> Hello {
349+ match self . routing {
350+ Routing :: No => HelloBuilder :: new ( & self . user , & self . password ) . build ( version) ,
351+ Routing :: Yes ( ref routing) => HelloBuilder :: new ( & self . user , & self . password )
352+ . with_routing (
353+ routing
354+ . iter ( )
355+ . map ( |( k, v) | ( k. value . as_str ( ) , v. value . as_str ( ) ) ) ,
356+ )
357+ . build ( version) ,
358+ }
359+ }
360+ }
361+
362+ #[ derive( Clone ) ]
305363pub ( crate ) struct ConnectionInfo {
306- pub user : Arc < str > ,
307- pub password : Arc < str > ,
308- pub host : Host < Arc < str > > ,
309- pub port : u16 ,
310- pub routing : Routing ,
311- pub encryption : Option < ( TlsConnector , ServerName < ' static > ) > ,
364+ pub ( crate ) prepare : PrepareOpts ,
365+ pub ( crate ) init : InitOpts ,
312366}
313367
314368impl Debug for ConnectionInfo {
315369 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
316370 f. debug_struct ( "ConnectionInfo" )
317- . field ( "user" , & self . user )
371+ . field ( "user" , & self . init . user )
318372 . field ( "password" , & "***" )
319- . field ( "host" , & self . host )
320- . field ( "port" , & self . port )
321- . field ( "routing" , & self . routing )
322- . field ( "encryption" , & self . encryption . is_some ( ) )
373+ . field ( "host" , & self . prepare . host )
374+ . field ( "port" , & self . prepare . port )
375+ . field ( "routing" , & self . init . routing )
376+ . field ( "encryption" , & self . prepare . encryption . is_some ( ) )
323377 . finish_non_exhaustive ( )
324378 }
325379}
@@ -360,7 +414,8 @@ impl ConnectionInfo {
360414 "Client-side routing is in experimental mode." ,
361415 "It is possible that operations against a cluster (such as Aura) will fail."
362416 ) ) ;
363- Routing :: Yes ( url. routing_context ( ) )
417+ let context = url. routing_context ( ) ;
418+ Routing :: Yes ( context. into ( ) )
364419 } else {
365420 Routing :: No
366421 } ;
@@ -373,14 +428,19 @@ impl ConnectionInfo {
373428 Host :: Ipv6 ( d) => Host :: Ipv6 ( d) ,
374429 } ;
375430
376- Ok ( Self {
377- user : user. into ( ) ,
378- password : password. into ( ) ,
431+ let prepare = PrepareOpts {
379432 host,
380433 port : url. port ( ) ,
381434 encryption,
435+ } ;
436+
437+ let init = InitOpts {
438+ user : user. into ( ) ,
439+ password : password. into ( ) ,
382440 routing,
383- } )
441+ } ;
442+
443+ Ok ( Self { prepare, init } )
384444 }
385445
386446 fn tls_connector (
@@ -432,27 +492,6 @@ impl ConnectionInfo {
432492
433493 Ok ( ( connector, domain) )
434494 }
435-
436- #[ cfg( not( feature = "unstable-bolt-protocol-impl-v2" ) ) ]
437- pub ( crate ) fn to_hello ( & self , version : Version ) -> BoltRequest {
438- HelloBuilder :: new ( & * self . user , & * self . password )
439- . with_routing ( self . routing . clone ( ) )
440- . build ( version)
441- }
442-
443- #[ cfg( feature = "unstable-bolt-protocol-impl-v2" ) ]
444- pub ( crate ) fn to_hello ( & self , version : Version ) -> Hello {
445- match self . routing {
446- Routing :: No => HelloBuilder :: new ( & self . user , & self . password ) . build ( version) ,
447- Routing :: Yes ( ref routing) => HelloBuilder :: new ( & self . user , & self . password )
448- . with_routing (
449- routing
450- . iter ( )
451- . map ( |( k, v) | ( k. value . as_str ( ) , v. value . as_str ( ) ) ) ,
452- )
453- . build ( version) ,
454- }
455- }
456495}
457496
458497#[ derive( Clone , Debug ) ]
0 commit comments