@@ -1329,8 +1329,7 @@ pub struct Flake8ImportConventionsOptions {
1329
1329
scipy = "sp"
1330
1330
"#
1331
1331
) ]
1332
- #[ serde( deserialize_with = "deserialize_and_validate_aliases" ) ]
1333
- pub aliases : Option < FxHashMap < String , String > > ,
1332
+ pub aliases : Option < FxHashMap < ModuleName , Alias > > ,
1334
1333
1335
1334
/// A mapping from module to conventional import alias. These aliases will
1336
1335
/// be added to the [`aliases`](#lint_flake8-import-conventions_aliases) mapping.
@@ -1373,34 +1372,67 @@ pub struct Flake8ImportConventionsOptions {
1373
1372
pub banned_from : Option < FxHashSet < String > > ,
1374
1373
}
1375
1374
1376
- fn deserialize_and_validate_aliases < ' de , D > (
1377
- deserializer : D ,
1378
- ) -> Result < Option < FxHashMap < String , String > > , D :: Error >
1379
- where
1380
- D : Deserializer < ' de > ,
1381
- {
1382
- let Some ( aliases) = Option :: < FxHashMap < String , String > > :: deserialize ( deserializer) ? else {
1383
- return Ok ( None ) ;
1384
- } ;
1385
-
1386
- for ( module, alias) in & aliases {
1387
- if module. is_empty ( ) || module. split ( '.' ) . any ( |part| !is_identifier ( part) ) {
1388
- return Err ( de:: Error :: custom (
1389
- "module must be a valid identifier separated by single periods" ,
1390
- ) ) ;
1391
- }
1392
- if !is_identifier ( alias) {
1393
- return Err ( de:: Error :: custom ( "alias must be a valid identifier" ) ) ;
1375
+ #[ derive( Clone , Debug , PartialEq , Eq , Hash , Default , Serialize ) ]
1376
+ #[ cfg_attr( feature = "schemars" , derive( schemars:: JsonSchema ) ) ]
1377
+ pub struct ModuleName ( String ) ;
1378
+
1379
+ impl ModuleName {
1380
+ pub fn into_string ( self ) -> String {
1381
+ self . 0
1382
+ }
1383
+ }
1384
+
1385
+ impl < ' de > Deserialize < ' de > for ModuleName {
1386
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
1387
+ where
1388
+ D : Deserializer < ' de > ,
1389
+ {
1390
+ let name = String :: deserialize ( deserializer) ?;
1391
+ if name. is_empty ( ) || name. split ( '.' ) . any ( |part| !is_identifier ( part) ) {
1392
+ Err ( de:: Error :: invalid_value (
1393
+ de:: Unexpected :: Str ( & name) ,
1394
+ & "module must be a valid identifier separated by single periods" ,
1395
+ ) )
1396
+ } else {
1397
+ Ok ( Self ( name) )
1394
1398
}
1395
1399
}
1400
+ }
1401
+
1402
+ #[ derive( Clone , Debug , PartialEq , Eq , Hash , Default , Serialize ) ]
1403
+ #[ cfg_attr( feature = "schemars" , derive( schemars:: JsonSchema ) ) ]
1404
+ pub struct Alias ( String ) ;
1405
+
1406
+ impl Alias {
1407
+ pub fn into_string ( self ) -> String {
1408
+ self . 0
1409
+ }
1410
+ }
1396
1411
1397
- Ok ( Some ( aliases) ) // Return the validated map.
1412
+ impl < ' de > Deserialize < ' de > for Alias {
1413
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
1414
+ where
1415
+ D : Deserializer < ' de > ,
1416
+ {
1417
+ let name = String :: deserialize ( deserializer) ?;
1418
+ if is_identifier ( & name) {
1419
+ Ok ( Self ( name) )
1420
+ } else {
1421
+ Err ( de:: Error :: invalid_value (
1422
+ de:: Unexpected :: Str ( & name) ,
1423
+ & "alias must be a valid identifier" ,
1424
+ ) )
1425
+ }
1426
+ }
1398
1427
}
1399
1428
1400
1429
impl Flake8ImportConventionsOptions {
1401
1430
pub fn into_settings ( self ) -> flake8_import_conventions:: settings:: Settings {
1402
- let mut aliases = match self . aliases {
1403
- Some ( options_aliases) => options_aliases,
1431
+ let mut aliases: FxHashMap < String , String > = match self . aliases {
1432
+ Some ( options_aliases) => options_aliases
1433
+ . into_iter ( )
1434
+ . map ( |( module, alias) | ( module. into_string ( ) , alias. into_string ( ) ) )
1435
+ . collect ( ) ,
1404
1436
None => flake8_import_conventions:: settings:: default_aliases ( ) ,
1405
1437
} ;
1406
1438
if let Some ( extend_aliases) = self . extend_aliases {
0 commit comments