diff --git a/src/parsing.rs b/src/parsing.rs index eb350d5..08108e8 100644 --- a/src/parsing.rs +++ b/src/parsing.rs @@ -1503,6 +1503,8 @@ pub fn parse_group( .iter() .enumerate() .map(|(i, group_choice)| { + let rule_metadata = + RuleMetadata::from(group_choice.comments_before_grpchoice.as_ref()); // If we're a 1-element we should just wrap that type in the variant rather than // define a new struct just for each variant. // TODO: handle map-based enums? It would require being able to extract the key logic @@ -1518,15 +1520,17 @@ pub fn parse_group( } else { false }; - let variant_ident = - convert_to_camel_case(&match group_entry_to_raw_field_name(group_entry) { + let ident_name = rule_metadata.name.unwrap_or_else(|| { + match group_entry_to_raw_field_name(group_entry) { Some(name) => name, None => append_number_if_duplicate( &mut variants_names_used, ty.for_variant().to_string(), ), - }); - let variant_ident = VariantIdent::new_custom(variant_ident); + } + }); + let variant_ident = + VariantIdent::new_custom(convert_to_camel_case(&ident_name)); EnumVariant::new(variant_ident, ty, serialize_as_embedded) // None => { // // TODO: Weird case, group choice with only one fixed-value field. @@ -1539,8 +1543,6 @@ pub fn parse_group( // EnumVariant::new(variant_name.clone(), RustType::Rust(variant_name), true) // }, } else { - let rule_metadata = - RuleMetadata::from(group_choice.comments_before_grpchoice.as_ref()); let ident_name = rule_metadata.name.unwrap_or_else(|| format!("{name}{i}")); // General case, GroupN type identifiers and generate group choice since it's inlined here let variant_name = RustIdent::new(CDDLIdent::new(ident_name)); diff --git a/tests/core/input.cddl b/tests/core/input.cddl index d594bdb..0040ced 100644 --- a/tests/core/input.cddl +++ b/tests/core/input.cddl @@ -11,7 +11,7 @@ foo2 = #6.23([uint, opt_text]) bar = { foo: #6.1337(foo), ? derp: uint, - 1 : uint / null, + 1 : uint / null, ; @name one ? 5: text, five: 5, float: float64, @@ -125,8 +125,11 @@ top_level_array = [* uint] top_level_single_elem = [uint] overlapping_inlined = [ + ; @name one 0 // + ; @name two 0, uint // + ; @name three 0, uint, text ] @@ -134,7 +137,9 @@ overlapping0 = [0] overlapping1 = [0, uint] overlapping2 = [0, uint, text] -overlapping = overlapping0 / overlapping1 / overlapping2 +overlapping = overlapping0 ; @name A + / overlapping1 ; @name B + / overlapping2 ; @name C array_opt_fields = [ ? x: 1.010101 diff --git a/tests/core/tests.rs b/tests/core/tests.rs index 8d95705..50a3140 100644 --- a/tests/core/tests.rs +++ b/tests/core/tests.rs @@ -38,7 +38,11 @@ mod tests { #[test] fn bar() { - deser_test(&Bar::new(Foo::new(436, String::from("jfkdf"), vec![6, 4]), None, 3.3)); + let mut bar = Bar::new(Foo::new(436, String::from("jfkdf"), vec![6, 4]), None, 3.3); + deser_test(&bar); + // tests @name + bar.one = Some(10); + } #[test] @@ -201,22 +205,22 @@ mod tests { #[test] fn overlapping() { - let overlap0 = Overlapping::new_overlapping0(Overlapping0::new()); + let overlap0 = Overlapping::new_a(Overlapping0::new()); deser_test(&overlap0); - let overlap1 = Overlapping::new_overlapping1(Overlapping1::new(9)); + let overlap1 = Overlapping::new_b(Overlapping1::new(9)); deser_test(&overlap1); - let overlap2 = Overlapping::new_overlapping2(Overlapping2::new(5, "overlapping".into())); + let overlap2 = Overlapping::new_c(Overlapping2::new(5, "overlapping".into())); deser_test(&overlap2); } #[test] fn overlapping_inlined() { // this test won't work until https://github.com/dcSpark/cddl-codegen/issues/175 is resolved. - let overlap0 = OverlappingInlined::new_i0(); + let overlap0 = OverlappingInlined::new_one(); deser_test(&overlap0); - let overlap1 = OverlappingInlined::new_overlapping_inlined1(9); + let overlap1 = OverlappingInlined::new_two(9); //deser_test(&overlap1); - let overlap2 = OverlappingInlined::new_overlapping_inlined2(5, "overlapping".into()); + let overlap2 = OverlappingInlined::new_three(5, "overlapping".into()); //deser_test(&overlap2); }