@@ -23,7 +23,9 @@ static CONFIG: Lazy<ArcSwap<Config>> = Lazy::new(|| ArcSwap::from_pointee(Config
23
23
/// Server role: primary or replica.
24
24
#[ derive( Clone , PartialEq , Serialize , Deserialize , Hash , std:: cmp:: Eq , Debug , Copy ) ]
25
25
pub enum Role {
26
+ #[ serde( alias = "primary" , alias = "Primary" ) ]
26
27
Primary ,
28
+ #[ serde( alias = "replica" , alias = "Replica" ) ]
27
29
Replica ,
28
30
}
29
31
@@ -202,17 +204,28 @@ impl Default for Pool {
202
204
}
203
205
}
204
206
207
+ #[ derive( Clone , PartialEq , Serialize , Deserialize , Debug , Hash , Eq ) ]
208
+ pub struct ServerConfig {
209
+ pub host : String ,
210
+ pub port : u16 ,
211
+ pub role : Role ,
212
+ }
213
+
205
214
/// Shard configuration.
206
215
#[ derive( Serialize , Deserialize , Debug , Clone , PartialEq ) ]
207
216
pub struct Shard {
208
217
pub database : String ,
209
- pub servers : Vec < ( String , u16 , String ) > ,
218
+ pub servers : Vec < ServerConfig > ,
210
219
}
211
220
212
221
impl Default for Shard {
213
222
fn default ( ) -> Shard {
214
223
Shard {
215
- servers : vec ! [ ( String :: from( "localhost" ) , 5432 , String :: from( "primary" ) ) ] ,
224
+ servers : vec ! [ ServerConfig {
225
+ host: String :: from( "localhost" ) ,
226
+ port: 5432 ,
227
+ role: Role :: Primary ,
228
+ } ] ,
216
229
database : String :: from ( "postgres" ) ,
217
230
}
218
231
}
@@ -538,23 +551,10 @@ pub async fn parse(path: &str) -> Result<(), Error> {
538
551
dup_check. insert ( server) ;
539
552
540
553
// Check that we define only zero or one primary.
541
- match server. 2 . as_ref ( ) {
542
- "primary" => primary_count += 1 ,
554
+ match server. role {
555
+ Role :: Primary => primary_count += 1 ,
543
556
_ => ( ) ,
544
557
} ;
545
-
546
- // Check role spelling.
547
- match server. 2 . as_ref ( ) {
548
- "primary" => ( ) ,
549
- "replica" => ( ) ,
550
- _ => {
551
- error ! (
552
- "Shard {} server role must be either 'primary' or 'replica', got: '{}'" ,
553
- shard. 0 , server. 2
554
- ) ;
555
- return Err ( Error :: BadConfig ) ;
556
- }
557
- } ;
558
558
}
559
559
560
560
if primary_count > 1 {
@@ -617,12 +617,12 @@ mod test {
617
617
assert_eq ! ( get_config( ) . pools[ "simple_db" ] . users. len( ) , 1 ) ;
618
618
619
619
assert_eq ! (
620
- get_config( ) . pools[ "sharded_db" ] . shards[ "0" ] . servers[ 0 ] . 0 ,
620
+ get_config( ) . pools[ "sharded_db" ] . shards[ "0" ] . servers[ 0 ] . host ,
621
621
"127.0.0.1"
622
622
) ;
623
623
assert_eq ! (
624
- get_config( ) . pools[ "sharded_db" ] . shards[ "1" ] . servers[ 0 ] . 2 ,
625
- "primary"
624
+ get_config( ) . pools[ "sharded_db" ] . shards[ "1" ] . servers[ 0 ] . role ,
625
+ Role :: Primary
626
626
) ;
627
627
assert_eq ! (
628
628
get_config( ) . pools[ "sharded_db" ] . shards[ "1" ] . database,
@@ -640,11 +640,11 @@ mod test {
640
640
assert_eq ! ( get_config( ) . pools[ "sharded_db" ] . default_role, "any" ) ;
641
641
642
642
assert_eq ! (
643
- get_config( ) . pools[ "simple_db" ] . shards[ "0" ] . servers[ 0 ] . 0 ,
643
+ get_config( ) . pools[ "simple_db" ] . shards[ "0" ] . servers[ 0 ] . host ,
644
644
"127.0.0.1"
645
645
) ;
646
646
assert_eq ! (
647
- get_config( ) . pools[ "simple_db" ] . shards[ "0" ] . servers[ 0 ] . 1 ,
647
+ get_config( ) . pools[ "simple_db" ] . shards[ "0" ] . servers[ 0 ] . port ,
648
648
5432
649
649
) ;
650
650
assert_eq ! (
0 commit comments