Skip to content

Commit

Permalink
refactor: raname to Explicit struct
Browse files Browse the repository at this point in the history
  • Loading branch information
dishmaker committed Oct 6, 2024
1 parent 05e0133 commit bc9cb84
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 33 deletions.
5 changes: 3 additions & 2 deletions der/src/asn1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
mod internal_macros;

mod any;
mod application;
mod bit_string;
#[cfg(feature = "alloc")]
mod bmp_string;
Expand Down Expand Up @@ -37,7 +38,7 @@ pub use self::{
bit_string::{BitStringIter, BitStringRef},
choice::Choice,
//context_specific::{ContextSpecific, ContextSpecificRef},
context_specific::ContextSpecific,
context_specific::{ContextSpecificExplicit, ContextSpecificImplicit},
custom_class::{AnyCustomClassExplicit, AnyCustomClassImplicit},
generalized_time::GeneralizedTime,
ia5_string::Ia5StringRef,
Expand All @@ -46,7 +47,7 @@ pub use self::{
octet_string::OctetStringRef,
printable_string::PrintableStringRef,
//private::{Private, PrivateRef},
private::Private,
private::{PrivateExplicit, PrivateImplicit},
sequence::{Sequence, SequenceRef},
sequence_of::{SequenceOf, SequenceOfIter},
set_of::{SetOf, SetOfIter},
Expand Down
11 changes: 11 additions & 0 deletions der/src/asn1/application.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//! Application field.
use crate::tag::CLASS_APPLICATION;

use super::custom_class::{CustomClassExplicit, CustomClassImplicit};

/// Application class, EXPLICIT
pub type ApplicationExplicit<const TAG: u16, T> = CustomClassExplicit<T, TAG, CLASS_APPLICATION>;

/// Application class, IMPLICIT
pub type ApplicationImplicit<const TAG: u16, T> = CustomClassImplicit<T, TAG, CLASS_APPLICATION>;
20 changes: 11 additions & 9 deletions der/src/asn1/context_specific.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ use crate::{
use core::cmp::Ordering;

use super::custom_class::{
self, AnyCustomClassExplicit, AnyCustomClassImplicit, CustomClass, CustomClassImplicit,
self, AnyCustomClassExplicit, AnyCustomClassImplicit, CustomClassExplicit, CustomClassImplicit,
};

/// Context-specific class, EXPLICIT
pub type ContextSpecific<const TAG: u16, T> = CustomClass<T, TAG, CLASS_CONTEXT_SPECIFIC>;
pub type ContextSpecificExplicit<const TAG: u16, T> =
CustomClassExplicit<T, TAG, CLASS_CONTEXT_SPECIFIC>;

/// Context-specific class, IMPLICIT
pub type ContextSpecificImplicit<const TAG: u16, T> =
Expand Down Expand Up @@ -291,7 +292,7 @@ pub fn decode_explicit<'a, R: Reader<'a>, T: Decode<'a>>(
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::ContextSpecific;
use super::ContextSpecificExplicit;
use crate::{
asn1::{context_specific::ContextSpecificImplicit, BitStringRef},
Decode, Encode, SliceReader, TagMode, TagNumber,
Expand All @@ -304,7 +305,8 @@ mod tests {

#[test]
fn round_trip() {
let field = ContextSpecific::<1, BitStringRef<'_>>::from_der(EXAMPLE_BYTES).unwrap();
let field =
ContextSpecificExplicit::<1, BitStringRef<'_>>::from_der(EXAMPLE_BYTES).unwrap();
assert_eq!(
field.value,
BitStringRef::from_bytes(&EXAMPLE_BYTES[5..]).unwrap()
Expand All @@ -320,20 +322,20 @@ mod tests {
// Empty message
let mut reader = SliceReader::new(&[]).unwrap();
assert_eq!(
ContextSpecific::<0, u8>::decode_optional(&mut reader).unwrap(),
ContextSpecificExplicit::<0, u8>::decode_optional(&mut reader).unwrap(),
None
);

// Message containing a non-context-specific type
let mut reader = SliceReader::new(&hex!("020100")).unwrap();
assert_eq!(
ContextSpecific::<0, u8>::decode_optional(&mut reader).unwrap(),
ContextSpecificExplicit::<0, u8>::decode_optional(&mut reader).unwrap(),
None
);

// Message containing an EXPLICIT context-specific field
let mut reader = SliceReader::new(&hex!("A003020100")).unwrap();
let field = ContextSpecific::<0, u8>::decode_optional(&mut reader)
let field = ContextSpecificExplicit::<0, u8>::decode_optional(&mut reader)
.unwrap()
.unwrap();

Expand Down Expand Up @@ -365,7 +367,7 @@ mod tests {
#[test]
fn context_specific_skipping_unknown_field() {
let mut reader = SliceReader::new(&hex!("A003020100A103020101")).unwrap();
let field = ContextSpecific::<1, u8>::decode_optional(&mut reader)
let field = ContextSpecificExplicit::<1, u8>::decode_optional(&mut reader)
.unwrap()
.unwrap();
assert_eq!(field.value, 1);
Expand All @@ -375,7 +377,7 @@ mod tests {
fn context_specific_returns_none_on_greater_tag_number() {
let mut reader = SliceReader::new(&hex!("A103020101")).unwrap();
assert_eq!(
ContextSpecific::<0, u8>::decode_optional(&mut reader).unwrap(),
ContextSpecificExplicit::<0, u8>::decode_optional(&mut reader).unwrap(),
None
);
}
Expand Down
17 changes: 9 additions & 8 deletions der/src/asn1/custom_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
/// This type decodes/encodes a field which is specific to a particular context
/// and is identified by a [`TagNumber`].
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct CustomClass<T, const TAG: u16, const CLASS: u8> {
pub struct CustomClassExplicit<T, const TAG: u16, const CLASS: u8> {
/// Value of the field.
pub value: T,
}
Expand All @@ -28,7 +28,7 @@ pub struct CustomClassImplicit<T, const TAG: u16, const CLASS: u8> {
pub value: T,
}

impl<T, const TAG: u16, const CLASS: u8> CustomClass<T, TAG, CLASS> {
impl<T, const TAG: u16, const CLASS: u8> CustomClassExplicit<T, TAG, CLASS> {
/// Attempt to decode an `EXPLICIT` ASN.1 custom-tagged field with the
/// provided [`TagNumber`].
///
Expand Down Expand Up @@ -186,7 +186,7 @@ where
}
}

impl<'a, T, const TAG: u16, const CLASS: u8> Choice<'a> for CustomClass<T, TAG, CLASS>
impl<'a, T, const TAG: u16, const CLASS: u8> Choice<'a> for CustomClassExplicit<T, TAG, CLASS>
where
T: Decode<'a> + Tagged,
{
Expand All @@ -206,7 +206,7 @@ where
}
}

impl<'a, T, const TAG: u16, const CLASS: u8> Decode<'a> for CustomClass<T, TAG, CLASS>
impl<'a, T, const TAG: u16, const CLASS: u8> Decode<'a> for CustomClassExplicit<T, TAG, CLASS>
where
T: Decode<'a>,
{
Expand Down Expand Up @@ -243,7 +243,7 @@ where
}
}

impl<T, const TAG: u16, const CLASS: u8> EncodeValue for CustomClass<T, TAG, CLASS>
impl<T, const TAG: u16, const CLASS: u8> EncodeValue for CustomClassExplicit<T, TAG, CLASS>
where
T: EncodeValue + Tagged,
{
Expand Down Expand Up @@ -287,7 +287,7 @@ pub const fn expected_tag_constructed(class: Class, number: TagNumber, construct
}
}

impl<T, const TAG: u16, const CLASS: u8> Tagged for CustomClass<T, TAG, CLASS> {
impl<T, const TAG: u16, const CLASS: u8> Tagged for CustomClassExplicit<T, TAG, CLASS> {
fn tag(&self) -> Tag {
expected_tag_constructed(Class::from(CLASS), TagNumber(TAG), true)
}
Expand All @@ -303,7 +303,8 @@ where
}
}

impl<'a, T, const TAG: u16, const CLASS: u8> TryFrom<AnyRef<'a>> for CustomClass<T, TAG, CLASS>
impl<'a, T, const TAG: u16, const CLASS: u8> TryFrom<AnyRef<'a>>
for CustomClassExplicit<T, TAG, CLASS>
where
T: Decode<'a>,
{
Expand Down Expand Up @@ -353,7 +354,7 @@ where
}
}

impl<T, const TAG: u16, const CLASS: u8> ValueOrd for CustomClass<T, TAG, CLASS>
impl<T, const TAG: u16, const CLASS: u8> ValueOrd for CustomClassExplicit<T, TAG, CLASS>
where
T: DerOrd,
{
Expand Down
20 changes: 7 additions & 13 deletions der/src/asn1/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@ use crate::{
use crate::{Class, FixedTag};
use core::cmp::Ordering;

use super::custom_class::{CustomClass, CustomClassImplicit};

/// Application class, EXPLICIT
pub type Application<const TAG: u16, T> = CustomClass<T, TAG, CLASS_APPLICATION>;
use super::custom_class::{CustomClassExplicit, CustomClassImplicit};

/// Private class, EXPLICIT
pub type Private<const TAG: u16, T> = CustomClass<T, TAG, CLASS_PRIVATE>;

/// Application class, IMPLICIT
pub type ApplicationImplicit<const TAG: u16, T> = CustomClassImplicit<T, TAG, CLASS_APPLICATION>;
pub type PrivateExplicit<const TAG: u16, T> = CustomClassExplicit<T, TAG, CLASS_PRIVATE>;

/// Private class, IMPLICIT
pub type PrivateImplicit<const TAG: u16, T> = CustomClassImplicit<T, TAG, CLASS_PRIVATE>;
Expand Down Expand Up @@ -75,7 +69,7 @@ pub type PrivateImplicit<const TAG: u16, T> = CustomClassImplicit<T, TAG, CLASS_
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::Private;
use super::PrivateExplicit;
use super::PrivateImplicit;
use crate::{asn1::BitStringRef, Decode, Encode, SliceReader, TagMode, TagNumber};
use hex_literal::hex;
Expand All @@ -86,7 +80,7 @@ mod tests {

#[test]
fn round_trip() {
let field = Private::<1, BitStringRef<'_>>::from_der(EXAMPLE_BYTES).unwrap();
let field = PrivateExplicit::<1, BitStringRef<'_>>::from_der(EXAMPLE_BYTES).unwrap();
assert_eq!(
field.value,
BitStringRef::from_bytes(&EXAMPLE_BYTES[5..]).unwrap()
Expand Down Expand Up @@ -117,7 +111,7 @@ mod tests {

// Message containing an EXPLICIT private field
let mut reader = SliceReader::new(&hex!("C003020100")).unwrap();
let field = Private::<0, u8>::decode_optional(&mut reader)
let field = PrivateExplicit::<0, u8>::decode_optional(&mut reader)
.unwrap()
.unwrap();

Expand All @@ -141,15 +135,15 @@ mod tests {
#[test]
fn private_not_skipping_unknown_field() {
let mut reader = SliceReader::new(&hex!("E003020100E103020101")).unwrap();
let field = Private::<1, u8>::decode_optional(&mut reader).unwrap();
let field = PrivateExplicit::<1, u8>::decode_optional(&mut reader).unwrap();
assert_eq!(field, None);
}

#[test]
fn private_returns_none_on_unequal_tag_number() {
let mut reader = SliceReader::new(&hex!("C103020101")).unwrap();
assert_eq!(
Private::<0, u8>::decode_optional(&mut reader).unwrap(),
PrivateExplicit::<0, u8>::decode_optional(&mut reader).unwrap(),
None
);
}
Expand Down
2 changes: 1 addition & 1 deletion der/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub(crate) mod pem;
pub(crate) mod slice;

use crate::{
asn1::{AnyCustomClassExplicit, AnyCustomClassImplicit, ContextSpecific},
asn1::{AnyCustomClassExplicit, AnyCustomClassImplicit, ContextSpecificExplicit},
Class, Decode, DecodeValue, Encode, EncodingRules, Error, ErrorKind, FixedTag, Header, Length,
Tag, TagMode, TagNumber,
};
Expand Down

0 comments on commit bc9cb84

Please sign in to comment.