Skip to content

Commit

Permalink
Fix extensible fields in SEQUENCE and SET not treat as optional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerkindt committed Jun 9, 2021
1 parent a4cec0e commit 75f2882
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 152 deletions.
11 changes: 8 additions & 3 deletions asn1rs-model/src/model/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ impl Model<Rust> {
fields,
extension_after,
}) => {
let fields = Self::asn_fields_to_rust_fields(name, fields, ctxt);
let fields = Self::asn_fields_to_rust_fields(name, fields, *extension_after, ctxt);
ctxt.add_definition(Definition(
name.into(),
Rust::Struct {
Expand All @@ -748,7 +748,7 @@ impl Model<Rust> {
fields,
extension_after,
}) => {
let fields = Self::asn_fields_to_rust_fields(name, fields, ctxt);
let fields = Self::asn_fields_to_rust_fields(name, fields, *extension_after, ctxt);
ctxt.add_definition(Definition(
name.into(),
Rust::Struct {
Expand Down Expand Up @@ -820,17 +820,22 @@ impl Model<Rust> {
fn asn_fields_to_rust_fields(
name: &str,
fields: &[crate::model::Field<Asn>],
extension_after: Option<usize>,
ctxt: &mut Context<'_>,
) -> Vec<Field> {
let mut rust_fields = Vec::with_capacity(fields.len());

for field in fields.iter() {
for (index, field) in fields.iter().enumerate() {
let rust_name = format!("{}{}", name, ctxt.struct_or_enum_name(&field.name));
let tag = field.role.tag;
let rust_role =
Self::definition_type_to_rust_type(&rust_name, &field.role.r#type, tag, ctxt);
let rust_role = if let Some(def) = &field.role.default {
RustType::Default(Box::new(rust_role.no_option()), def.clone())
} else if extension_after.map(|e| index > e).unwrap_or(false)
&& !rust_role.is_optional()
{
RustType::Option(Box::new(rust_role))
} else {
rust_role
};
Expand Down
14 changes: 8 additions & 6 deletions src/io/per/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub enum Error {
InsufficientSpaceInDestinationBuffer,
InsufficientDataInSourceBuffer,
InvalidChoiceIndex(u64, u64),
InvalidExtensionConstellation(bool, bool),
ExtensionFieldsInconsistent(String),
ValueNotInRange(i64, i64, i64),
ValueExceedsMaxInt,
ValueIsNegativeButExpectedUnsigned(i64),
Expand Down Expand Up @@ -55,11 +55,13 @@ impl std::fmt::Display for Error {
"Unexpected choice-index {} with variant count {}",
index, variant_count
),
Error::InvalidExtensionConstellation(expects, has) => write!(
f,
"Unexpected extension constellation, expected: {}, read: {}",
expects, has
),
Error::ExtensionFieldsInconsistent(name) => {
write!(
f,
"The extension fields of {} are inconsistent, either all or none must be present",
name
)
}
Error::ValueNotInRange(value, min, max) => write!(
f,
"The value {} is not within the inclusive range of {} and {}",
Expand Down
Loading

0 comments on commit 75f2882

Please sign in to comment.