logo
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//! Trait definition for [`Decodable`].

use crate::{DecodeValue, Decoder, FixedTag, Header, Result};

/// Decoding trait.
///
/// This trait provides the core abstraction upon which all decoding operations
/// are based.
///
/// # Blanket impl for `TryFrom<Any>`
///
/// In almost all cases you do not need to impl this trait yourself, but rather
/// can instead impl `TryFrom<Any<'a>, Error = Error>` and receive a blanket
/// impl of this trait.
pub trait Decodable<'a>: Sized {
    /// Attempt to decode this message using the provided decoder.
    fn decode(decoder: &mut Decoder<'a>) -> Result<Self>;

    /// Parse `Self` from the provided DER-encoded byte slice.
    fn from_der(bytes: &'a [u8]) -> Result<Self> {
        let mut decoder = Decoder::new(bytes)?;
        let result = Self::decode(&mut decoder)?;
        decoder.finish(result)
    }
}

impl<'a, T> Decodable<'a> for T
where
    T: DecodeValue<'a> + FixedTag,
{
    fn decode(decoder: &mut Decoder<'a>) -> Result<T> {
        let header = Header::decode(decoder)?;
        header.tag.assert_eq(T::TAG)?;
        T::decode_value(decoder, header.length)
    }
}