@@ -933,6 +933,63 @@ impl StringValidator {
933933 }
934934}
935935
936+ /// A re-ordering of JSON schema instance types that puts integer values before
937+ /// number values.
938+ ///
939+ /// This is used for untagged enum generation to ensure that integer values
940+ /// are matched before number values.
941+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
942+ pub ( crate ) enum ReorderedInstanceType {
943+ /// The JSON schema instance type `null`.
944+ Null ,
945+
946+ /// The JSON schema instance type `boolean`.
947+ Boolean ,
948+
949+ /// The JSON schema instance type `integer`.
950+ Integer ,
951+
952+ /// The JSON schema instance type `number`.
953+ Number ,
954+
955+ /// The JSON schema instance type `string`.
956+ String ,
957+
958+ /// The JSON schema instance type `array`.
959+ Array ,
960+
961+ /// The JSON schema instance type `object`.
962+ Object ,
963+ }
964+
965+ impl From < InstanceType > for ReorderedInstanceType {
966+ fn from ( instance_type : InstanceType ) -> Self {
967+ match instance_type {
968+ InstanceType :: Null => Self :: Null ,
969+ InstanceType :: Boolean => Self :: Boolean ,
970+ InstanceType :: Object => Self :: Object ,
971+ InstanceType :: Array => Self :: Array ,
972+ InstanceType :: Integer => Self :: Integer ,
973+ InstanceType :: Number => Self :: Number ,
974+ InstanceType :: String => Self :: String ,
975+ }
976+ }
977+ }
978+
979+ impl From < ReorderedInstanceType > for InstanceType {
980+ fn from ( instance_type : ReorderedInstanceType ) -> Self {
981+ match instance_type {
982+ ReorderedInstanceType :: Null => Self :: Null ,
983+ ReorderedInstanceType :: Boolean => Self :: Boolean ,
984+ ReorderedInstanceType :: Object => Self :: Object ,
985+ ReorderedInstanceType :: Array => Self :: Array ,
986+ ReorderedInstanceType :: Integer => Self :: Integer ,
987+ ReorderedInstanceType :: Number => Self :: Number ,
988+ ReorderedInstanceType :: String => Self :: String ,
989+ }
990+ }
991+ }
992+
936993#[ cfg( test) ]
937994mod tests {
938995 use std:: collections:: BTreeMap ;
@@ -944,7 +1001,7 @@ mod tests {
9441001 } ;
9451002
9461003 use crate :: {
947- util:: { decode_segment, sanitize, schemas_mutually_exclusive, Case } ,
1004+ util:: { decode_segment, sanitize, schemas_mutually_exclusive, Case , ReorderedInstanceType } ,
9481005 Name ,
9491006 } ;
9501007
@@ -1121,4 +1178,22 @@ mod tests {
11211178 assert ! ( ach. is_valid( "Meshach" ) ) ;
11221179 assert ! ( !ach. is_valid( "Abednego" ) ) ;
11231180 }
1181+
1182+ #[ test]
1183+ fn test_instance_type_ordering ( ) {
1184+ let null = ReorderedInstanceType :: Null ;
1185+ let boolean = ReorderedInstanceType :: Boolean ;
1186+ let integer = ReorderedInstanceType :: Integer ;
1187+ let number = ReorderedInstanceType :: Number ;
1188+ let string = ReorderedInstanceType :: String ;
1189+ let array = ReorderedInstanceType :: Array ;
1190+ let object = ReorderedInstanceType :: Object ;
1191+
1192+ assert ! ( null < boolean) ;
1193+ assert ! ( boolean < integer) ;
1194+ assert ! ( integer < number) ;
1195+ assert ! ( number < string) ;
1196+ assert ! ( string < array) ;
1197+ assert ! ( array < object) ;
1198+ }
11241199}
0 commit comments