Skip to content

Commit

Permalink
Skip the content of unknown variants for extensible CHOICES #6
Browse files Browse the repository at this point in the history
This allows the caller to recover from a new/unknown variant waiting
to be deserialized by skipping it.
  • Loading branch information
kellerkindt committed Apr 16, 2020
1 parent 37d52bd commit 2ec5a26
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
19 changes: 13 additions & 6 deletions asn1rs-model/src/gen/rust/uper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,9 @@ impl UperSerializer {
let mut block = Block::new("match variant");
for (i, (variant, role)) in enumeration.variants().enumerate() {
let mut block_case = Block::new(&format!("{} => Ok({}::{}(", i, name, variant));

let var_name = RustCodeGenerator::rust_module_name(variant);
let is_extended_variant = Self::is_extended_variant(enumeration, i);

if is_extended_variant {
if Self::is_extended_variant(enumeration, i) {
block_case.line(
"let mut reader = reader.read_substring_with_length_determinant_prefix()?;",
);
Expand All @@ -276,10 +274,19 @@ impl UperSerializer {
block_case.after(")),");
block.push_block(block_case);
}
block.line(format!(
"_ => Err(UperError::ValueNotInRange(variant, 0, {}))",
let err_line = format!(
"Err(UperError::ValueNotInRange(variant, 0, {}))",
enumeration.len() - 1
));
);
if enumeration.is_extensible() {
let mut block_default = Block::new("_ => ");
block_default.line("// skip the content of the unknown variant");
block_default.line("let _ = reader.read_substring_with_length_determinant_prefix()?;");
block_default.line(err_line);
block.push_block(block_default);
} else {
block.line(format!("_ => {}", err_line));
}
function.push_block(block);
}

Expand Down
4 changes: 4 additions & 0 deletions asn1rs-model/src/model/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ impl<T> Enumeration<T> {
pub fn last_standard_index(&self) -> Option<usize> {
self.extended_after_index
}

pub fn is_extensible(&self) -> bool {
self.extended_after_index.is_some()
}
}

impl Model<Rust> {
Expand Down

0 comments on commit 2ec5a26

Please sign in to comment.