Skip to content

Commit

Permalink
Fix @name not working on single elem group choices (#213)
Browse files Browse the repository at this point in the history
* Fix @name not working on single elem group choices

Fixes #211
Fixes #153

* Add use of @name into test cases (tests compiling)
  • Loading branch information
rooooooooob authored Nov 21, 2023
1 parent 8f3c90e commit d84f78a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
14 changes: 8 additions & 6 deletions src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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));
Expand Down
9 changes: 7 additions & 2 deletions tests/core/input.cddl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -125,16 +125,21 @@ top_level_array = [* uint]
top_level_single_elem = [uint]

overlapping_inlined = [
; @name one
0 //
; @name two
0, uint //
; @name three
0, uint, text
]

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
Expand Down
18 changes: 11 additions & 7 deletions tests/core/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit d84f78a

Please sign in to comment.