Macro der_parser::parse_der_optional[][src]

macro_rules! parse_der_optional {
    ($i : expr, $f : ident) => { ... };
}
Expand description

Parse an optional DER element

Try to parse an optional DER element, and return it as a ContextSpecific item with tag 0. If the parsing failed, the ContextSpecific object has value None.

let empty = &b""[..];
let bytes1 = [ 0x30, 0x0a,
               0x0a, 0x03, 0x00, 0x00, 0x01,
               0x02, 0x03, 0x01, 0x00, 0x01];
let bytes2 = [ 0x30, 0x05,
               0x02, 0x03, 0x01, 0x00, 0x01];
let expected1 = BerObject::from_seq(vec![
    BerObject::from_obj(BerObjectContent::Optional(Some(Box::new(
        BerObject::from_obj(BerObjectContent::Enum(1)),
    )))),
    BerObject::from_int_slice(b"\x01\x00\x01"),
]);
let expected2  = BerObject::from_seq(vec![
    BerObject::from_obj(
        BerObjectContent::Optional(None),
    ),
    BerObject::from_int_slice(b"\x01\x00\x01"),
]);

fn parse_optional_enum(i:&[u8]) -> BerResult {
    parse_der_optional!(i, parse_ber_enum)
}
fn parser(i:&[u8]) -> BerResult {
    parse_der_sequence_defined!(i,
        parse_optional_enum >>
        parse_ber_integer
    )
}

assert_eq!(parser(&bytes1), Ok((empty, expected1)));
assert_eq!(parser(&bytes2), Ok((empty, expected2)));