rasn_compiler/intermediate/types.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
//! `types` contains representations for ASN.1's basic types, such as `BOOLEAN`s
//! or `SEQUENCE`s.
#[cfg(test)]
use internal_macros::EnumDebug;
use std::vec;
use super::{constraints::*, *};
/// Trait shared by ASN1 `SET`, `SEQUENCE`, AND `CHOICE` that allows iterating
/// over their field types.
pub trait IterNameTypes {
fn iter_name_types(&self) -> impl Iterator<Item = (&str, &ASN1Type)>;
}
/// Trait shared by all ASN1 types that can be constrained a.k.a subtyped.
/// *See also Rec. ITU-T X.680 (02/2021) §49 - §51*
pub trait Constrainable {
/// returns a reference to the type's constraints
fn constraints(&self) -> &Vec<Constraint>;
/// returns a mutable reference to the type's constraints
fn constraints_mut(&mut self) -> &mut Vec<Constraint>;
}
macro_rules! constrainable {
($typ:ty) => {
impl Constrainable for $typ {
fn constraints(&self) -> &Vec<Constraint> {
&self.constraints
}
fn constraints_mut(&mut self) -> &mut Vec<Constraint> {
&mut self.constraints
}
}
};
}
constrainable!(Boolean);
constrainable!(Integer);
constrainable!(BitString);
constrainable!(OctetString);
constrainable!(CharacterString);
constrainable!(Real);
constrainable!(SequenceOrSet);
constrainable!(SequenceOrSetOf);
constrainable!(Choice);
constrainable!(Enumerated);
constrainable!(DeclarationElsewhere);
constrainable!(InformationObjectFieldReference);
constrainable!(Time);
/// Representation of an ASN1 BOOLEAN data element
/// with corresponding constraints.
/// *As defined in Rec. ITU-T X.680 (02/2021) §18*
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Boolean {
pub constraints: Vec<Constraint>,
}
impl From<Option<Vec<Constraint>>> for Boolean {
fn from(value: Option<Vec<Constraint>>) -> Self {
Self {
constraints: value.unwrap_or_default(),
}
}
}
/// Representation of an ASN1 INTEGER data element
/// with corresponding constraints and distinguished values.
/// *As defined in Rec. ITU-T X.680 (02/2021) §19*
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Integer {
pub constraints: Vec<Constraint>,
pub distinguished_values: Option<Vec<DistinguishedValue>>,
}
impl Integer {
/// Returns the [IntegerType] of `self`.
/// The [IntegerType] describes the absolute range of an integer
pub fn int_type(&self) -> IntegerType {
self.constraints
.iter()
.fold(IntegerType::Unbounded, |acc, c| {
c.integer_constraints().max_restrictive(acc)
})
}
}
impl From<(i128, i128, bool)> for Integer {
fn from(value: (i128, i128, bool)) -> Self {
Self {
constraints: vec![Constraint::SubtypeConstraint(ElementSet {
set: ElementOrSetOperation::Element(SubtypeElement::ValueRange {
min: Some(ASN1Value::Integer(value.0)),
max: Some(ASN1Value::Integer(value.1)),
extensible: value.2,
}),
extensible: value.2,
})],
distinguished_values: None,
}
}
}
impl From<(Option<i128>, Option<i128>, bool)> for Integer {
fn from(value: (Option<i128>, Option<i128>, bool)) -> Self {
Self {
constraints: vec![Constraint::SubtypeConstraint(ElementSet {
set: ElementOrSetOperation::Element(SubtypeElement::ValueRange {
min: value.0.map(ASN1Value::Integer),
max: value.1.map(ASN1Value::Integer),
extensible: value.2,
}),
extensible: value.2,
})],
distinguished_values: None,
}
}
}
impl
From<(
&str,
Option<Vec<DistinguishedValue>>,
Option<Vec<Constraint>>,
)> for Integer
{
fn from(
value: (
&str,
Option<Vec<DistinguishedValue>>,
Option<Vec<Constraint>>,
),
) -> Self {
Self {
constraints: value.2.unwrap_or_default(),
distinguished_values: value.1,
}
}
}
/// Representation of an ASN1 REAL data element
/// with corresponding constraints.
/// *As defined in Rec. ITU-T X.680 (02/2021) §21*
#[derive(Debug, Clone, PartialEq)]
pub struct Real {
pub constraints: Vec<Constraint>,
}
impl From<Option<Vec<Constraint>>> for Real {
fn from(value: Option<Vec<Constraint>>) -> Self {
Self {
constraints: value.unwrap_or_default(),
}
}
}
/// Representation of an ASN1 GeneralizedTime data element
/// with corresponding constraints.
/// *As defined in Rec. ITU-T X.680 (02/2021) §46*
#[derive(Debug, Clone, PartialEq)]
pub struct GeneralizedTime {
pub constraints: Vec<Constraint>,
}
/// Representation of an ASN1 Universal time (a.k.a UTCTime)
/// data element with corresponding constraints.
/// *As defined in Rec. ITU-T X.680 (02/2021) §47*
#[derive(Debug, Clone, PartialEq)]
pub struct UTCTime {
pub constraints: Vec<Constraint>,
}
/// Representation of an ASN1 OCTET STRING data element
/// with corresponding constraints.
/// *As defined in Rec. ITU-T X.680 (02/2021) §23*
#[derive(Debug, Clone, PartialEq)]
pub struct OctetString {
pub constraints: Vec<Constraint>,
}
impl From<Option<Vec<Constraint>>> for OctetString {
fn from(value: Option<Vec<Constraint>>) -> Self {
OctetString {
constraints: value.unwrap_or_default(),
}
}
}
/// Representation of an ASN1 BIT STRING data element
/// with corresponding constraints and distinguished values
/// defining the individual bits.
/// *As defined in Rec. ITU-T X.680 (02/2021) §22*
#[derive(Debug, Clone, PartialEq)]
pub struct BitString {
pub constraints: Vec<Constraint>,
pub distinguished_values: Option<Vec<DistinguishedValue>>,
}
impl From<(Option<Vec<DistinguishedValue>>, Option<Vec<Constraint>>)> for BitString {
fn from(value: (Option<Vec<DistinguishedValue>>, Option<Vec<Constraint>>)) -> Self {
BitString {
constraints: value.1.unwrap_or_default(),
distinguished_values: value.0,
}
}
}
/// Representation of an ASN1 OBJECT IDENTIFIER data element
/// with corresponding constraints.
/// *As defined in Rec. ITU-T X.680 (02/2021) §32*
#[derive(Debug, Clone, PartialEq)]
pub struct ObjectIdentifier {
pub constraints: Vec<Constraint>,
}
impl From<Option<Vec<Constraint>>> for ObjectIdentifier {
fn from(value: Option<Vec<Constraint>>) -> Self {
ObjectIdentifier {
constraints: value.unwrap_or_default(),
}
}
}
/// Representation of an ASN1 TIME data element
/// with corresponding constraints.
/// *As defined in Rec. ITU-T X.680 (02/2021) §38*
#[derive(Debug, Clone, PartialEq)]
pub struct Time {
pub constraints: Vec<Constraint>,
}
impl From<Option<Vec<Constraint>>> for Time {
fn from(value: Option<Vec<Constraint>>) -> Self {
Time {
constraints: value.unwrap_or_default(),
}
}
}
/// Representation of an ASN1 Character String type data element
/// with corresponding constraints. ASN1 Character String types
/// include IA5String, UTF8String, VideotexString.
/// *As defined in Rec. ITU-T X.680 (02/2021) §39-*§44
#[derive(Debug, Clone, PartialEq)]
pub struct CharacterString {
pub constraints: Vec<Constraint>,
pub ty: CharacterStringType,
}
impl From<(&str, Option<Vec<Constraint>>)> for CharacterString {
fn from(value: (&str, Option<Vec<Constraint>>)) -> Self {
CharacterString {
constraints: value.1.unwrap_or_default(),
ty: value.0.into(),
}
}
}
/// Representation of an ASN1 SEQUENCE OF and SET OF data element
/// with corresponding constraints and element type info
/// Whether the struct describes a SEQUENCE OF or a SET OF
/// is identified by the `ASN1Type` enum variant that
/// holds this struct as a value (i.e. `ASN1Type::SetOf(SequenceOrSetOf { .. })`
/// or `ASN1Type::SequenceOf(SequenceOrSetOf { .. })`).
/// *As defined in Rec. ITU-T X.680 (02/2021) §26 and §28*
#[derive(Debug, Clone, PartialEq)]
pub struct SequenceOrSetOf {
pub constraints: Vec<Constraint>,
/// [ASN.1 type](ASN1Type) of the individual elements of the collection
/// ### Example
/// The ASN.1 type
/// ```ignore
/// Sequence-of-booleans ::= SEQUENCE OF BOOLEAN
/// ```
/// will have an `element_type` field of
/// ```
/// # use rasn_compiler::prelude::ir::*;
/// # let test =
/// Box::new(ASN1Type::Boolean(Boolean { constraints: vec![] } ))
/// # ;
/// ```
pub element_type: Box<ASN1Type>,
pub is_recursive: bool,
}
impl From<(Option<Vec<Constraint>>, ASN1Type)> for SequenceOrSetOf {
fn from(value: (Option<Vec<Constraint>>, ASN1Type)) -> Self {
Self {
constraints: value.0.unwrap_or_default(),
element_type: Box::new(value.1),
is_recursive: false,
}
}
}
/// Representation of an ASN1 SEQUENCE or SET data element
/// with corresponding members and extension information.
/// Whether the struct describes a SEQUENCE or a SET
/// is identified by the `ASN1Type` enum variant that
/// holds this struct as a value (i.e. `ASN1Type::Set(SequenceOrSet { .. })`
/// or `ASN1Type::Sequence(SequenceOrSet { .. })`).
/// *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 constraints: Vec<Constraint>,
pub members: Vec<SequenceOrSetMember>,
}
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))
}
}
impl
From<(
(
Vec<SequenceComponent>,
Option<ExtensionMarker>,
Option<Vec<SequenceComponent>>,
),
Option<Vec<Constraint>>,
)> for SequenceOrSet
{
fn from(
mut value: (
(
Vec<SequenceComponent>,
Option<ExtensionMarker>,
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());
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),
}
}
SequenceOrSet {
components_of,
constraints: value.1.unwrap_or_default(),
extensible: value.0 .1.map(|_| index_of_first_extension),
members,
}
}
}
impl
From<(
(
Vec<SequenceOrSetMember>,
Option<ExtensionMarker>,
Option<Vec<SequenceOrSetMember>>,
),
Option<Vec<Constraint>>,
)> for SequenceOrSet
{
fn from(
mut value: (
(
Vec<SequenceOrSetMember>,
Option<ExtensionMarker>,
Option<Vec<SequenceOrSetMember>>,
),
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![],
constraints: value.1.unwrap_or_default(),
extensible: value.0 .1.map(|_| index_of_first_extension),
members: value.0 .0,
}
}
}
/// Intermediate parsing type to parse COMPONENTS OF notation.
/// `SequenceComponent` is an intermediary type that implementors of
/// a [Backend] will usually not interact with.
/// When parsing the body of an ASN.1 SEQUENCE or SET, the lexer
/// distinguishes between a group of members (`SequenceComponent::ComponentsOf`) that is imnported from
/// another ASN.1 data element using the `COMPONENTS OF` notation
/// (i.e. `Extending-Sequence ::= SEQUENCE { COMPONENTS OF Another-Sequence, added-field BOOLEAN }`)
/// and the regular member declaration (`SequenceComponent::Member`)
/// (i.e. `Simple-Sequence ::= SEQUENCE { field BOOLEAN }`).
/// When the lexer assembles the complete [SequenceOrSet] struct,
/// it groups the parsed `SequenceComponent` items into the `members`
/// and `components_of` fields of the [SequenceOrSet] struct. The linker
/// will subsequently try to resolve the `components_of` identifiers.
#[cfg_attr(test, derive(EnumDebug))]
#[cfg_attr(not(test), derive(Debug))]
#[derive(Clone, PartialEq)]
pub enum SequenceComponent {
Member(SequenceOrSetMember),
ComponentsOf(String),
}
/// Representation of an single ASN1 SEQUENCE or SET member.
/// ### Example
/// The ASN.1 SEQUENCE defined as
/// ```ignore
/// Test-Sequence ::= SEQUENCE {
/// int-member [0] INTEGER (0..2) DEFAULT 1
/// }
/// ```
/// defines one member, which is representated as follows
/// ```
/// # use rasn_compiler::prelude::ir::*;
/// # let test =
/// SequenceOrSetMember {
/// is_recursive: false,
/// name: String::from("int-member"),
/// tag: Some(AsnTag {
/// environment: TaggingEnvironment::Automatic,
/// tag_class: TagClass::ContextSpecific,
/// id: 0,
/// }),
/// ty: ASN1Type::Integer(Integer {
/// constraints: vec![
/// Constraint::SubtypeConstraint(ElementSet {
/// set: ElementOrSetOperation::Element(SubtypeElement::ValueRange {
/// min: Some(ASN1Value::Integer(0)),
/// max: Some(ASN1Value::Integer(2)),
/// extensible: false
/// }),
/// extensible: false
/// })
/// ],
/// distinguished_values: None,
/// }),
/// default_value: Some(ASN1Value::Integer(1)),
/// is_optional: true,
/// constraints: vec![]
/// }
/// # ;
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct SequenceOrSetMember {
pub name: String,
pub tag: Option<AsnTag>,
pub ty: ASN1Type,
pub default_value: Option<ASN1Value>,
pub is_optional: bool,
pub is_recursive: bool,
pub constraints: Vec<Constraint>,
}
impl
From<(
&str,
Option<AsnTag>,
ASN1Type,
Option<Vec<Constraint>>,
Option<OptionalMarker>,
Option<ASN1Value>,
)> for SequenceOrSetMember
{
fn from(
value: (
&str,
Option<AsnTag>,
ASN1Type,
Option<Vec<Constraint>>,
Option<OptionalMarker>,
Option<ASN1Value>,
),
) -> Self {
SequenceOrSetMember {
name: value.0.into(),
tag: value.1,
ty: value.2,
is_optional: value.4.is_some() || value.5.is_some(),
default_value: value.5,
is_recursive: false,
constraints: value.3.unwrap_or_default(),
}
}
}
/// Representation of an ASN1 CHOICE data element
/// with corresponding members and extension information.
/// *As defined in Rec. ITU-T X.680 (02/2021) §29*
#[derive(Debug, Clone, PartialEq)]
pub struct Choice {
pub extensible: Option<usize>,
pub options: Vec<ChoiceOption>,
pub constraints: Vec<Constraint>,
}
impl IterNameTypes for Choice {
fn iter_name_types(&self) -> impl Iterator<Item = (&str, &ASN1Type)> {
self.options.iter().map(|o| (o.name.as_str(), &o.ty))
}
}
impl
From<(
Vec<ChoiceOption>,
Option<ExtensionMarker>,
Option<Vec<ChoiceOption>>,
)> for Choice
{
fn from(
mut value: (
Vec<ChoiceOption>,
Option<ExtensionMarker>,
Option<Vec<ChoiceOption>>,
),
) -> Self {
let index_of_first_extension = value.0.len();
value.0.append(&mut value.2.unwrap_or_default());
Choice {
extensible: value.1.map(|_| index_of_first_extension),
options: value.0,
constraints: vec![],
}
}
}
/// Representation of an single ASN1 CHOICE option.
/// ### Example
/// The ASN.1 CHOICE defined as
/// ```ignore
/// Test-Choice ::= CHOICE {
/// boolean-option [0] BOOLEAN
/// }
/// ```
/// defines one option, which is representated as follows
/// ```
/// # use rasn_compiler::prelude::ir::*;
/// # let test =
/// ChoiceOption {
/// name: String::from("boolean-option"),
/// is_recursive: false,
/// tag: Some(AsnTag {
/// environment: TaggingEnvironment::Automatic,
/// tag_class: TagClass::ContextSpecific,
/// id: 0,
/// }),
/// ty: ASN1Type::Boolean(Boolean {
/// constraints: vec![]
/// }),
/// constraints: vec![]
/// }
/// # ;
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct ChoiceOption {
pub name: String,
pub tag: Option<AsnTag>,
pub ty: ASN1Type,
pub constraints: Vec<Constraint>,
pub is_recursive: bool,
}
impl From<(&str, Option<AsnTag>, ASN1Type, Option<Vec<Constraint>>)> for ChoiceOption {
fn from(value: (&str, Option<AsnTag>, ASN1Type, Option<Vec<Constraint>>)) -> Self {
ChoiceOption {
name: value.0.into(),
tag: value.1,
ty: value.2,
constraints: value.3.unwrap_or_default(),
is_recursive: false,
}
}
}
/// Representation of an ASN1 ENUMERATED data element
/// with corresponding enumerals and extension information.
/// *As defined in Rec. ITU-T X.680 (02/2021) §20*
#[derive(Debug, Clone, PartialEq)]
pub struct Enumerated {
pub members: Vec<Enumeral>,
pub extensible: Option<usize>,
pub constraints: Vec<Constraint>,
}
impl
From<(
Vec<Enumeral>,
Option<ExtensionMarker>,
Option<Vec<Enumeral>>,
)> for Enumerated
{
fn from(
mut value: (
Vec<Enumeral>,
Option<ExtensionMarker>,
Option<Vec<Enumeral>>,
),
) -> Self {
let index_of_first_extension = value.0.len();
value.0.append(&mut value.2.unwrap_or_default());
Enumerated {
members: value.0,
extensible: value.1.map(|_| index_of_first_extension),
constraints: vec![],
}
}
}
/// Representation of a single member/enumeral of an ASN1
/// ENUMERATED data element.
/// ### Example
/// The ASN.1 ENUMERATED defined as
/// ```ignore
/// Test-Enum ::= ENUMERATED {
/// first-item(7) -- This is the first item of Test-Enum
/// }
/// ```
/// defines one option, which is representated as follows
/// ```
/// # use rasn_compiler::prelude::ir::*;
/// # let test =
/// Enumeral {
/// name: String::from("first-item"),
/// description: Some(String::from(" This is the first item of Test-Enum")),
/// index: 7
/// }
/// # ;
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Enumeral {
pub name: String,
pub description: Option<String>,
pub index: i128,
}
/// Representation of a ASN1 distinguished value,
/// as seen in some INTEGER and BIT STRING declarations
/// *As defined in Rec. ITU-T X.680 (02/2021) §19.5 and §22.4*
#[derive(Debug, Clone, PartialEq)]
pub struct DistinguishedValue {
pub name: String,
pub value: i128,
}
impl From<(&str, i128)> for DistinguishedValue {
fn from(value: (&str, i128)) -> Self {
Self {
name: value.0.into(),
value: value.1,
}
}
}
/// Representation of a ASN1 selection type as used with ASN1 CHOICEs
/// *As defined in Rec. ITU-T X.680 (02/2021) §30*
#[derive(Debug, Clone, PartialEq)]
pub struct ChoiceSelectionType {
pub choice_name: String,
pub selected_option: String,
}
impl From<(&str, &str)> for ChoiceSelectionType {
fn from(value: (&str, &str)) -> Self {
Self {
choice_name: value.1.into(),
selected_option: value.0.into(),
}
}
}