From 16d11745a3a73a28b6f0786271ae24ff6ce0d6ba Mon Sep 17 00:00:00 2001 From: Nick Zana Date: Sat, 10 Jun 2023 23:31:13 -0400 Subject: [PATCH] ctap2-proto: Serialize/Deserialize authenticator::client_pin::AuthProtocolVersion as u8 Use u8::From and AuthProtocolVersion::TryFrom implementations instead of manually implementing Serialize/Deserialize. --- .../src/authenticator/client_pin/mod.rs | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) 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>, {