Skip to content

Commit ccb9961

Browse files
committed
der: add IsConstructed trait, impl'ed on any FixedTag
1 parent fe59d44 commit ccb9961

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

der/src/asn1/context_specific.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::{
44
Choice, Decode, DecodeValue, DerOrd, Encode, EncodeValue, EncodeValueRef, Error, Header,
55
Length, Reader, Tag, TagMode, TagNumber, Tagged, ValueOrd, Writer, asn1::AnyRef,
6+
tag::IsConstructed,
67
};
78
use core::cmp::Ordering;
89

@@ -60,13 +61,13 @@ impl<T> ContextSpecific<T> {
6061
tag_number: TagNumber,
6162
) -> Result<Option<Self>, T::Error>
6263
where
63-
T: DecodeValue<'a> + Tagged,
64+
T: DecodeValue<'a> + IsConstructed,
6465
{
6566
Self::decode_with::<_, _, T::Error>(reader, tag_number, |reader| {
6667
let header = Header::decode(reader)?;
6768
let value = T::decode_value(reader, header)?;
6869

69-
if header.tag.is_constructed() != value.tag().is_constructed() {
70+
if header.tag.is_constructed() != T::CONSTRUCTED {
7071
return Err(header.tag.non_canonical_error().into());
7172
}
7273

der/src/tag.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ impl<T: FixedTag + ?Sized> Tagged for T {
3232
}
3333
}
3434

35+
/// Types which have a constant ASN.1 constructed bit.
36+
pub trait IsConstructed {
37+
/// ASN.1 constructed bit
38+
const CONSTRUCTED: bool;
39+
}
40+
41+
/// Types which are [`FixedTag`] always known if they are constructed (or primitive).
42+
impl<T: FixedTag + ?Sized> IsConstructed for T {
43+
const CONSTRUCTED: bool = T::TAG.is_constructed();
44+
}
45+
3546
/// ASN.1 tags.
3647
///
3748
/// Tags are the leading identifier octet of the Tag-Length-Value encoding
@@ -229,7 +240,7 @@ impl Tag {
229240
}
230241

231242
/// Does this tag represent a constructed (as opposed to primitive) field?
232-
pub fn is_constructed(self) -> bool {
243+
pub const fn is_constructed(self) -> bool {
233244
match self {
234245
Tag::Sequence | Tag::Set => true,
235246
Tag::Application { constructed, .. }

0 commit comments

Comments
 (0)