Function yasna::try_construct_der

source ·
pub fn try_construct_der<F, E>(callback: F) -> Result<Vec<u8>, E>where
    F: FnOnce(DERWriter<'_>) -> Result<(), E>,
Expand description

Tries to construct DER-encoded data as Vec<u8>.

Same as construct_der, only that it allows returning an error from the passed closure.

This function uses the loan pattern: callback is called back with a DERWriterSeq, to which the ASN.1 values are written.

Examples

use yasna;
let res_ok = yasna::try_construct_der::<_, ()>(|writer| {
    writer.write_sequence(|writer| {
        writer.next().write_i64(10);
        writer.next().write_bool(true);
    });
    Ok(())
});
let res_err = yasna::try_construct_der::<_, &str>(|writer| {
    writer.write_sequence(|writer| {
        writer.next().write_i64(10);
        writer.next().write_bool(true);
        return Err("some error here");
    })?;
    Ok(())
});
assert_eq!(res_ok, Ok(vec![48, 6, 2, 1, 10, 1, 1, 255]));
assert_eq!(res_err, Err("some error here"));