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
37
38
39
40
41
42
43
44
45
46
47
48
use super::{AsnType, Explicit, ObjectIdentifier};
use crate::tag::{Class, Tag};

/// An instance of a defined object class.
#[derive(Debug, Clone, PartialEq)]
pub struct InstanceOf<T> {
    /// The OID identifying T's real type.
    pub type_id: ObjectIdentifier,
    /// The value identified by `type_id`.
    pub value: T,
}

impl<T> AsnType for InstanceOf<T> {
    const TAG: Tag = Tag::EXTERNAL;
}

struct C0;
const CONTEXT_0: Tag = Tag::new(Class::Context, 0);

impl AsnType for C0 {
    const TAG: Tag = CONTEXT_0;
}

impl<T: crate::Decode> crate::Decode for InstanceOf<T> {
    fn decode_with_tag<D: crate::Decoder>(decoder: &mut D, tag: Tag) -> Result<Self, D::Error> {
        let mut sequence = decoder.decode_sequence(tag)?;
        let type_id = ObjectIdentifier::decode(&mut sequence)?;
        let value = <Explicit<C0, T>>::decode(&mut sequence)?.value;

        Ok(Self { type_id, value })
    }
}

impl<T: crate::Encode> crate::Encode for InstanceOf<T> {
    fn encode_with_tag<D: crate::Encoder>(
        &self,
        encoder: &mut D,
        tag: Tag,
    ) -> Result<(), D::Error> {
        encoder.encode_sequence(tag, |sequence| {
            self.type_id.encode(sequence)?;
            sequence.encode_explicit_prefix(CONTEXT_0, &self.value)?;
            Ok(())
        })?;

        Ok(())
    }
}