Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions rasn-compiler/src/generator/rasn/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use super::{
TaggingEnvironment,
};
use crate::generator::error::{GeneratorError, GeneratorErrorType};
use crate::prelude::ir::SequenceComponent;

pub(crate) const INNER_ARRAY_LIKE_PREFIX: &str = "Anonymous_";

Expand Down Expand Up @@ -681,17 +682,11 @@ impl Rasn {
match tld.ty {
ASN1Type::Sequence(ref seq) | ASN1Type::Set(ref seq) => {
let name = self.to_rust_title_case(&tld.name);
let extensible = seq
.extensible
.or(
(self.extensibility_environment == ExtensibilityEnvironment::Implied)
.then_some(seq.members.len()),
)
.map(|_| {
quote! {
#[non_exhaustive]}
})
.unwrap_or_default();
let extensible = if seq.extensible || self.extensibility_environment == ExtensibilityEnvironment::Implied {
quote! {#[non_exhaustive]}
} else {
TokenStream::new()
};
let set_annotation = if let ASN1Type::Set(_) = tld.ty {
quote!(set)
} else {
Expand All @@ -700,7 +695,8 @@ impl Rasn {
let class_fields = if self.config.opaque_open_types {
TokenStream::new()
} else {
seq.members.iter().fold(
seq.direct_members()
.fold(
TokenStream::new(),
|mut acc, m| {
[
Expand Down Expand Up @@ -747,7 +743,7 @@ impl Rasn {
self.format_tag(
tld.tag.as_ref(),
self.tagging_environment == TaggingEnvironment::Automatic
&& !seq.members.iter().any(|m| m.tag.is_some()),
&& !seq.direct_members().any(|m| m.tag.is_some()),
),
];
if name.to_string() != tld.name {
Expand All @@ -764,7 +760,7 @@ impl Rasn {
formatted_members.struct_body,
formatted_members.nested_anonymous_types,
self.join_annotations(annotations, false, true)?,
self.format_default_methods(&seq.members, &name.to_string())?,
self.format_default_methods(&seq.direct_members().cloned().collect(), &name.to_string())?,
self.format_new_impl(&name, formatted_members.name_types),
class_fields,
))
Expand Down
50 changes: 25 additions & 25 deletions rasn-compiler/src/generator/rasn/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,8 @@ impl Rasn {
sequence_or_set: &SequenceOrSet,
parent_name: &String,
) -> Result<FormattedMembers, GeneratorError> {
let first_extension_index = sequence_or_set.extensible;

sequence_or_set.members.iter().enumerate().try_fold(
let extension_range = sequence_or_set.extension_range();
sequence_or_set.direct_members().enumerate().try_fold(
FormattedMembers::default(),
|mut acc, (i, m)| {
let nested = if Self::needs_unnesting(&m.ty) {
Expand All @@ -351,11 +350,11 @@ impl Rasn {
} else {
Ok(None)
};
let extension_annotation = if i >= first_extension_index.unwrap_or(usize::MAX)
let extension_annotation = if extension_range.contains(&i)
&& m.name.starts_with(INTERNAL_EXTENSION_GROUP_NAME_PREFIX)
{
quote!(extension_addition_group)
} else if i >= first_extension_index.unwrap_or(usize::MAX) {
} else if extension_range.contains(&i) {
quote!(extension_addition)
} else {
TokenStream::new()
Expand Down Expand Up @@ -1096,7 +1095,7 @@ impl Rasn {

const REQUIRED_DERIVES: [&'static str; 6] =
["Debug", "AsnType", "Encode", "Decode", "PartialEq", "Clone"];
const COPY_DERIVE: &str = "Copy";
const COPY_DERIVE: &'static str = "Copy";
const RUST_KEYWORDS: [&'static str; 53] = [
"as",
"break",
Expand Down Expand Up @@ -1321,8 +1320,7 @@ impl ASN1Type {
.iter()
.fold(true, |acc, opt| opt.ty.is_const_type() && acc),
ASN1Type::Set(s) | ASN1Type::Sequence(s) => s
.members
.iter()
.direct_members()
.fold(true, |acc, m| m.ty.is_const_type() && acc),
ASN1Type::SetOf(s) | ASN1Type::SequenceOf(s) => s.element_type.is_const_type(),
_ => false,
Expand Down Expand Up @@ -1361,7 +1359,7 @@ mod tests {
types::{Boolean, Enumeral, Integer},
AsnTag,
};

use crate::prelude::ir::SequenceComponent;
use super::*;

#[test]
Expand Down Expand Up @@ -1430,11 +1428,10 @@ mod tests {
generator
.format_sequence_or_set_members(
&SequenceOrSet {
components_of: vec![],
extensible: Some(1),
extensible: true,
constraints: vec![],
members: vec![
SequenceOrSetMember {
fixed_components: vec![
SequenceComponent::Member(SequenceOrSetMember {
is_recursive: false,
name: "testMember0".into(),
tag: None,
Expand All @@ -1444,28 +1441,31 @@ mod tests {
default_value: None,
is_optional: true,
constraints: vec![]
},
SequenceOrSetMember {
})
],
extension_components: vec![
SequenceComponent::Member(SequenceOrSetMember {
is_recursive: false,
name: "testMember1".into(),
tag: None,
ty: ASN1Type::Integer(Integer {
distinguished_values: None,
constraints: vec![Constraint::SubtypeConstraint(ElementSet {
extensible: false,
set: crate::intermediate::constraints::ElementOrSetOperation::Element(
crate::intermediate::constraints::SubtypeElement::SingleValue {
value: ASN1Value::Integer(4),
extensible: true
}
)
})]
extensible: false,
set: crate::intermediate::constraints::ElementOrSetOperation::Element(
crate::intermediate::constraints::SubtypeElement::SingleValue {
value: ASN1Value::Integer(4),
extensible: true
}
)
})]
}),
default_value: Some(ASN1Value::Integer(4)),
is_optional: true,
constraints: vec![]
}
]
}),
],
suffix_components: vec![],
},
&"Parent".into(),
)
Expand Down
11 changes: 6 additions & 5 deletions rasn-compiler/src/generator/typescript/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use num::pow::Pow;

use crate::generator::error::GeneratorError;

use super::{
types::{BitString, Choice, SequenceOrSet},
ASN1Type, ASN1Value,
Expand Down Expand Up @@ -62,8 +61,7 @@ pub fn format_sequence_or_set_members(se: &SequenceOrSet) -> String {
r#"{{
{}{}
}}"#,
se.members
.iter()
se.direct_members()
.map(|m| format!(
r#"{}{}: {},"#,
to_jer_identifier(&m.name),
Expand All @@ -72,8 +70,11 @@ pub fn format_sequence_or_set_members(se: &SequenceOrSet) -> String {
))
.collect::<Vec<_>>()
.join("\n"),
se.extensible
.map_or(String::new(), |_| String::from("\n\t[key: string]: any"))
if se.extensible {
String::from("\n\t[key: string]: any")
} else {
String::new()
}
)
}

Expand Down
23 changes: 16 additions & 7 deletions rasn-compiler/src/intermediate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,13 +578,20 @@ impl ToplevelDefinition {
id: t.id,
});
match &mut ty.ty {
ASN1Type::Sequence(s) | ASN1Type::Set(s) => s.members.iter_mut().for_each(|m| {
m.tag = m.tag.as_ref().map(|t| AsnTag {
environment: env + &t.environment,
tag_class: t.tag_class,
id: t.id,
});
}),
ASN1Type::Sequence(s) | ASN1Type::Set(s) => {
for s in [&mut s.fixed_components, &mut s.suffix_components, &mut s.extension_components] {
s.iter_mut().for_each(|m| {
if let SequenceComponent::Member(m) = m {
m.tag = m.tag.as_ref().map(|t| AsnTag {
environment: env + &t.environment,
tag_class: t.tag_class,
id: t.id,
});
}
})

}
},
ASN1Type::Choice(c) => c.options.iter_mut().for_each(|o| {
o.tag = o.tag.as_ref().map(|t| AsnTag {
environment: env + &t.environment,
Expand Down Expand Up @@ -950,6 +957,7 @@ impl ASN1Type {
ASN1Type::SetOf(s) | ASN1Type::SequenceOf(s) => Some(s.constraints()),
ASN1Type::ElsewhereDeclaredType(e) => Some(e.constraints()),
ASN1Type::InformationObjectFieldReference(f) => Some(f.constraints()),
ASN1Type::ObjectIdentifier(o) => Some(&o.constraints),
_ => None,
}
}
Expand All @@ -969,6 +977,7 @@ impl ASN1Type {
ASN1Type::SetOf(s) | ASN1Type::SequenceOf(s) => Some(s.constraints_mut()),
ASN1Type::ElsewhereDeclaredType(e) => Some(e.constraints_mut()),
ASN1Type::InformationObjectFieldReference(f) => Some(f.constraints_mut()),
ASN1Type::ObjectIdentifier(o) => Some(&mut o.constraints),
_ => None,
}
}
Expand Down
91 changes: 61 additions & 30 deletions rasn-compiler/src/intermediate/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,24 +315,60 @@ impl From<(Option<Vec<Constraint>>, (Option<AsnTag>, ASN1Type))> for SequenceOrS
/// *As defined in Rec. ITU-T X.680 (02/2021) §25 and §27*
#[derive(Debug, Clone, PartialEq)]
pub struct SequenceOrSet {
pub components_of: Vec<String>,
pub extensible: Option<usize>,
pub fixed_components: Vec<SequenceComponent>,
pub extension_components: Vec<SequenceComponent>,
pub suffix_components: Vec<SequenceComponent>,
pub extensible: bool,
pub constraints: Vec<Constraint>,
pub members: Vec<SequenceOrSetMember>,
}

impl SequenceOrSet {
pub fn direct_members(&self) -> impl Iterator<Item=&SequenceOrSetMember> {
[&self.fixed_components, &self.extension_components, &self.suffix_components]
.into_iter()
.flat_map(|x| x)
.flat_map(|x| {
if let SequenceComponent::Member(m) = x {
Some(m)
} else {
None
}
})
}

pub fn direct_members_mut(&mut self) -> impl Iterator<Item=&mut SequenceOrSetMember> {
[&mut self.fixed_components, &mut self.extension_components, &mut self.suffix_components]
.into_iter()
.flat_map(|x| x)
.flat_map(|x| {
if let SequenceComponent::Member(m) = x {
Some(m)
} else {
None
}
})
}

pub fn extension_range(&self) -> std::ops::Range<usize> {
self.fixed_components.len() .. self.fixed_components.len() + self.extension_components.len()
}
}

impl IterNameTypes for SequenceOrSet {
fn iter_name_types(&self) -> impl Iterator<Item = (&str, &ASN1Type)> {
self.members.iter().map(|m| (m.name.as_str(), &m.ty))
self.direct_members()
.map(|m| (m.name.as_str(), &m.ty))
}
}

impl
From<(
(
Vec<SequenceComponent>,
Option<ExtensionMarker>,
Option<Vec<SequenceComponent>>,
Option<(
Vec<SequenceComponent>,
Vec<SequenceComponent>,
)>,
),
Option<Vec<Constraint>>,
)> for SequenceOrSet
Expand All @@ -341,58 +377,53 @@ impl
mut value: (
(
Vec<SequenceComponent>,
Option<ExtensionMarker>,
Option<Vec<SequenceComponent>>,
Option<(
Vec<SequenceComponent>,
Vec<SequenceComponent>,
)>,
),
Option<Vec<Constraint>>,
),
) -> Self {
let index_of_first_extension = value.0 .0.len();
value.0 .0.append(&mut value.0 .2.unwrap_or_default());
let mut components_of = vec![];
let mut members = vec![];
for comp in value.0 .0 {
match comp {
SequenceComponent::Member(m) => members.push(m),
SequenceComponent::ComponentsOf(c) => components_of.push(c),
}
}
let extensible = value.0 .1.is_some();
let (extension_components, suffix_components) = value.0 .1.unwrap_or_default();

SequenceOrSet {
components_of,
fixed_components: value.0 .0,
extension_components,
suffix_components,
constraints: value.1.unwrap_or_default(),
extensible: value.0 .1.map(|_| index_of_first_extension),
members,
extensible,
}
}
}

impl
From<(
(
Vec<SequenceOrSetMember>,
Vec<SequenceComponent>,
Option<ExtensionMarker>,
Option<Vec<SequenceOrSetMember>>,
Option<Vec<SequenceComponent>>,
),
Option<Vec<Constraint>>,
)> for SequenceOrSet
{
fn from(
mut value: (
(
Vec<SequenceOrSetMember>,
Vec<SequenceComponent>,
Option<ExtensionMarker>,
Option<Vec<SequenceOrSetMember>>,
Option<Vec<SequenceComponent>>,
),
Option<Vec<Constraint>>,
),
) -> Self {
let index_of_first_extension = value.0 .0.len();
value.0 .0.append(&mut value.0 .2.unwrap_or_default());
SequenceOrSet {
components_of: vec![],
fixed_components: value.0 .0.into_iter().collect(),
extension_components: value.0 .2.into_iter().flat_map(|x| x).collect(),
suffix_components: vec![],
constraints: value.1.unwrap_or_default(),
extensible: value.0 .1.map(|_| index_of_first_extension),
members: value.0 .0,
extensible: value.0 .1.is_some(),
}
}
}
Expand Down
Loading
Loading