diff --git a/crates/ctap2-proto/src/authenticator/client_pin/mod.rs b/crates/ctap2-proto/src/authenticator/client_pin/mod.rs index 6678f92..b7f9a30 100644 --- a/crates/ctap2-proto/src/authenticator/client_pin/mod.rs +++ b/crates/ctap2-proto/src/authenticator/client_pin/mod.rs @@ -5,31 +5,33 @@ use std::collections::BTreeSet; use serde::{Deserialize, Serialize}; #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(into = "u8", try_from = "u8") +)] pub enum AuthProtocolVersion { - One, - Two, + One = 1, + Two = 2, } -// workaround until is merged -// PR: ( Integer/boolean tags for internally/adjacently tagged enums #2056 ) -#[cfg(feature = "serde")] -impl Serialize for AuthProtocolVersion { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - serializer.serialize_u8(match self { - AuthProtocolVersion::One => 1, - AuthProtocolVersion::Two => 2, - }) +impl From for u8 { + fn from(value: AuthProtocolVersion) -> Self { + value as u8 } } -// workaround until is merged -// PR: ( Integer/boolean tags for internally/adjacently tagged enums #2056 ) -#[cfg(feature = "serde")] -impl<'de> Deserialize<'de> for AuthProtocolVersion { - fn deserialize(deserializer: D) -> Result +impl TryFrom for AuthProtocolVersion { + type Error = Error; + + fn try_from(value: u8) -> Result { + match value { + 1 => Ok(AuthProtocolVersion::One), + 2 => Ok(AuthProtocolVersion::Two), + _ => Err(Error::InvalidParameter), + } + } +} where D: serde::Deserializer<'de>, {