11use indexmap:: IndexMap ;
22use itertools:: Itertools ;
3+ use serde:: de:: { MapAccess , Visitor , value:: MapAccessDeserializer } ;
34use serde:: { Deserialize , Deserializer } ;
45use std:: borrow:: Cow ;
56use std:: collections:: { BTreeMap , BTreeSet , Bound } ;
@@ -1092,13 +1093,7 @@ impl License {
10921093///
10931094/// The entry is derived from the email format of `John Doe <[email protected] >`. You need to 10941095/// provide at least name or email.
1095- #[ derive( Deserialize , Debug , Clone ) ]
1096- // deny_unknown_fields prevents using the name field when the email is not a string.
1097- #[ serde(
1098- untagged,
1099- deny_unknown_fields,
1100- expecting = "a table with 'name' and/or 'email' keys"
1101- ) ]
1096+ #[ derive( Debug , Clone ) ]
11021097pub ( crate ) enum Contact {
11031098 /// TODO(konsti): RFC 822 validation.
11041099 NameEmail { name : String , email : String } ,
@@ -1108,6 +1103,58 @@ pub(crate) enum Contact {
11081103 Email { email : String } ,
11091104}
11101105
1106+ impl < ' de > Deserialize < ' de > for Contact {
1107+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
1108+ where
1109+ D : Deserializer < ' de > ,
1110+ {
1111+ #[ derive( Deserialize ) ]
1112+ #[ serde( deny_unknown_fields) ]
1113+ struct Fields {
1114+ #[ serde( default , deserialize_with = "deserialize_string" ) ]
1115+ name : Option < String > ,
1116+ #[ serde( default , deserialize_with = "deserialize_string" ) ]
1117+ email : Option < String > ,
1118+ }
1119+
1120+ fn deserialize_string < ' de , D > ( deserializer : D ) -> Result < Option < String > , D :: Error >
1121+ where
1122+ D : Deserializer < ' de > ,
1123+ {
1124+ String :: deserialize ( deserializer) . map ( Some )
1125+ }
1126+
1127+ struct ContactVisitor ;
1128+
1129+ impl < ' de > Visitor < ' de > for ContactVisitor {
1130+ type Value = Contact ;
1131+
1132+ fn expecting ( & self , formatter : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
1133+ formatter. write_str ( "a table with 'name' and/or 'email' keys" )
1134+ }
1135+
1136+ fn visit_map < A > ( self , map : A ) -> Result < Self :: Value , A :: Error >
1137+ where
1138+ A : MapAccess < ' de > ,
1139+ {
1140+ let Fields { name, email } = Fields :: deserialize ( MapAccessDeserializer :: new ( map) ) ?;
1141+ match ( name, email) {
1142+ ( Some ( name) , Some ( email) ) => Ok ( Contact :: NameEmail { name, email } ) ,
1143+ ( Some ( name) , None ) => Ok ( Contact :: Name { name } ) ,
1144+ ( None , Some ( email) ) => Ok ( Contact :: Email { email } ) ,
1145+ ( None , None ) => Err ( serde:: de:: Error :: custom (
1146+ "a table with 'name' and/or 'email' keys" ,
1147+ ) ) ,
1148+ }
1149+ }
1150+ }
1151+
1152+ deserializer
1153+ . deserialize_map ( ContactVisitor )
1154+ . map_err ( |_| serde:: de:: Error :: custom ( "a table with 'name' and/or 'email' keys" ) )
1155+ }
1156+ }
1157+
11111158/// A trove classifier as originally specified in PEP 301.
11121159#[ derive( Debug , Clone ) ]
11131160pub ( crate ) struct Classifier ( String ) ;
@@ -1292,6 +1339,56 @@ mod tests {
12921339 formatted
12931340 }
12941341
1342+ /// A valid name must not make an invalid email acceptable.
1343+ #[ test]
1344+ fn contact_invalid_email ( ) {
1345+ let contents = extend_project ( r#"authors = [{ name = "Ferris", email = 42 }]"# ) ;
1346+ assert_snapshot ! ( toml:: from_str:: <PyProjectToml >( & contents) . unwrap_err( ) , @r#"
1347+ TOML parse error at line 4, column 11
1348+ |
1349+ 4 | authors = [{ name = "Ferris", email = 42 }]
1350+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1351+ a table with 'name' and/or 'email' keys
1352+ "# ) ;
1353+ }
1354+
1355+ #[ test]
1356+ fn contact_unknown_field ( ) {
1357+ let contents =
1358+ extend_project ( r#"maintainers = [{ email = "[email protected] ", extra = true }]"# ) ; 1359+ assert_snapshot ! ( toml:: from_str:: <PyProjectToml >( & contents) . unwrap_err( ) , @r#"
1360+ TOML parse error at line 4, column 15
1361+ |
1362+ 4 | maintainers = [{ email = "[email protected] ", extra = true }] 1363+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1364+ a table with 'name' and/or 'email' keys
1365+ "# ) ;
1366+ }
1367+
1368+ #[ test]
1369+ fn contact_empty ( ) {
1370+ let contents = extend_project ( "authors = [{}]" ) ;
1371+ assert_snapshot ! ( toml:: from_str:: <PyProjectToml >( & contents) . unwrap_err( ) , @r#"
1372+ TOML parse error at line 4, column 11
1373+ |
1374+ 4 | authors = [{}]
1375+ | ^^^^
1376+ a table with 'name' and/or 'email' keys
1377+ "# ) ;
1378+ }
1379+
1380+ #[ test]
1381+ fn contact_sequence ( ) {
1382+ let contents =
extend_project ( r#"authors = [["Ferris", "[email protected] "]]"# ) ; 1383+ assert_snapshot ! ( toml:: from_str:: <PyProjectToml >( & contents) . unwrap_err( ) , @r#"
1384+ TOML parse error at line 4, column 11
1385+ |
1386+ 4 | authors = [["Ferris", "[email protected] "]] 1387+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1388+ a table with 'name' and/or 'email' keys
1389+ "# ) ;
1390+ }
1391+
12951392 #[ test]
12961393 fn uppercase_package_name ( ) {
12971394 let contents = r#"
0 commit comments