From 502fe2b1761e3ffd3159b2539308543cf049a3bc Mon Sep 17 00:00:00 2001 From: Nick Zana Date: Tue, 16 May 2023 18:59:54 -0400 Subject: [PATCH] ctap2-proto: Implement serde for authentciator::client_pin::AuthProtocolVersion --- .../src/authenticator/client_pin/mod.rs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/crates/ctap2-proto/src/authenticator/client_pin/mod.rs b/crates/ctap2-proto/src/authenticator/client_pin/mod.rs index 41694e8..22b9815 100644 --- a/crates/ctap2-proto/src/authenticator/client_pin/mod.rs +++ b/crates/ctap2-proto/src/authenticator/client_pin/mod.rs @@ -1,6 +1,8 @@ use bounded_integer::BoundedUsize; use std::collections::BTreeSet; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub enum AuthProtocolVersion { @@ -8,6 +10,42 @@ pub enum AuthProtocolVersion { Two, } +// 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, + }) + } +} + +// 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 + where + D: serde::Deserializer<'de>, + { + use serde::de; + + match u8::deserialize(deserializer)? { + 1 => Ok(Self::One), + 2 => Ok(Self::Two), + i => Err(de::Error::invalid_value( + de::Unexpected::Unsigned(i.into()), + &"1 or 2", + )), + } + } +} + pub enum Subcommand { GetPinRetries, GetKeyAgreement,