@@ -53,8 +53,8 @@ pub struct Connection {
53
53
54
54
impl Connection {
55
55
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 ) ;
58
58
connection. hello ( hello) . await ?;
59
59
Ok ( connection)
60
60
}
@@ -63,14 +63,14 @@ impl Connection {
63
63
self . version
64
64
}
65
65
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 ?,
71
71
} ;
72
72
73
- Ok ( match & info . encryption {
73
+ Ok ( match & opts . encryption {
74
74
Some ( ( connector, domain) ) => {
75
75
let mut stream = connector. connect ( domain. clone ( ) , stream) . await ?;
76
76
let version = Self :: init ( & mut stream) . await ?;
@@ -269,7 +269,7 @@ impl Connection {
269
269
#[ derive( Debug , Clone , PartialEq , Eq ) ]
270
270
pub ( crate ) enum Routing {
271
271
No ,
272
- Yes ( Vec < ( BoltString , BoltString ) > ) ,
272
+ Yes ( Arc < [ ( BoltString , BoltString ) ] > ) ,
273
273
}
274
274
275
275
impl From < Routing > for Option < BoltMap > {
@@ -278,8 +278,8 @@ impl From<Routing> for Option<BoltMap> {
278
278
Routing :: No => None ,
279
279
Routing :: Yes ( routing) => Some (
280
280
routing
281
- . into_iter ( )
282
- . map ( |( k, v) | ( k, BoltType :: String ( v) ) )
281
+ . iter ( )
282
+ . map ( |( k, v) | ( k. clone ( ) , BoltType :: String ( v. clone ( ) ) ) )
283
283
. collect ( ) ,
284
284
) ,
285
285
}
@@ -302,24 +302,78 @@ impl Display for Routing {
302
302
}
303
303
}
304
304
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 ) ]
305
363
pub ( 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 ,
312
366
}
313
367
314
368
impl Debug for ConnectionInfo {
315
369
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
316
370
f. debug_struct ( "ConnectionInfo" )
317
- . field ( "user" , & self . user )
371
+ . field ( "user" , & self . init . user )
318
372
. 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 ( ) )
323
377
. finish_non_exhaustive ( )
324
378
}
325
379
}
@@ -360,7 +414,8 @@ impl ConnectionInfo {
360
414
"Client-side routing is in experimental mode." ,
361
415
"It is possible that operations against a cluster (such as Aura) will fail."
362
416
) ) ;
363
- Routing :: Yes ( url. routing_context ( ) )
417
+ let context = url. routing_context ( ) ;
418
+ Routing :: Yes ( context. into ( ) )
364
419
} else {
365
420
Routing :: No
366
421
} ;
@@ -373,14 +428,19 @@ impl ConnectionInfo {
373
428
Host :: Ipv6 ( d) => Host :: Ipv6 ( d) ,
374
429
} ;
375
430
376
- Ok ( Self {
377
- user : user. into ( ) ,
378
- password : password. into ( ) ,
431
+ let prepare = PrepareOpts {
379
432
host,
380
433
port : url. port ( ) ,
381
434
encryption,
435
+ } ;
436
+
437
+ let init = InitOpts {
438
+ user : user. into ( ) ,
439
+ password : password. into ( ) ,
382
440
routing,
383
- } )
441
+ } ;
442
+
443
+ Ok ( Self { prepare, init } )
384
444
}
385
445
386
446
fn tls_connector (
@@ -432,27 +492,6 @@ impl ConnectionInfo {
432
492
433
493
Ok ( ( connector, domain) )
434
494
}
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
- }
456
495
}
457
496
458
497
#[ derive( Clone , Debug ) ]
0 commit comments