1use crate::Error;
4use der::{Decode, Encode, FixedTag, Reader, Tag, Writer};
5
6#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
21#[repr(u8)]
22pub enum Version {
23 TwoPrime = 0,
25
26 Multi = 1,
28}
29
30impl Version {
31 pub fn is_multi(self) -> bool {
33 self == Self::Multi
34 }
35}
36
37impl From<Version> for u8 {
38 fn from(version: Version) -> Self {
39 version as u8
40 }
41}
42
43impl TryFrom<u8> for Version {
44 type Error = Error;
45 fn try_from(byte: u8) -> Result<Version, Error> {
46 match byte {
47 0 => Ok(Version::TwoPrime),
48 1 => Ok(Version::Multi),
49 _ => Err(Error::Version),
50 }
51 }
52}
53
54impl<'a> Decode<'a> for Version {
55 fn decode<R: Reader<'a>>(decoder: &mut R) -> der::Result<Self> {
56 Version::try_from(u8::decode(decoder)?).map_err(|_| Self::TAG.value_error())
57 }
58}
59
60impl Encode for Version {
61 fn encoded_len(&self) -> der::Result<der::Length> {
62 der::Length::ONE.for_tlv()
63 }
64
65 fn encode(&self, writer: &mut impl Writer) -> der::Result<()> {
66 u8::from(*self).encode(writer)
67 }
68}
69
70impl FixedTag for Version {
71 const TAG: Tag = Tag::Integer;
72}