rocket::mtls::oid::asn1_rs

Trait FromBer

Source
pub trait FromBer<'a>: Sized {
    // Required method
    fn from_ber(bytes: &'a [u8]) -> Result<(&'a [u8], Self), Err<Error>>;
}
Available on crate feature mtls only.
Expand description

Base trait for BER object parsers

Library authors should usually not directly implement this trait, but should prefer implementing the TryFrom<Any> trait, which offers greater flexibility and provides an equivalent FromBer implementation for free.

§Examples

use asn1_rs::{Any, Result, Tag};
use std::convert::TryFrom;

// The type to be decoded
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MyType(pub u32);

impl<'a> TryFrom<Any<'a>> for MyType {
    type Error = asn1_rs::Error;

    fn try_from(any: Any<'a>) -> Result<MyType> {
        any.tag().assert_eq(Tag::Integer)?;
        // for this fictive example, the type contains the number of characters
        let n = any.data.len() as u32;
        Ok(MyType(n))
    }
}

// The above code provides a `FromBer` implementation for free.

// Example of parsing code:
use asn1_rs::FromBer;

let input = &[2, 1, 2];
// Objects can be parsed using `from_ber`, which returns the remaining bytes
// and the parsed object:
let (rem, my_type) = MyType::from_ber(input).expect("parsing failed");

Required Methods§

Source

fn from_ber(bytes: &'a [u8]) -> Result<(&'a [u8], Self), Err<Error>>

Attempt to parse input bytes into a BER object

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'a> FromBer<'a> for Header<'a>

Source§

impl<'a> FromBer<'a> for Any<'a>

Source§

impl<'a, T> FromBer<'a> for Option<T>
where T: FromBer<'a>,

Source§

impl<'a, T> FromBer<'a> for Vec<T>
where T: FromBer<'a>,

Source§

impl<'a, T> FromBer<'a> for TaggedParser<'a, Explicit, T>
where T: FromBer<'a>,

Source§

impl<'a, T> FromBer<'a> for TaggedParser<'a, Implicit, T>
where T: TryFrom<Any<'a>, Error = Error> + Tagged,

Source§

impl<'a, T> FromBer<'a> for T
where T: TryFrom<Any<'a>, Error = Error>,