der/
value.rs

1//! Value traits
2
3use crate::{Decoder, Encoder, Header, Length, Result, Tagged};
4
5#[cfg(doc)]
6use crate::Tag;
7
8/// Decode the value part of a Tag-Length-Value encoded field, sans the [`Tag`]
9/// and [`Length`].
10pub trait DecodeValue<'a>: Sized {
11    /// Attempt to decode this message using the provided [`Decoder`].
12    fn decode_value(decoder: &mut Decoder<'a>, length: Length) -> Result<Self>;
13}
14
15/// Encode the value part of a Tag-Length-Value encoded field, sans the [`Tag`]
16/// and [`Length`].
17pub trait EncodeValue {
18    /// Get the [`Header`] used to encode this value.
19    fn header(&self) -> Result<Header>
20    where
21        Self: Tagged,
22    {
23        Header::new(self.tag(), self.value_len()?)
24    }
25
26    /// Compute the length of this value (sans [`Tag`]+[`Length`] header) when
27    /// encoded as ASN.1 DER.
28    fn value_len(&self) -> Result<Length>;
29
30    /// Encode value (sans [`Tag`]+[`Length`] header) as ASN.1 DER using the
31    /// provided [`Encoder`].
32    fn encode_value(&self, encoder: &mut Encoder<'_>) -> Result<()>;
33}