mod boolean;
mod integer;
mod null;
mod octet_string;
mod sequence;
mod utf8_string;
use crate::{ Asn1DerError, DerObject, Source, Sink, error::ErrorChain };
pub use crate::typed::{
boolean::Boolean, integer::Integer, null::Null,
octet_string::OctetString, sequence::Sequence, utf8_string::Utf8String
};
#[cfg(not(any(feature = "no_std", feature = "no_panic")))]
pub use sequence::SequenceVec;
pub trait DerTypeView<'a>: Sized {
const TAG: u8;
fn object(&self) -> DerObject<'a>;
}
pub trait DerDecodable<'a>: Sized {
fn load(object: DerObject<'a>) -> Result<Self, Asn1DerError>;
#[cfg_attr(feature = "no_panic", no_panic::no_panic)]
fn decode(raw: &'a[u8]) -> Result<Self, Asn1DerError> {
Self::decode_at(raw, 0)
}
#[cfg_attr(feature = "no_panic", no_panic::no_panic)]
fn decode_at(raw: &'a[u8], header_start: usize) -> Result<Self, Asn1DerError> {
let object = DerObject::decode_at(raw, header_start)
.propagate(e!("Failed to decode object"))?;
Self::load(object).propagate(e!("Failed to load object"))
}
#[cfg_attr(feature = "no_panic", no_panic::no_panic)]
fn decode_from_source<A: Source, B: Sink + Into<&'a[u8]>>(source: &mut A, sink: B)
-> Result<Self, Asn1DerError>
{
let object = DerObject::decode_from_source(source, sink)
.propagate(e!("Failed to decode object"))?;
Self::load(object).propagate(e!("Failed to load object"))
}
}
impl<'a> DerDecodable<'a> for DerObject<'a> {
#[cfg_attr(feature = "no_panic", no_panic::no_panic)]
fn load(object: DerObject<'a>) -> Result<Self, Asn1DerError> {
Ok(object)
}
}
pub trait DerEncodable: Sized {
fn encode<S: Sink>(&self, sink: &mut S) -> Result<(), Asn1DerError>;
#[cfg_attr(feature = "no_panic", no_panic::no_panic)]
fn der_object<'a, S: Sink + Into<&'a[u8]>>(&self, mut sink: S) -> Result<DerObject<'a>, Asn1DerError> {
self.encode(&mut sink).propagate(e!("Failed to encode object"))?;
DerObject::decode(sink.into()).propagate("Failed to load constructed object")
}
}
impl<'a> DerEncodable for DerObject<'a> {
#[cfg_attr(feature = "no_panic", no_panic::no_panic)]
fn encode<S: Sink>(&self, sink: &mut S) -> Result<(), Asn1DerError> {
self.encode(sink).propagate(e!("Failed to encode object"))
}
}
impl<T: DerEncodable> DerEncodable for &T {
#[cfg_attr(feature = "no_panic", no_panic::no_panic)]
fn encode<S: Sink>(&self, sink: &mut S) -> Result<(), Asn1DerError> {
(*self).encode(sink)
}
}
impl<T: DerEncodable> DerEncodable for &mut T {
#[cfg_attr(feature = "no_panic", no_panic::no_panic)]
fn encode<S: Sink>(&self, sink: &mut S) -> Result<(), Asn1DerError> {
(*self as &T).encode(sink)
}
}