Function rocket::mtls::x509::ber::parse_ber_set_defined_g

source ·
pub fn parse_ber_set_defined_g<'a, O, F, E>(f: F) -> impl FnMut(&'a [u8])
where F: FnMut(&'a [u8], Header<'a>) -> Result<(&'a [u8], O), Err<E>>, E: ParseError<&'a [u8]> + From<Error>,
Available on crate feature mtls only.
Expand description

Parse a defined SET object (generic version)

Given a parser for set content, apply it to build a DER set and return the remaining bytes and the built object.

The remaining bytes point after the set: any bytes that are part of the sequence but not parsed are ignored. The nom combinator all_consuming can be used to ensure all the content is parsed.

Unlike parse_ber_set_defined, this function allows returning any object or error type, and also passes the object header to the callback.

§Examples

Parsing a defined set with different types:

pub struct MyObject<'a> {
    a: u32,
    b: &'a [u8],
}

/// Read a DER-encoded object:
/// SET {
///     a INTEGER (0..4294967295),
///     b OCTETSTRING
/// }
fn parse_myobject(i: &[u8]) -> BerResult<MyObject> {
    parse_ber_set_defined_g(
        |i:&[u8], _| {
            let (i, a) = parse_ber_u32(i)?;
            let (i, obj) = parse_ber_octetstring(i)?;
            let b = obj.as_slice().unwrap();
            Ok((i, MyObject{ a, b }))
        }
    )(i)
}

let (rem, v) = parse_myobject(&bytes).expect("parsing failed");