Skip to main content

IsConstructed

Trait IsConstructed 

Source
pub trait IsConstructed {
    const CONSTRUCTED: bool;
}
Expand description

Types which have a constant ASN.1 constructed bit.

Auto-implemented on all types that implement FixedTag.

§Example

use der::{asn1::ContextSpecific, DecodeValue, ErrorKind, Header, IsConstructed, Length, Reader, Result, SliceReader, TagNumber};

/// Type, which can be decoded for example as `CONTEXT-SPECIFIC [0] (primitive)`
struct MyPrimitiveYear(u16);

impl IsConstructed for MyPrimitiveYear {
    const CONSTRUCTED: bool = false;
}

impl<'a> DecodeValue<'a> for MyPrimitiveYear {
    type Error = der::Error;

    fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
        let slice = reader.read_slice(Length::new(4))?;
        let year = std::str::from_utf8(slice).ok().and_then(|s| s.parse::<u16>().ok());
        if let Some(year) = year {
            Ok(Self(year))
        } else {
            Err(reader.error(ErrorKind::DateTime))
        }
    }
}

let mut reader = SliceReader::new(b"\x80\x041670".as_slice()).unwrap();

let decoded = ContextSpecific::<MyPrimitiveYear>::decode_implicit(&mut reader, TagNumber(0)).unwrap().unwrap();

assert_eq!(decoded.value.0, 1670);

Required Associated Constants§

Source

const CONSTRUCTED: bool

ASN.1 constructed bit

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T: FixedTag + ?Sized> IsConstructed for T

Types which are FixedTag always known if they are constructed (or primitive).