@@ -23,6 +23,18 @@ impl RSConvert<RSEnum> for GTUnion {
2323
2424 trim_variant_names ( & name, & mut variants, & mut variant_names) ;
2525
26+ if context. config ( ) . derive . contains ( & "Default" . into ( ) ) {
27+ let default_attrs = variants
28+ . iter ( )
29+ . flat_map ( |variant| variant. attributes . iter ( ) . find ( |attr| attr. 0 == "default" ) ) ;
30+ let count = default_attrs. clone ( ) . count ( ) ;
31+ if count == 0 {
32+ return Err ( RSConverterError :: MissingDefaultVariant ( self . span . clone ( ) ) . into ( ) ) ;
33+ } else if count > 1 {
34+ return Err ( RSConverterError :: MultipleDefaultVariants ( self . span . clone ( ) ) . into ( ) ) ;
35+ }
36+ }
37+
2638 let r#enum = RSEnum {
2739 id,
2840 doc,
@@ -55,6 +67,10 @@ fn convert_variant(
5567
5668 context. enter_parent ( RSContextParent :: EnumVariant ( variant_name. clone ( ) ) ) ;
5769
70+ if GTAttribute :: find_flag ( descriptor. attributes ( ) , "default" ) {
71+ attributes. push ( RSAttribute ( "default" . into ( ) ) ) ;
72+ }
73+
5874 let descriptor = match descriptor {
5975 GTDescriptor :: Literal ( literal) => {
6076 let str = render_literal ( literal) ;
@@ -970,4 +986,130 @@ mod tests {
970986 "#
971987 ) ;
972988 }
989+
990+ #[ test]
991+ fn test_attr_default ( ) {
992+ let mut context = Gtrs :: convert_context_with_parent ( "Status" ) ;
993+ let mut config = RsConfigLang :: default ( ) ;
994+ config. derive . push ( "Default" . into ( ) ) ;
995+ context. assign_config ( config) ;
996+ let union = Gt :: union ( descriptor_nodes ! [
997+ Gt :: primitive_string( ) ,
998+ node_with!(
999+ Gt :: primitive_number( ) ,
1000+ attributes = vec![ attribute_node!( default ) ]
1001+ ) ,
1002+ ] ) ;
1003+ assert_debug_snapshot ! (
1004+ convert_node_with( union , & mut context) ,
1005+ @r#"
1006+ RSEnum {
1007+ id: GTDefinitionId(
1008+ GTModuleId(
1009+ "module",
1010+ ),
1011+ "Status",
1012+ ),
1013+ doc: None,
1014+ attributes: [
1015+ RSAttribute(
1016+ "derive(Debug, Clone, PartialEq, Serialize, Deserialize)",
1017+ ),
1018+ RSAttribute(
1019+ "serde(untagged)",
1020+ ),
1021+ ],
1022+ name: RSIdentifier(
1023+ "Status",
1024+ ),
1025+ variants: [
1026+ RSEnumVariant {
1027+ doc: None,
1028+ attributes: [],
1029+ name: RSIdentifier(
1030+ "String",
1031+ ),
1032+ descriptor: Some(
1033+ Descriptor(
1034+ Primitive(
1035+ String,
1036+ ),
1037+ ),
1038+ ),
1039+ },
1040+ RSEnumVariant {
1041+ doc: None,
1042+ attributes: [
1043+ RSAttribute(
1044+ "default",
1045+ ),
1046+ ],
1047+ name: RSIdentifier(
1048+ "Number",
1049+ ),
1050+ descriptor: Some(
1051+ Descriptor(
1052+ Primitive(
1053+ Float64,
1054+ ),
1055+ ),
1056+ ),
1057+ },
1058+ ],
1059+ }
1060+ "#
1061+ ) ;
1062+ }
1063+
1064+ #[ test]
1065+ fn test_attr_default_missing_err ( ) {
1066+ let mut context = Gtrs :: convert_context_with_parent ( "Status" ) ;
1067+ let mut config = RsConfigLang :: default ( ) ;
1068+ config. derive . push ( "Default" . into ( ) ) ;
1069+ context. assign_config ( config) ;
1070+ let union = Gt :: union ( descriptor_nodes ! [
1071+ Gt :: primitive_string( ) ,
1072+ Gt :: primitive_number( )
1073+ ] ) ;
1074+ assert_debug_snapshot ! (
1075+ convert_node_err_with( union , & mut context) ,
1076+ @"
1077+ MissingDefaultVariant(
1078+ GTSpan(
1079+ 0,
1080+ 0,
1081+ ),
1082+ )
1083+ "
1084+ ) ;
1085+ }
1086+
1087+ #[ test]
1088+ fn test_attr_default_multiple_err ( ) {
1089+ let mut context = Gtrs :: convert_context_with_parent ( "Status" ) ;
1090+ let mut config = RsConfigLang :: default ( ) ;
1091+ config. derive . push ( "Default" . into ( ) ) ;
1092+ context. assign_config ( config) ;
1093+ let union = Gt :: union ( descriptor_nodes ! [
1094+ node_with!(
1095+ Gt :: primitive_string( ) ,
1096+ attributes = vec![ attribute_node!( default ) ]
1097+ ) ,
1098+ node_with!(
1099+ Gt :: primitive_number( ) ,
1100+ attributes = vec![ attribute_node!( default ) ]
1101+ )
1102+ ] ) ;
1103+ assert_debug_snapshot ! (
1104+ convert_node_err_with( union , & mut context) ,
1105+ @"
1106+ MultipleDefaultVariants(
1107+ GTSpan(
1108+ 0,
1109+ 0,
1110+ ),
1111+ )
1112+ "
1113+ ) ;
1114+ }
9731115}
0 commit comments