#[cfg(test)]
use internal_macros::EnumDebug;
use std::vec;
#[cfg(doc)]
use crate::Backend;
use super::{constraints::*, *};
#[derive(Debug, Clone, PartialEq)]
pub enum Optionality<T> {
Required,
Optional,
Default(T),
}
impl<T> Optionality<T> {
pub fn default(&self) -> Option<&T> {
match self {
Optionality::Required | Optionality::Optional => None,
Optionality::Default(d) => Some(d),
}
}
pub fn default_mut(&mut self) -> Option<&mut T> {
match self {
Optionality::Required | Optionality::Optional => None,
Optionality::Default(d) => Some(d),
}
}
}
pub trait IterNameTypes {
fn iter_name_types(&self) -> impl Iterator<Item = (&str, &ASN1Type)>;
}
pub trait MemberOrOption {
const IS_CHOICE_OPTION: bool;
fn name(&self) -> &str;
fn ty(&self) -> &ASN1Type;
fn constraints(&self) -> &[Constraint];
fn is_recursive(&self) -> bool;
fn tag(&self) -> Option<&AsnTag>;
}
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!(ObjectClassFieldType);
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::Subtype(ElementSetSpecs {
set: ElementOrSetOperation::Element(SubtypeElements::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::Subtype(ElementSetSpecs {
set: ElementOrSetOperation::Element(SubtypeElements::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,
}
#[derive(Debug, Clone, PartialEq)]
pub struct SequenceOrSetOf {
pub constraints: Vec<Constraint>,
pub element_type: Box<ASN1Type>,
pub element_tag: Option<AsnTag>,
pub is_recursive: bool,
}
impl From<(Option<Vec<Constraint>>, (Option<AsnTag>, ASN1Type))> for SequenceOrSetOf {
fn from(value: (Option<Vec<Constraint>>, (Option<AsnTag>, ASN1Type))) -> Self {
Self {
constraints: value.0.unwrap_or_default(),
element_type: Box::new(value.1 .1),
element_tag: value.1 .0,
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 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,
}
}
}
#[cfg_attr(test, derive(EnumDebug))]
#[cfg_attr(not(test), derive(Debug))]
#[allow(clippy::large_enum_variant)]
#[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 optionality: Optionality<ASN1Value>,
pub is_recursive: bool,
pub constraints: Vec<Constraint>,
}
impl MemberOrOption for SequenceOrSetMember {
fn name(&self) -> &str {
&self.name
}
fn ty(&self) -> &ASN1Type {
&self.ty
}
fn constraints(&self) -> &[Constraint] {
&self.constraints
}
fn is_recursive(&self) -> bool {
self.is_recursive
}
fn tag(&self) -> Option<&AsnTag> {
self.tag.as_ref()
}
const IS_CHOICE_OPTION: bool = false;
}
impl
From<(
&str,
Option<AsnTag>,
ASN1Type,
Option<Vec<Constraint>>,
Optionality<ASN1Value>,
)> for SequenceOrSetMember
{
fn from(
value: (
&str,
Option<AsnTag>,
ASN1Type,
Option<Vec<Constraint>>,
Optionality<ASN1Value>,
),
) -> Self {
SequenceOrSetMember {
name: value.0.into(),
tag: value.1,
ty: value.2,
optionality: value.4,
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 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![],
}
}
}
#[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 MemberOrOption for ChoiceOption {
fn name(&self) -> &str {
&self.name
}
fn ty(&self) -> &ASN1Type {
&self.ty
}
fn constraints(&self) -> &[Constraint] {
&self.constraints
}
fn is_recursive(&self) -> bool {
self.is_recursive
}
fn tag(&self) -> Option<&AsnTag> {
self.tag.as_ref()
}
const IS_CHOICE_OPTION: bool = true;
}
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(),
}
}
}