pub trait FromBer<'a, E = Error>: Sized {
// Required method
fn from_ber(bytes: &'a [u8]) -> Result<(&'a [u8], Self), Err<E>>;
}
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§
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.
Implementations on Foreign Types§
Implementors§
impl<'a> FromBer<'a> for Any<'a>
impl<'a> FromBer<'a> for Header<'a>
impl<'a> FromBer<'a, LdapError> for Filter<'a>
impl<'a> FromBer<'a, LdapError> for AuthenticationChoice<'a>
impl<'a> FromBer<'a, LdapError> for Attribute<'a>
impl<'a> FromBer<'a, LdapError> for AttributeValueAssertion<'a>
impl<'a> FromBer<'a, LdapError> for PartialAttribute<'a>
impl<'a> FromBer<'a, LdapError> for AddRequest<'a>
impl<'a> FromBer<'a, LdapError> for BindRequest<'a>
impl<'a> FromBer<'a, LdapError> for BindResponse<'a>
impl<'a> FromBer<'a, LdapError> for Change<'a>
impl<'a> FromBer<'a, LdapError> for CompareRequest<'a>
impl<'a> FromBer<'a, LdapError> for Control<'a>
impl<'a> FromBer<'a, LdapError> for ExtendedRequest<'a>
impl<'a> FromBer<'a, LdapError> for ExtendedResponse<'a>
impl<'a> FromBer<'a, LdapError> for IntermediateResponse<'a>
impl<'a> FromBer<'a, LdapError> for LdapDN<'a>
impl<'a> FromBer<'a, LdapError> for LdapMessage<'a>
Parse a single LDAP message and return a structure borrowing fields from the input buffer
use ldap_parser::FromBer;
use ldap_parser::ldap::{LdapMessage, MessageID, ProtocolOp, ProtocolOpTag};
static DATA: &[u8] = include_bytes!("../assets/message-search-request-01.bin");
let res = LdapMessage::from_ber(DATA);
match res {
Ok((rem, msg)) => {
assert!(rem.is_empty());
//
assert_eq!(msg.message_id, MessageID(4));
assert_eq!(msg.protocol_op.tag(), ProtocolOpTag::SearchRequest);
match msg.protocol_op {
ProtocolOp::SearchRequest(req) => {
assert_eq!(req.base_object.0, "dc=rccad,dc=net");
},
_ => panic!("Unexpected message type"),
}
},
_ => panic!("LDAP parsing failed: {:?}", res),
}