@@ -13,7 +13,6 @@ use crate::{
13
13
bson:: oid:: ObjectId ,
14
14
client:: ClusterTime ,
15
15
cmap:: Command ,
16
- error:: { ErrorKind , Result } ,
17
16
options:: { ClientOptions , StreamAddress } ,
18
17
sdam:: description:: server:: { ServerDescription , ServerType } ,
19
18
selection_criteria:: { ReadPreference , SelectionCriteria } ,
@@ -111,7 +110,7 @@ impl TopologyDescription {
111
110
}
112
111
}
113
112
114
- pub ( crate ) fn new ( options : ClientOptions ) -> Result < Self > {
113
+ pub ( crate ) fn new ( options : ClientOptions ) -> crate :: error :: Result < Self > {
115
114
verify_max_staleness (
116
115
options
117
116
. selection_criteria
@@ -395,7 +394,10 @@ impl TopologyDescription {
395
394
396
395
/// Update the topology based on the new information about the topology contained by the
397
396
/// ServerDescription.
398
- pub ( crate ) fn update ( & mut self , mut server_description : ServerDescription ) -> Result < ( ) > {
397
+ pub ( crate ) fn update (
398
+ & mut self ,
399
+ mut server_description : ServerDescription ,
400
+ ) -> Result < ( ) , String > {
399
401
// Ignore updates from servers not currently in the cluster.
400
402
if !self . servers . contains_key ( & server_description. address ) {
401
403
return Ok ( ( ) ) ;
@@ -439,7 +441,10 @@ impl TopologyDescription {
439
441
}
440
442
441
443
/// Update the Unknown topology description based on the server description.
442
- fn update_unknown_topology ( & mut self , server_description : ServerDescription ) -> Result < ( ) > {
444
+ fn update_unknown_topology (
445
+ & mut self ,
446
+ server_description : ServerDescription ,
447
+ ) -> Result < ( ) , String > {
443
448
match server_description. server_type {
444
449
ServerType :: Unknown | ServerType :: RSGhost => { }
445
450
ServerType :: Standalone => {
@@ -473,7 +478,7 @@ impl TopologyDescription {
473
478
fn update_replica_set_no_primary_topology (
474
479
& mut self ,
475
480
server_description : ServerDescription ,
476
- ) -> Result < ( ) > {
481
+ ) -> Result < ( ) , String > {
477
482
match server_description. server_type {
478
483
ServerType :: Unknown | ServerType :: RSGhost => { }
479
484
ServerType :: Standalone | ServerType :: Mongos => {
@@ -495,7 +500,7 @@ impl TopologyDescription {
495
500
fn update_replica_set_with_primary_topology (
496
501
& mut self ,
497
502
server_description : ServerDescription ,
498
- ) -> Result < ( ) > {
503
+ ) -> Result < ( ) , String > {
499
504
match server_description. server_type {
500
505
ServerType :: Unknown | ServerType :: RSGhost => {
501
506
self . record_primary_state ( ) ;
@@ -527,7 +532,7 @@ impl TopologyDescription {
527
532
fn update_rs_without_primary_server (
528
533
& mut self ,
529
534
server_description : ServerDescription ,
530
- ) -> Result < ( ) > {
535
+ ) -> Result < ( ) , String > {
531
536
if self . set_name . is_none ( ) {
532
537
self . set_name = server_description. set_name ( ) ?;
533
538
} else if self . set_name != server_description. set_name ( ) ? {
@@ -550,7 +555,7 @@ impl TopologyDescription {
550
555
fn update_rs_with_primary_from_member (
551
556
& mut self ,
552
557
server_description : ServerDescription ,
553
- ) -> Result < ( ) > {
558
+ ) -> Result < ( ) , String > {
554
559
if self . set_name != server_description. set_name ( ) ? {
555
560
self . servers . remove ( & server_description. address ) ;
556
561
self . record_primary_state ( ) ;
@@ -572,7 +577,7 @@ impl TopologyDescription {
572
577
fn update_rs_from_primary_server (
573
578
& mut self ,
574
579
server_description : ServerDescription ,
575
- ) -> Result < ( ) > {
580
+ ) -> Result < ( ) , String > {
576
581
if self . set_name . is_none ( ) {
577
582
self . set_name = server_description. set_name ( ) ?;
578
583
} else if self . set_name != server_description. set_name ( ) ? {
@@ -661,8 +666,13 @@ impl TopologyDescription {
661
666
}
662
667
663
668
/// Create a new ServerDescription for each address and add it to the topology.
664
- fn add_new_servers < ' a > ( & mut self , servers : impl Iterator < Item = & ' a String > ) -> Result < ( ) > {
665
- let servers: Result < Vec < _ > > = servers. map ( |server| StreamAddress :: parse ( server) ) . collect ( ) ;
669
+ fn add_new_servers < ' a > (
670
+ & mut self ,
671
+ servers : impl Iterator < Item = & ' a String > ,
672
+ ) -> Result < ( ) , String > {
673
+ let servers: Result < Vec < _ > , String > = servers
674
+ . map ( |server| StreamAddress :: parse ( server) . map_err ( |e| e. to_string ( ) ) )
675
+ . collect ( ) ;
666
676
667
677
self . add_new_servers_from_addresses ( servers?. iter ( ) ) ;
668
678
Ok ( ( ) )
@@ -731,15 +741,17 @@ pub(crate) struct TopologyDescriptionDiff {
731
741
pub ( crate ) new_addresses : HashSet < StreamAddress > ,
732
742
}
733
743
734
- fn verify_max_staleness ( max_staleness : Option < Duration > ) -> Result < ( ) > {
744
+ fn verify_max_staleness ( max_staleness : Option < Duration > ) -> crate :: error:: Result < ( ) > {
745
+ verify_max_staleness_inner ( max_staleness)
746
+ . map_err ( |s| crate :: error:: ErrorKind :: ArgumentError { message : s } . into ( ) )
747
+ }
748
+
749
+ fn verify_max_staleness_inner ( max_staleness : Option < Duration > ) -> Result < ( ) , String > {
735
750
if max_staleness
736
751
. map ( |staleness| staleness > Duration :: from_secs ( 0 ) && staleness < Duration :: from_secs ( 90 ) )
737
752
. unwrap_or ( false )
738
753
{
739
- return Err ( ErrorKind :: ArgumentError {
740
- message : "max staleness cannot be both positive and below 90 seconds" . into ( ) ,
741
- }
742
- . into ( ) ) ;
754
+ return Err ( "max staleness cannot be both positive and below 90 seconds" . into ( ) ) ;
743
755
}
744
756
745
757
Ok ( ( ) )
0 commit comments