Skip to content

Commit

Permalink
Derive Integer for pkcs10::Version
Browse files Browse the repository at this point in the history
Signed-off-by: Nathaniel McCallum <nathaniel@profian.com>
  • Loading branch information
npmccallum committed Feb 11, 2022
1 parent f6ee1a4 commit ea00dfc
Showing 1 changed file with 4 additions and 39 deletions.
43 changes: 4 additions & 39 deletions pkcs10/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,14 @@
//! Certification request information version identifier.
use der::{Decodable, Decoder, Encodable, Encoder, FixedTag, Tag};
use der::Enumerated;

/// Version identifier for certification request information.
///
/// (RFC 2986 designates `0` as the only valid version)
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, Enumerated)]
#[asn1(type = "INTEGER")]
#[repr(u8)]
pub enum Version {
/// Denotes PKCS#8 v1
V1 = 0,
}

impl Decodable<'_> for Version {
fn decode(decoder: &mut Decoder<'_>) -> der::Result<Self> {
Version::try_from(u8::decode(decoder)?).map_err(|_| Self::TAG.value_error())
}
}

impl Encodable for Version {
fn encoded_len(&self) -> der::Result<der::Length> {
der::Length::from(1u8).for_tlv()
}

fn encode(&self, encoder: &mut Encoder<'_>) -> der::Result<()> {
u8::from(*self).encode(encoder)
}
}

impl From<Version> for u8 {
fn from(version: Version) -> Self {
version as u8
}
}

impl TryFrom<u8> for Version {
type Error = der::Error;

fn try_from(byte: u8) -> Result<Version, der::Error> {
match byte {
0 => Ok(Version::V1),
_ => Err(Self::TAG.value_error()),
}
}
}

impl FixedTag for Version {
const TAG: Tag = Tag::Integer;
}

0 comments on commit ea00dfc

Please sign in to comment.