der/asn1/choice.rs
1//! ASN.1 `CHOICE` support.
2
3use crate::{Decode, FixedTag, Tag, Tagged};
4
5/// ASN.1 `CHOICE` denotes a union of one or more possible alternatives.
6///
7/// The types MUST have distinct tags.
8///
9/// This crate models choice as a trait, with a blanket impl for all types
10/// which impl `Decode + FixedTag` (i.e. they are modeled as a `CHOICE`
11/// with only one possible variant)
12pub trait Choice<'a>: Decode<'a> + Tagged {
13 /// Is the provided [`Tag`] decodable as a variant of this `CHOICE`?
14 fn can_decode(tag: Tag) -> bool;
15}
16
17/// This blanket impl allows any [`Tagged`] type to function as a [`Choice`]
18/// with a single alternative.
19impl<'a, T> Choice<'a> for T
20where
21 T: Decode<'a> + FixedTag,
22{
23 fn can_decode(tag: Tag) -> bool {
24 T::TAG == tag
25 }
26}