@@ -176,13 +176,23 @@ impl FromStr for ServerAddress {
176
176
}
177
177
178
178
impl ServerAddress {
179
- /// Parses an address string into a `StreamAddress `.
179
+ /// Parses an address string into a `ServerAddress `.
180
180
pub fn parse ( address : impl AsRef < str > ) -> Result < Self > {
181
181
let address = address. as_ref ( ) ;
182
182
let mut parts = address. split ( ':' ) ;
183
-
184
183
let hostname = match parts. next ( ) {
185
- Some ( part) => part,
184
+ Some ( part) => {
185
+ if part. is_empty ( ) {
186
+ return Err ( ErrorKind :: InvalidArgument {
187
+ message : format ! (
188
+ "invalid server address: \" {}\" ; hostname cannot be empty" ,
189
+ address
190
+ ) ,
191
+ }
192
+ . into ( ) ) ;
193
+ }
194
+ part
195
+ }
186
196
None => {
187
197
return Err ( ErrorKind :: InvalidArgument {
188
198
message : format ! ( "invalid server address: \" {}\" " , address) ,
@@ -200,6 +210,15 @@ impl ServerAddress {
200
210
) ,
201
211
} ) ?;
202
212
213
+ if port == 0 {
214
+ return Err ( ErrorKind :: InvalidArgument {
215
+ message : format ! (
216
+ "invalid server address: \" {}\" ; port must be non-zero" ,
217
+ address
218
+ ) ,
219
+ }
220
+ . into ( ) ) ;
221
+ }
203
222
if parts. next ( ) . is_some ( ) {
204
223
return Err ( ErrorKind :: InvalidArgument {
205
224
message : format ! (
@@ -216,7 +235,7 @@ impl ServerAddress {
216
235
} ;
217
236
218
237
Ok ( ServerAddress :: Tcp {
219
- host : hostname. to_string ( ) ,
238
+ host : hostname. to_lowercase ( ) ,
220
239
port,
221
240
} )
222
241
}
@@ -1327,52 +1346,7 @@ impl ClientOptionsParser {
1327
1346
None => ( None , None ) ,
1328
1347
} ;
1329
1348
1330
- let hosts: Result < Vec < _ > > = hosts_section
1331
- . split ( ',' )
1332
- . map ( |host| {
1333
- let ( hostname, port) = match host. find ( ':' ) {
1334
- Some ( index) => host. split_at ( index) ,
1335
- None => ( host, "" ) ,
1336
- } ;
1337
-
1338
- if hostname. is_empty ( ) {
1339
- return Err ( ErrorKind :: InvalidArgument {
1340
- message : "connection string contains no host" . to_string ( ) ,
1341
- }
1342
- . into ( ) ) ;
1343
- }
1344
- let port = if port. is_empty ( ) {
1345
- None
1346
- } else {
1347
- let port_string_without_colon = & port[ 1 ..] ;
1348
- let p: u16 = port_string_without_colon. parse ( ) . map_err ( |_| {
1349
- ErrorKind :: InvalidArgument {
1350
- message : format ! (
1351
- "invalid port specified in connection string: {}" ,
1352
- port
1353
- ) ,
1354
- }
1355
- } ) ?;
1356
-
1357
- if p == 0 {
1358
- return Err ( ErrorKind :: InvalidArgument {
1359
- message : format ! (
1360
- "invalid port specified in connection string: {}" ,
1361
- port
1362
- ) ,
1363
- }
1364
- . into ( ) ) ;
1365
- }
1366
-
1367
- Some ( p)
1368
- } ;
1369
-
1370
- Ok ( ServerAddress :: Tcp {
1371
- host : hostname. to_lowercase ( ) ,
1372
- port,
1373
- } )
1374
- } )
1375
- . collect ( ) ;
1349
+ let hosts: Result < Vec < _ > > = hosts_section. split ( ',' ) . map ( ServerAddress :: parse) . collect ( ) ;
1376
1350
1377
1351
let hosts = hosts?;
1378
1352
0 commit comments