Skip to content

Commit 461195f

Browse files
committed
fix: Actually use number of root variants in CHOICE variance constraints
fixes librasn#528
1 parent f2c5611 commit 461195f

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

macros/macros_impl/src/enum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ impl Enum<'_> {
102102
// Check count of the root components in the choice
103103
// https://github.com/XAMPPRocky/rasn/issues/168
104104
// Choice index starts from zero, so we need to reduce variance by one
105-
let variant_count = if self.variants.is_empty() {
105+
let variant_count = if base_variants.is_empty() {
106106
0
107107
} else {
108-
self.variants.len() - 1
108+
base_variants.len() - 1
109109
};
110110

111111
let variance_constraint = Constraints {

src/uper.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,50 @@ mod tests {
271271
round_trip!(uper, Choice, Choice::Normal, &[0]);
272272
round_trip!(uper, Choice, Choice::Medium, &[0x80, 1, 0]);
273273
round_trip!(aper, Choice, Choice::Medium, &[0x80, 1, 0]);
274+
275+
#[derive(AsnType, Decode, Debug, Encode, PartialEq)]
276+
#[rasn(choice, automatic_tags)]
277+
#[non_exhaustive]
278+
enum ChoiceWithData {
279+
Normal(u8),
280+
High(u8),
281+
}
282+
283+
round_trip!(
284+
uper,
285+
ChoiceWithData,
286+
ChoiceWithData::Normal(0xaf),
287+
&[0b0010_1011, 0b1100_0000]
288+
);
289+
round_trip!(
290+
uper,
291+
ChoiceWithData,
292+
ChoiceWithData::High(0xaf),
293+
&[0b0110_1011, 0b1100_0000]
294+
);
295+
296+
#[derive(AsnType, Decode, Debug, Encode, PartialEq)]
297+
#[rasn(choice, automatic_tags)]
298+
#[non_exhaustive]
299+
enum ExtendedChoiceWithData {
300+
Normal(u8),
301+
High(u8),
302+
#[rasn(extension_addition)]
303+
Medium(u8),
304+
}
305+
306+
round_trip!(
307+
uper,
308+
ExtendedChoiceWithData,
309+
ExtendedChoiceWithData::Normal(0xaf),
310+
&[0b0010_1011, 0b1100_0000]
311+
);
312+
round_trip!(
313+
uper,
314+
ExtendedChoiceWithData,
315+
ExtendedChoiceWithData::Medium(0x42),
316+
&[0x80, 1, 0x42]
317+
);
274318
}
275319

276320
#[test]

0 commit comments

Comments
 (0)