ctap2-proto: Serialize/Deserialize authenticator::client_pin::AuthProtocolVersion as u8

Use u8::From<AuthProtocolVersion> and AuthProtocolVersion::TryFrom<u8>
implementations instead of manually implementing Serialize/Deserialize.
main
Nick Zana 1 year ago
parent 4a816e846c
commit 16d11745a3

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

Loading…
Cancel
Save