Skip to content

Commit 0c0a4aa

Browse files
committed
Move TaggedParser methods from_ber_and_then (and _der) to Any
1 parent 4689ebe commit 0c0a4aa

File tree

4 files changed

+90
-53
lines changed

4 files changed

+90
-53
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ It also adds a lot of tests to improve code coverage.
1616
asn1-rs:
1717

1818
- Add helper types for Application/Private tagged values
19-
- TaggedParser: remove constraints `from_ber_and_then` (and `_der`)
19+
- Any: add methods `from_ber_and_then` (and `_der`)
20+
- TaggedParser: add documentation for `from_ber_and_then` (and `_der`)
2021
- Oid: add method `starts_with`
2122
- Fix documentation of application and private tagged helpers
2223
- Fix clippy warnings

src/asn1_types/any.rs

+48
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,54 @@ impl<'a> Any<'a> {
8585
T::from_ber(self.data)
8686
}
8787

88+
/// Parse a BER value and apply the provided parsing function to content
89+
///
90+
/// After parsing, the sequence object and header are discarded.
91+
pub fn from_ber_and_then<F, T, E>(
92+
class: Class,
93+
tag: u32,
94+
bytes: &'a [u8],
95+
op: F,
96+
) -> ParseResult<'a, T, E>
97+
where
98+
F: FnOnce(&'a [u8]) -> ParseResult<T, E>,
99+
E: From<Error>,
100+
{
101+
let (rem, any) = Any::from_ber(bytes).map_err(Err::convert)?;
102+
any.tag()
103+
.assert_eq(Tag(tag))
104+
.map_err(|e| nom::Err::Error(e.into()))?;
105+
any.class()
106+
.assert_eq(class)
107+
.map_err(|e| nom::Err::Error(e.into()))?;
108+
let (_, res) = op(any.data)?;
109+
Ok((rem, res))
110+
}
111+
112+
/// Parse a DER value and apply the provided parsing function to content
113+
///
114+
/// After parsing, the sequence object and header are discarded.
115+
pub fn from_der_and_then<F, T, E>(
116+
class: Class,
117+
tag: u32,
118+
bytes: &'a [u8],
119+
op: F,
120+
) -> ParseResult<'a, T, E>
121+
where
122+
F: FnOnce(&'a [u8]) -> ParseResult<T, E>,
123+
E: From<Error>,
124+
{
125+
let (rem, any) = Any::from_der(bytes).map_err(Err::convert)?;
126+
any.tag()
127+
.assert_eq(Tag(tag))
128+
.map_err(|e| nom::Err::Error(e.into()))?;
129+
any.class()
130+
.assert_eq(class)
131+
.map_err(|e| nom::Err::Error(e.into()))?;
132+
let (_, res) = op(any.data)?;
133+
Ok((rem, res))
134+
}
135+
88136
#[inline]
89137
pub fn parse_der<T>(&'a self) -> ParseResult<'a, T>
90138
where

src/asn1_types/tagged/explicit.rs

+40
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,46 @@ impl<'a, T, E> TaggedParser<'a, Explicit, T, E> {
127127
_e: PhantomData,
128128
}
129129
}
130+
131+
/// Parse a BER tagged value and apply the provided parsing function to content
132+
///
133+
/// After parsing, the sequence object and header are discarded.
134+
///
135+
/// Note: this function is provided for `Explicit`, but there is not difference between
136+
/// explicit or implicit tags. The `op` function is responsible of handling the content.
137+
#[inline]
138+
pub fn from_ber_and_then<F>(
139+
class: Class,
140+
tag: u32,
141+
bytes: &'a [u8],
142+
op: F,
143+
) -> ParseResult<'a, T, E>
144+
where
145+
F: FnOnce(&'a [u8]) -> ParseResult<T, E>,
146+
E: From<Error>,
147+
{
148+
Any::from_ber_and_then(class, tag, bytes, op)
149+
}
150+
151+
/// Parse a DER tagged value and apply the provided parsing function to content
152+
///
153+
/// After parsing, the sequence object and header are discarded.
154+
///
155+
/// Note: this function is provided for `Explicit`, but there is not difference between
156+
/// explicit or implicit tags. The `op` function is responsible of handling the content.
157+
#[inline]
158+
pub fn from_der_and_then<F>(
159+
class: Class,
160+
tag: u32,
161+
bytes: &'a [u8],
162+
op: F,
163+
) -> ParseResult<'a, T, E>
164+
where
165+
F: FnOnce(&'a [u8]) -> ParseResult<T, E>,
166+
E: From<Error>,
167+
{
168+
Any::from_der_and_then(class, tag, bytes, op)
169+
}
130170
}
131171

132172
impl<'a, T, E> FromBer<'a, E> for TaggedParser<'a, Explicit, T, E>

src/asn1_types/tagged/parser.rs

-52
Original file line numberDiff line numberDiff line change
@@ -37,58 +37,6 @@ impl<'a, TagKind, T, E> TaggedParser<'a, TagKind, T, E> {
3737
pub const fn tag(&self) -> Tag {
3838
self.header.tag
3939
}
40-
41-
/// Parse a BER tagged value and apply the provided parsing function to content
42-
///
43-
/// After parsing, the sequence object and header are discarded.
44-
///
45-
/// Note: there is not difference between explicit or implicit tags.
46-
pub fn from_ber_and_then<F>(
47-
class: Class,
48-
tag: u32,
49-
bytes: &'a [u8],
50-
op: F,
51-
) -> ParseResult<'a, T, E>
52-
where
53-
F: FnOnce(&'a [u8]) -> ParseResult<T, E>,
54-
E: From<Error>,
55-
{
56-
let (rem, any) = Any::from_ber(bytes).map_err(Err::convert)?;
57-
any.tag()
58-
.assert_eq(Tag(tag))
59-
.map_err(|e| nom::Err::Error(e.into()))?;
60-
any.class()
61-
.assert_eq(class)
62-
.map_err(|e| nom::Err::Error(e.into()))?;
63-
let (_, res) = op(any.data)?;
64-
Ok((rem, res))
65-
}
66-
67-
/// Parse a DER tagged value and apply the provided parsing function to content
68-
///
69-
/// After parsing, the sequence object and header are discarded.
70-
///
71-
/// Note: there is not difference between explicit or implicit tags.
72-
pub fn from_der_and_then<F>(
73-
class: Class,
74-
tag: u32,
75-
bytes: &'a [u8],
76-
op: F,
77-
) -> ParseResult<'a, T, E>
78-
where
79-
F: FnOnce(&'a [u8]) -> ParseResult<T, E>,
80-
E: From<Error>,
81-
{
82-
let (rem, any) = Any::from_der(bytes).map_err(Err::convert)?;
83-
any.tag()
84-
.assert_eq(Tag(tag))
85-
.map_err(|e| nom::Err::Error(e.into()))?;
86-
any.class()
87-
.assert_eq(class)
88-
.map_err(|e| nom::Err::Error(e.into()))?;
89-
let (_, res) = op(any.data)?;
90-
Ok((rem, res))
91-
}
9240
}
9341

9442
impl<'a, TagKind, T, E> AsRef<T> for TaggedParser<'a, TagKind, T, E> {

0 commit comments

Comments
 (0)