@@ -35,7 +35,7 @@ use serde_with::{
3535 NoneAsEmptyString , OneOrMany , Same , Seq , StringWithSeparator ,
3636} ;
3737use std:: {
38- collections:: HashMap ,
38+ collections:: { HashMap , HashSet } ,
3939 sync:: { Mutex , RwLock } ,
4040} ;
4141
@@ -1323,6 +1323,122 @@ fn test_one_or_many_prefer_many() {
13231323 ) ;
13241324}
13251325
1326+ #[ test]
1327+ fn test_one_or_many_hashset_prefer_one ( ) {
1328+ #[ serde_as]
1329+ #[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
1330+ struct S1 ( #[ serde_as( as = "OneOrMany<_>" ) ] HashSet < u32 > ) ;
1331+
1332+ is_equal ( S1 ( HashSet :: new ( ) ) , expect ! [ [ r#"[]"# ] ] ) ;
1333+ is_equal ( S1 ( HashSet :: from ( [ 1 ] ) ) , expect ! [ [ r#"1"# ] ] ) ;
1334+ check_deserialization ( S1 ( HashSet :: from ( [ 1 ] ) ) , r#"1"# ) ;
1335+ check_deserialization ( S1 ( HashSet :: from ( [ 1 ] ) ) , r#"[1]"# ) ;
1336+ check_deserialization ( S1 ( HashSet :: from ( [ 1 , 2 , 3 ] ) ) , r#"[1, 2, 3]"# ) ;
1337+ check_deserialization ( S1 ( HashSet :: from ( [ 1 , 2 , 3 ] ) ) , r#"[3, 2, 1]"# ) ;
1338+ check_error_deserialization :: < S1 > (
1339+ r#""xx""# ,
1340+ expect ! [ [ r#"
1341+ OneOrMany could not deserialize any variant:
1342+ One: invalid type: string "xx", expected u32
1343+ Many: invalid type: string "xx", expected a sequence"# ] ] ,
1344+ ) ;
1345+
1346+ #[ serde_as]
1347+ #[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
1348+ struct S2 ( #[ serde_as( as = "OneOrMany<DisplayFromStr>" ) ] HashSet < u32 > ) ;
1349+
1350+ is_equal ( S2 ( HashSet :: from ( [ 1 ] ) ) , expect ! [ [ r#""1""# ] ] ) ;
1351+ check_deserialization ( S2 ( HashSet :: from ( [ 1 ] ) ) , r#""1""# ) ;
1352+ check_deserialization ( S2 ( HashSet :: from ( [ 1 ] ) ) , r#"["1"]"# ) ;
1353+ check_deserialization ( S2 ( HashSet :: from ( [ 1 , 2 , 3 ] ) ) , r#"["1", "2", "3"]"# ) ;
1354+ check_error_deserialization :: < S2 > (
1355+ r#"{}"# ,
1356+ expect ! [ [ r#"
1357+ OneOrMany could not deserialize any variant:
1358+ One: invalid type: map, expected a string
1359+ Many: invalid type: map, expected a sequence"# ] ] ,
1360+ ) ;
1361+ }
1362+
1363+ #[ test]
1364+ fn test_one_or_many_hashset_prefer_many ( ) {
1365+ use serde_with:: formats:: PreferMany ;
1366+
1367+ #[ serde_as]
1368+ #[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
1369+ struct S1 ( #[ serde_as( as = "OneOrMany<_, PreferMany>" ) ] HashSet < u32 > ) ;
1370+
1371+ is_equal ( S1 ( HashSet :: new ( ) ) , expect ! [ [ r#"[]"# ] ] ) ;
1372+ is_equal (
1373+ S1 ( HashSet :: from ( [ 1 ] ) ) ,
1374+ expect ! [ [ r#"
1375+ [
1376+ 1
1377+ ]"# ] ] ,
1378+ ) ;
1379+ check_deserialization ( S1 ( HashSet :: from ( [ 1 ] ) ) , r#"1"# ) ;
1380+ check_deserialization ( S1 ( HashSet :: from ( [ 1 ] ) ) , r#"[1]"# ) ;
1381+ check_deserialization ( S1 ( HashSet :: from ( [ 1 , 2 , 3 ] ) ) , r#"[1, 2, 3]"# ) ;
1382+ }
1383+
1384+ #[ test]
1385+ fn test_one_or_many_btreeset_prefer_one ( ) {
1386+ #[ serde_as]
1387+ #[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
1388+ struct S1 ( #[ serde_as( as = "OneOrMany<_>" ) ] BTreeSet < u32 > ) ;
1389+
1390+ is_equal ( S1 ( BTreeSet :: new ( ) ) , expect ! [ [ r#"[]"# ] ] ) ;
1391+ is_equal ( S1 ( BTreeSet :: from ( [ 1 ] ) ) , expect ! [ [ r#"1"# ] ] ) ;
1392+ check_deserialization ( S1 ( BTreeSet :: from ( [ 1 ] ) ) , r#"1"# ) ;
1393+ check_deserialization ( S1 ( BTreeSet :: from ( [ 1 ] ) ) , r#"[1]"# ) ;
1394+ check_deserialization ( S1 ( BTreeSet :: from ( [ 1 , 2 , 3 ] ) ) , r#"[1, 2, 3]"# ) ;
1395+ check_deserialization ( S1 ( BTreeSet :: from ( [ 1 , 2 , 3 ] ) ) , r#"[3, 2, 1]"# ) ;
1396+ check_error_deserialization :: < S1 > (
1397+ r#""xx""# ,
1398+ expect ! [ [ r#"
1399+ OneOrMany could not deserialize any variant:
1400+ One: invalid type: string "xx", expected u32
1401+ Many: invalid type: string "xx", expected a sequence"# ] ] ,
1402+ ) ;
1403+
1404+ #[ serde_as]
1405+ #[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
1406+ struct S2 ( #[ serde_as( as = "OneOrMany<DisplayFromStr>" ) ] BTreeSet < u32 > ) ;
1407+
1408+ is_equal ( S2 ( BTreeSet :: from ( [ 1 ] ) ) , expect ! [ [ r#""1""# ] ] ) ;
1409+ check_deserialization ( S2 ( BTreeSet :: from ( [ 1 ] ) ) , r#""1""# ) ;
1410+ check_deserialization ( S2 ( BTreeSet :: from ( [ 1 ] ) ) , r#"["1"]"# ) ;
1411+ check_deserialization ( S2 ( BTreeSet :: from ( [ 1 , 2 , 3 ] ) ) , r#"["1", "2", "3"]"# ) ;
1412+ check_error_deserialization :: < S2 > (
1413+ r#"{}"# ,
1414+ expect ! [ [ r#"
1415+ OneOrMany could not deserialize any variant:
1416+ One: invalid type: map, expected a string
1417+ Many: invalid type: map, expected a sequence"# ] ] ,
1418+ ) ;
1419+ }
1420+
1421+ #[ test]
1422+ fn test_one_or_many_btreeset_prefer_many ( ) {
1423+ use serde_with:: formats:: PreferMany ;
1424+
1425+ #[ serde_as]
1426+ #[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
1427+ struct S1 ( #[ serde_as( as = "OneOrMany<_, PreferMany>" ) ] BTreeSet < u32 > ) ;
1428+
1429+ is_equal ( S1 ( BTreeSet :: new ( ) ) , expect ! [ [ r#"[]"# ] ] ) ;
1430+ is_equal (
1431+ S1 ( BTreeSet :: from ( [ 1 ] ) ) ,
1432+ expect ! [ [ r#"
1433+ [
1434+ 1
1435+ ]"# ] ] ,
1436+ ) ;
1437+ check_deserialization ( S1 ( BTreeSet :: from ( [ 1 ] ) ) , r#"1"# ) ;
1438+ check_deserialization ( S1 ( BTreeSet :: from ( [ 1 ] ) ) , r#"[1]"# ) ;
1439+ check_deserialization ( S1 ( BTreeSet :: from ( [ 1 , 2 , 3 ] ) ) , r#"[1, 2, 3]"# ) ;
1440+ }
1441+
13261442/// Test that Cow borrows from the input
13271443#[ test]
13281444fn test_borrow_cow_str ( ) {
0 commit comments