rasn/types/
instance.rs

1use super::{AsnType, Class, Constraints, ObjectIdentifier, Tag};
2use crate::types::fields::{Field, FieldPresence, Fields};
3
4/// An instance of a defined object class.
5#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Hash)]
6pub struct InstanceOf<T> {
7    /// The OID identifying T's real type.
8    pub type_id: ObjectIdentifier,
9    /// The value identified by `type_id`.
10    pub value: T,
11}
12
13impl<T> AsnType for InstanceOf<T> {
14    const TAG: Tag = Tag::EXTERNAL;
15}
16
17impl<T: crate::Decode> crate::Decode for InstanceOf<T> {
18    fn decode_with_tag_and_constraints<D: crate::Decoder>(
19        decoder: &mut D,
20        tag: Tag,
21        _: Constraints,
22    ) -> Result<Self, D::Error> {
23        decoder.decode_sequence(tag, None::<fn() -> Self>, |sequence| {
24            let type_id = ObjectIdentifier::decode(sequence)?;
25            let value = sequence.decode_explicit_prefix(Tag::new(Class::Context, 0))?;
26
27            Ok(Self { type_id, value })
28        })
29    }
30}
31
32impl<T: crate::Encode> crate::Encode for InstanceOf<T> {
33    fn encode_with_tag_and_constraints<EN: crate::Encoder>(
34        &self,
35        encoder: &mut EN,
36        tag: Tag,
37        _: Constraints,
38    ) -> core::result::Result<(), EN::Error> {
39        encoder.encode_sequence::<Self, _>(tag, |sequence| {
40            self.type_id.encode(sequence)?;
41            sequence.encode_explicit_prefix(Tag::new(Class::Context, 0), &self.value)?;
42            Ok(())
43        })?;
44
45        Ok(())
46    }
47}
48
49impl<T: AsnType> crate::types::Constructed for InstanceOf<T> {
50    const FIELDS: Fields = Fields::from_static(&[
51        Field {
52            tag: ObjectIdentifier::TAG,
53            tag_tree: ObjectIdentifier::TAG_TREE,
54            presence: FieldPresence::Required,
55            name: "type_id",
56        },
57        Field {
58            tag: T::TAG,
59            tag_tree: T::TAG_TREE,
60            presence: FieldPresence::Required,
61            name: "value",
62        },
63    ]);
64}