synta 0.1.10

ASN.1 parser, decoder, and encoder library with DER/BER support and C FFI
Documentation
//! Decoding trait

use crate::der::decoder::Decoder;
use crate::error::Result;

/// Trait for types that can be decoded from ASN.1.
///
/// # Implementing this trait
///
/// An implementation **must** consume exactly the bytes that make up one
/// complete TLV (tag, length, value) triplet from the decoder on success.
/// The decoder cursor must be left pointing at the first byte after the
/// value, ready to read the next element.
///
/// On error, the decoder cursor position is unspecified.  Callers that need
/// to recover and continue decoding should save the position before calling
/// `decode` and restore it on failure (or use a peek-based approach).
///
/// The tag consumed by `decode` must match `<Self as Tagged>::tag()` when
/// the `Tagged` trait is also implemented.  The [`ImplicitTag`] wrapper
/// relies on this invariant to reconstruct the tag before calling the inner
/// `decode`.
///
/// [`ImplicitTag`]: crate::ImplicitTag
pub trait Decode<'a>: Sized {
    /// Decode one ASN.1 value from `decoder`.
    ///
    /// Reads exactly one TLV from the decoder and returns the decoded value.
    /// Returns an error if the data is malformed, the tag does not match, or
    /// the encoding rules are violated.
    fn decode(decoder: &mut Decoder<'a>) -> Result<Self>;
}