#[cfg(test)]
use internal_macros::EnumDebug;
use std::vec;
use super::{constraints::*, *};
pub trait Constrainable {
fn constraints(&self) -> &Vec<Constraint>;
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);
#[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(),
}
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Integer {
pub constraints: Vec<Constraint>,
pub distinguished_values: Option<Vec<DistinguishedValue>>,
}
impl 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,
}
}
}
#[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(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct GeneralizedTime {
pub constraints: Vec<Constraint>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct UTCTime {
pub constraints: Vec<Constraint>,
}
#[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(),
}
}
}
#[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,
}
}
}
#[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(),
}
}
}
#[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(),
}
}
}
#[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(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct SequenceOrSetOf {
pub constraints: Vec<Constraint>,
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,
}
}
}
#[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
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,
}
}
}
#[cfg_attr(test, derive(EnumDebug))]
#[cfg_attr(not(test), derive(Debug))]
#[derive(Clone, PartialEq)]
pub enum SequenceComponent {
Member(SequenceOrSetMember),
ComponentsOf(String),
}
#[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(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Choice {
pub extensible: Option<usize>,
pub options: Vec<ChoiceOption>,
pub constraints: Vec<Constraint>,
}
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![],
}
}
}
#[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,
}
}
}
#[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![],
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Enumeral {
pub name: String,
pub description: Option<String>,
pub index: i128,
}
#[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,
}
}
}
#[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(),
}
}
}