Skip to content

Commit 3f1ad79

Browse files
authored
der: add predicate methods to EncodingRules (#1953)
Adds `is_ber` and `is_der` to simplify these predicates and avoid having to import `EncodingRules`
1 parent 92267d6 commit 3f1ad79

File tree

7 files changed

+24
-14
lines changed

7 files changed

+24
-14
lines changed

der/src/asn1/application.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Application class field.
22
33
use crate::{
4-
Choice, Class, Decode, DecodeValue, DerOrd, Encode, EncodeValue, EncodeValueRef, EncodingRules,
5-
Error, Header, Length, Reader, Tag, TagMode, TagNumber, Tagged, ValueOrd, Writer, asn1::AnyRef,
4+
Choice, Class, Decode, DecodeValue, DerOrd, Encode, EncodeValue, EncodeValueRef, Error, Header,
5+
Length, Reader, Tag, TagMode, TagNumber, Tagged, ValueOrd, Writer, asn1::AnyRef,
66
tag::IsConstructed,
77
};
88
use core::cmp::Ordering;

der/src/asn1/context_specific.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Context-specific field.
22
33
use crate::{
4-
Choice, Class, Decode, DecodeValue, DerOrd, Encode, EncodeValue, EncodeValueRef, EncodingRules,
5-
Error, Header, Length, Reader, Tag, TagMode, TagNumber, Tagged, ValueOrd, Writer, asn1::AnyRef,
4+
Choice, Class, Decode, DecodeValue, DerOrd, Encode, EncodeValue, EncodeValueRef, Error, Header,
5+
Length, Reader, Tag, TagMode, TagNumber, Tagged, ValueOrd, Writer, asn1::AnyRef,
66
tag::IsConstructed,
77
};
88
use core::cmp::Ordering;

der/src/asn1/internal_macros.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,13 @@ macro_rules! impl_custom_class {
143143
if !Tag::peek_matches(reader, Class::$class_enum_name, tag_number)? {
144144
return Ok(None);
145145
}
146+
146147
// Decode IMPLICIT header
147148
let header = Header::decode(reader)?;
148149

149150
// the encoding shall be constructed if the base encoding is constructed
150151
if header.tag.is_constructed() != T::CONSTRUCTED
151-
&& reader.encoding_rules() == EncodingRules::Der {
152+
&& reader.encoding_rules().is_der() {
152153
return Err(reader.error(header.tag.non_canonical_error()).into());
153154
}
154155

der/src/asn1/private.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Private class field.
22
33
use crate::{
4-
Choice, Class, Decode, DecodeValue, DerOrd, Encode, EncodeValue, EncodeValueRef, EncodingRules,
5-
Error, Header, Length, Reader, Tag, TagMode, TagNumber, Tagged, ValueOrd, Writer, asn1::AnyRef,
4+
Choice, Class, Decode, DecodeValue, DerOrd, Encode, EncodeValue, EncodeValueRef, Error, Header,
5+
Length, Reader, Tag, TagMode, TagNumber, Tagged, ValueOrd, Writer, asn1::AnyRef,
66
tag::IsConstructed,
77
};
88
use core::cmp::Ordering;

der/src/bytes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'a> arbitrary::Arbitrary<'a> for &'a BytesRef {
118118
pub(crate) mod allocating {
119119
use super::BytesRef;
120120
#[cfg(feature = "ber")]
121-
use crate::{EncodingRules, length::indefinite::read_constructed_vec};
121+
use crate::length::indefinite::read_constructed_vec;
122122

123123
use crate::{
124124
DecodeValue, DerOrd, EncodeValue, Error, Header, Length, Reader, Result, Tag, Writer,
@@ -156,7 +156,7 @@ pub(crate) mod allocating {
156156
) -> Result<Self> {
157157
// Reassemble indefinite length string types
158158
#[cfg(feature = "ber")]
159-
if reader.encoding_rules() == EncodingRules::Ber
159+
if reader.encoding_rules().is_ber()
160160
&& header.length.is_indefinite()
161161
&& !inner_tag.is_constructed()
162162
{

der/src/encoding_rules.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ pub enum EncodingRules {
2121
Der,
2222
}
2323

24+
impl EncodingRules {
25+
/// Are we using Basic Encoding Rules?
26+
#[cfg(feature = "ber")]
27+
pub const fn is_ber(self) -> bool {
28+
matches!(self, EncodingRules::Ber)
29+
}
30+
31+
/// Are we using Distinguished Encoding Rules?
32+
pub const fn is_der(self) -> bool {
33+
matches!(self, EncodingRules::Der)
34+
}
35+
}
36+
2437
impl FromStr for EncodingRules {
2538
type Err = Error;
2639

der/src/tag.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ mod number;
88
pub use self::{class::Class, mode::TagMode, number::TagNumber};
99

1010
use crate::{Decode, DerOrd, Encode, Error, ErrorKind, Length, Reader, Result, Writer};
11-
12-
#[cfg(feature = "ber")]
13-
use crate::EncodingRules;
14-
1511
use core::{cmp::Ordering, fmt};
1612

1713
/// Indicator bit for constructed form encoding (i.e. vs primitive form)
@@ -198,7 +194,7 @@ impl Tag {
198194
0x1B => Tag::GeneralString,
199195
0x1E => Tag::BmpString,
200196
#[cfg(feature = "ber")]
201-
0x24 if reader.encoding_rules() == EncodingRules::Ber => Tag::OctetString,
197+
0x24 if reader.encoding_rules().is_ber() => Tag::OctetString,
202198
0x30 => Tag::Sequence, // constructed
203199
0x31 => Tag::Set, // constructed
204200
0x40..=0x7F => {

0 commit comments

Comments
 (0)