extern crate alloc;
use super::{
AllAcceptedCredentialsOptions, AuthTransports, AuthenticatorAttachment, AuthenticatorTransport,
Challenge, CredentialId, CurrentUserDetailsOptions, Origin, SentChallenge,
};
use alloc::borrow::Cow;
use core::{
fmt::{self, Formatter},
marker::PhantomData,
};
use serde::{
de::{Deserialize, Deserializer, Error, IgnoredAny, MapAccess, SeqAccess, Unexpected, Visitor},
ser::{Serialize, SerializeSeq as _, SerializeStruct as _, Serializer},
};
const BLE: &str = "ble";
const HYBRID: &str = "hybrid";
const INTERNAL: &str = "internal";
const NFC: &str = "nfc";
const SMART_CARD: &str = "smart-card";
const USB: &str = "usb";
impl Serialize for AuthenticatorTransport {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(match *self {
Self::Ble => BLE,
Self::Hybrid => HYBRID,
Self::Internal => INTERNAL,
Self::Nfc => NFC,
Self::SmartCard => SMART_CARD,
Self::Usb => USB,
})
}
}
impl<'de> Deserialize<'de> for AuthenticatorTransport {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct AuthenticatorTransportVisitor;
impl Visitor<'_> for AuthenticatorTransportVisitor {
type Value = AuthenticatorTransport;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("AuthenticatorTransport")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
const CA_BLE: &str = "cable";
match v {
BLE => Ok(AuthenticatorTransport::Ble),
CA_BLE | HYBRID => Ok(AuthenticatorTransport::Hybrid),
INTERNAL => Ok(AuthenticatorTransport::Internal),
NFC => Ok(AuthenticatorTransport::Nfc),
SMART_CARD => Ok(AuthenticatorTransport::SmartCard),
USB => Ok(AuthenticatorTransport::Usb),
_ => Err(E::invalid_value(
Unexpected::Str(v),
&format!(
"'{BLE}', '{CA_BLE}', '{HYBRID}', '{INTERNAL}', '{NFC}', '{SMART_CARD}', or '{USB}'"
)
.as_str(),
)),
}
}
}
deserializer.deserialize_str(AuthenticatorTransportVisitor)
}
}
impl Serialize for AuthTransports {
#[expect(clippy::unreachable, reason = "there is a bug, so we want to crash")]
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let count = usize::try_from(self.count())
.unwrap_or_else(|_e| unreachable!("there is a bug in AuthenticatorTransports::count"));
serializer.serialize_seq(Some(count)).and_then(|mut ser| {
if self.contains(AuthenticatorTransport::Ble) {
ser.serialize_element(&AuthenticatorTransport::Ble)
} else {
Ok(())
}
.and_then(|()| {
if self.contains(AuthenticatorTransport::Hybrid) {
ser.serialize_element(&AuthenticatorTransport::Hybrid)
} else {
Ok(())
}
.and_then(|()| {
if self.contains(AuthenticatorTransport::Internal) {
ser.serialize_element(&AuthenticatorTransport::Internal)
} else {
Ok(())
}
.and_then(|()| {
if self.contains(AuthenticatorTransport::Nfc) {
ser.serialize_element(&AuthenticatorTransport::Nfc)
} else {
Ok(())
}
.and_then(|()| {
if self.contains(AuthenticatorTransport::SmartCard) {
ser.serialize_element(&AuthenticatorTransport::SmartCard)
} else {
Ok(())
}
.and_then(|()| {
if self.contains(AuthenticatorTransport::Usb) {
ser.serialize_element(&AuthenticatorTransport::Usb)
} else {
Ok(())
}
.and_then(|()| ser.end())
})
})
})
})
})
})
}
}
impl<'de> Deserialize<'de> for AuthTransports {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct AuthTransportsVisitor;
impl<'d> Visitor<'d> for AuthTransportsVisitor {
type Value = AuthTransports;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("AuthTransports")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'d>,
{
let mut transports = AuthTransports::new();
while let Some(val) = seq.next_element::<AuthenticatorTransport>()? {
transports = transports.add_transport(val);
}
Ok(transports)
}
}
deserializer.deserialize_seq(AuthTransportsVisitor)
}
}
impl<T: AsRef<[u8]>> Serialize for CredentialId<T> {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(base64url_nopad::encode(self.0.as_ref()).as_str())
}
}
impl<'de> Deserialize<'de> for CredentialId<Box<[u8]>> {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct CredentialIdVisitor;
impl Visitor<'_> for CredentialIdVisitor {
type Value = CredentialId<Box<[u8]>>;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("CredentialId")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
const MIN_LEN: usize = base64url_nopad::encode_len(super::CRED_ID_MIN_LEN);
const MAX_LEN: usize = base64url_nopad::encode_len(super::CRED_ID_MAX_LEN);
if (MIN_LEN..=MAX_LEN).contains(&v.len()) {
base64url_nopad::decode(v.as_bytes())
.map_err(E::custom)
.map(|c| CredentialId(c.into_boxed_slice()))
} else {
Err(E::invalid_value(
Unexpected::Str(v),
&"16 to 1023 bytes encoded in base64url without padding",
))
}
}
}
deserializer.deserialize_str(CredentialIdVisitor)
}
}
impl<'de> Deserialize<'de> for AuthenticatorAttachment {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct AuthenticatorAttachmentVisitor;
impl Visitor<'_> for AuthenticatorAttachmentVisitor {
type Value = AuthenticatorAttachment;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("AuthenticatorAttachment")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
const PLATFORM: &str = "platform";
const CROSS_PLATFORM: &str = "cross-platform";
match v {
PLATFORM => Ok(AuthenticatorAttachment::Platform),
CROSS_PLATFORM => Ok(AuthenticatorAttachment::CrossPlatform),
_ => Err(E::invalid_value(
Unexpected::Str(v),
&format!("'{PLATFORM}' or '{CROSS_PLATFORM}'").as_str(),
)),
}
}
}
deserializer.deserialize_str(AuthenticatorAttachmentVisitor)
}
}
pub(crate) struct Base64DecodedVal(pub Vec<u8>);
impl<'de> Deserialize<'de> for Base64DecodedVal {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct Base64DecodedValVisitor;
impl Visitor<'_> for Base64DecodedValVisitor {
type Value = Base64DecodedVal;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("base64url-encoded data")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
base64url_nopad::decode(v.as_bytes())
.map_err(E::custom)
.map(Base64DecodedVal)
}
}
deserializer.deserialize_str(Base64DecodedValVisitor)
}
}
impl<'de> Deserialize<'de> for SentChallenge {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct ChallengeVisitor;
impl Visitor<'_> for ChallengeVisitor {
type Value = SentChallenge;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str(
"base64 encoding of the 16-byte challenge in a URL safe way without padding",
)
}
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: Error,
{
if v.len() == Challenge::BASE64_LEN {
let mut data = [0; 16];
base64url_nopad::decode_buffer_exact(v, data.as_mut_slice())
.map_err(E::custom)
.map(|()| SentChallenge::from_array(data))
} else {
Err(E::invalid_value(
Unexpected::Bytes(v),
&"22 bytes encoded in base64url without padding",
))
}
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
self.visit_bytes(v.as_bytes())
}
}
deserializer.deserialize_bytes(ChallengeVisitor)
}
}
impl<'de: 'a, 'a> Deserialize<'de> for Origin<'a> {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct OriginVisitor<'b>(PhantomData<fn() -> &'b ()>);
impl<'d: 'b, 'b> Visitor<'d> for OriginVisitor<'b> {
type Value = Origin<'b>;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("Origin")
}
fn visit_borrowed_str<E>(self, v: &'d str) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Origin(Cow::Borrowed(v)))
}
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Origin(Cow::Owned(v)))
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
self.visit_string(v.to_owned())
}
}
deserializer.deserialize_str(OriginVisitor(PhantomData))
}
}
pub(super) trait ClientExtensions: Sized {
fn empty() -> Self;
}
pub(super) struct PublicKeyCredential<const RELAXED: bool, const REG: bool, AuthResp, Ext> {
pub id: Option<CredentialId<Box<[u8]>>>,
pub response: AuthResp,
pub authenticator_attachment: AuthenticatorAttachment,
pub client_extension_results: Ext,
}
pub(crate) struct Type;
impl<'e> Deserialize<'e> for Type {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'e>,
{
struct TypeVisitor;
impl Visitor<'_> for TypeVisitor {
type Value = Type;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str(PUBLIC_KEY)
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
if v == PUBLIC_KEY {
Ok(Type)
} else {
Err(E::invalid_value(Unexpected::Str(v), &PUBLIC_KEY))
}
}
}
deserializer.deserialize_str(TypeVisitor)
}
}
struct PublicKeyCredentialVisitor<const RELAXED: bool, const REG: bool, R, E>(
pub PhantomData<fn() -> (R, E)>,
);
impl<'d, const REL: bool, const REGI: bool, R, E> Visitor<'d>
for PublicKeyCredentialVisitor<REL, REGI, R, E>
where
R: Deserialize<'d>,
E: for<'a> Deserialize<'a> + ClientExtensions,
{
type Value = PublicKeyCredential<REL, REGI, R, E>;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("PublicKeyCredential")
}
#[expect(
clippy::too_many_lines,
reason = "rather hide all the internal logic instead instead of moving into an outer scope"
)]
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'d>,
{
enum Field<const IGNORE_UNKNOWN: bool> {
Id,
Type,
RawId,
Response,
AuthenticatorAttachment,
ClientExtensionResults,
Other,
}
impl<'e, const IGNORE: bool> Deserialize<'e> for Field<IGNORE> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'e>,
{
struct FieldVisitor<const IGNORE_UNKNOWN: bool>;
impl<const IGN: bool> Visitor<'_> for FieldVisitor<IGN> {
type Value = Field<IGN>;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
write!(
formatter,
"'{ID}', '{TYPE}', '{RAW_ID}', '{RESPONSE}', '{AUTHENTICATOR_ATTACHMENT}', or '{CLIENT_EXTENSION_RESULTS}'"
)
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
match v {
ID => Ok(Field::Id),
TYPE => Ok(Field::Type),
RAW_ID => Ok(Field::RawId),
RESPONSE => Ok(Field::Response),
AUTHENTICATOR_ATTACHMENT => Ok(Field::AuthenticatorAttachment),
CLIENT_EXTENSION_RESULTS => Ok(Field::ClientExtensionResults),
_ => {
if IGN {
Ok(Field::Other)
} else {
Err(E::unknown_field(v, REG_FIELDS))
}
}
}
}
}
deserializer.deserialize_identifier(FieldVisitor::<IGNORE>)
}
}
let mut opt_id = None;
let mut typ = false;
let mut raw = None;
let mut resp = None;
let mut attach = None;
let mut ext = None;
while let Some(key) = map.next_key::<Field<REL>>()? {
match key {
Field::Id => {
if opt_id.is_some() {
return Err(Error::duplicate_field(ID));
}
opt_id = map.next_value::<CredentialId<_>>().map(Some)?;
}
Field::Type => {
if typ {
return Err(Error::duplicate_field(TYPE));
}
typ = map.next_value::<Type>().map(|_| true)?;
}
Field::RawId => {
if raw.is_some() {
return Err(Error::duplicate_field(RAW_ID));
}
raw = map.next_value::<CredentialId<_>>().map(Some)?;
}
Field::Response => {
if resp.is_some() {
return Err(Error::duplicate_field(RESPONSE));
}
resp = map.next_value::<R>().map(Some)?;
}
Field::AuthenticatorAttachment => {
if attach.is_some() {
return Err(Error::duplicate_field(AUTHENTICATOR_ATTACHMENT));
}
attach = map.next_value().map(Some)?;
}
Field::ClientExtensionResults => {
if ext.is_some() {
return Err(Error::duplicate_field(CLIENT_EXTENSION_RESULTS));
}
ext = map.next_value::<Option<E>>().map(Some)?;
}
Field::Other => map.next_value::<IgnoredAny>().map(|_| ())?,
}
}
resp.ok_or_else(|| Error::missing_field(RESPONSE))
.and_then(|response| {
opt_id.map_or_else(
|| {
if REL && REGI {
Ok(None)
} else {
Err(Error::missing_field(ID))
}
},
|id| Ok(Some(id)),
)
.and_then(|id| {
raw.map_or_else(
|| {
if REL {
Ok(())
} else {
Err(Error::missing_field(RAW_ID))
}
},
|raw_id| {
id.as_ref().map_or_else(
|| Ok(()),
|i| {
if raw_id == i {
Ok(())
} else {
Err(Error::invalid_value(
Unexpected::Bytes(raw_id.as_ref()),
&format!("{ID} and {RAW_ID} to match: {i:?}").as_str(),
))
}
},
)
}
)
.and_then(|()| {
ext.ok_or(false).and_then(|opt_ext| opt_ext.ok_or(true)).map_or_else(
|flag| {
if REL {
Ok(E::empty())
} else if flag {
Err(Error::invalid_type(Unexpected::Other("null"), &format!("{CLIENT_EXTENSION_RESULTS} to be a map of allowed client extensions").as_str()))
} else {
Err(Error::missing_field(CLIENT_EXTENSION_RESULTS))
}
},
Ok
)
.and_then(|client_extension_results| {
if typ || REL {
Ok(PublicKeyCredential {
id,
response,
authenticator_attachment: attach.flatten().unwrap_or(AuthenticatorAttachment::None),
client_extension_results,
})
} else {
Err(Error::missing_field(TYPE))
}
})
})
})
})
}
}
const ID: &str = "id";
const TYPE: &str = "type";
const RAW_ID: &str = "rawId";
const RESPONSE: &str = "response";
const AUTHENTICATOR_ATTACHMENT: &str = "authenticatorAttachment";
const CLIENT_EXTENSION_RESULTS: &str = "clientExtensionResults";
const PUBLIC_KEY: &str = "public-key";
const REG_FIELDS: &[&str; 6] = &[
ID,
TYPE,
RAW_ID,
RESPONSE,
AUTHENTICATOR_ATTACHMENT,
CLIENT_EXTENSION_RESULTS,
];
impl<'de, const REL: bool, const REGI: bool, R, E> Deserialize<'de>
for PublicKeyCredential<REL, REGI, R, E>
where
R: Deserialize<'de>,
E: for<'a> Deserialize<'a> + ClientExtensions,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_struct(
"PublicKeyCredential",
REG_FIELDS,
PublicKeyCredentialVisitor::<REL, REGI, _, _>(PhantomData),
)
}
}
use super::UserHandle;
impl<const USER_LEN: usize> Serialize for AllAcceptedCredentialsOptions<'_, '_, USER_LEN>
where
UserHandle<USER_LEN>: Serialize,
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer
.serialize_struct("AllAcceptedCredentialsOptions", 3)
.and_then(|mut ser| {
ser.serialize_field("rpId", self.rp_id).and_then(|()| {
ser.serialize_field("userId", &self.user_id).and_then(|()| {
ser.serialize_field(
"allAcceptedCredentialIds",
&self.all_accepted_credential_ids,
)
.and_then(|()| ser.end())
})
})
})
}
}
impl<const LEN: usize> Serialize for CurrentUserDetailsOptions<'_, '_, '_, '_, LEN>
where
UserHandle<LEN>: Serialize,
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer
.serialize_struct("CurrentUserDetailsOptions", 4)
.and_then(|mut ser| {
ser.serialize_field("rpId", self.rp_id).and_then(|()| {
ser.serialize_field("userId", &self.user.id).and_then(|()| {
ser.serialize_field("name", &self.user.name).and_then(|()| {
ser.serialize_field("displayName", &self.user.display_name)
.and_then(|()| ser.end())
})
})
})
})
}
}
pub(crate) struct Null;
impl<'de> Deserialize<'de> for Null {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct NullVisitor;
impl Visitor<'_> for NullVisitor {
type Value = Null;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("null")
}
fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Null)
}
}
deserializer.deserialize_option(NullVisitor)
}
}
pub(super) struct AuthenticationExtensionsPrfValues;
pub(super) struct AuthenticationExtensionsPrfValuesVisitor<const RELAXED: bool>;
impl<'d, const R: bool> Visitor<'d> for AuthenticationExtensionsPrfValuesVisitor<R> {
type Value = AuthenticationExtensionsPrfValues;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("AuthenticationExtensionsPrfValues")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'d>,
{
enum Field<const IGNORE_UNKNOWN: bool> {
First,
Second,
Other,
}
impl<'e, const I: bool> Deserialize<'e> for Field<I> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'e>,
{
struct FieldVisitor<const IGNORE_UNKNOWN: bool>;
impl<const IG: bool> Visitor<'_> for FieldVisitor<IG> {
type Value = Field<IG>;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
write!(formatter, "'{FIRST}' or '{SECOND}'")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
match v {
FIRST => Ok(Field::First),
SECOND => Ok(Field::Second),
_ => {
if IG {
Ok(Field::Other)
} else {
Err(E::unknown_field(v, PRF_VALUES_FIELDS))
}
}
}
}
}
deserializer.deserialize_identifier(FieldVisitor)
}
}
let mut first = None;
let mut second = None;
while let Some(key) = map.next_key::<Field<R>>()? {
match key {
Field::First => {
if first.is_some() {
return Err(Error::duplicate_field(FIRST));
}
first = map.next_value::<Null>().map(Some)?;
}
Field::Second => {
if second.is_some() {
return Err(Error::duplicate_field(SECOND));
}
second = map.next_value::<Null>().map(Some)?;
}
Field::Other => map.next_value::<IgnoredAny>().map(|_| ())?,
}
}
if first.is_some() || (R && second.is_none()) {
Ok(AuthenticationExtensionsPrfValues)
} else {
Err(Error::missing_field(FIRST))
}
}
}
const FIRST: &str = "first";
const SECOND: &str = "second";
pub(super) const PRF_VALUES_FIELDS: &[&str; 2] = &[FIRST, SECOND];
impl<'de> Deserialize<'de> for AuthenticationExtensionsPrfValues {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_struct(
"AuthenticationExtensionsPrfValues",
PRF_VALUES_FIELDS,
AuthenticationExtensionsPrfValuesVisitor::<false>,
)
}
}
pub(super) struct AuthenticationExtensionsPrfOutputsHelper<
const RELAXED: bool,
const REGISTRATION: bool,
Prf,
>(pub Option<bool>, pub PhantomData<fn() -> Prf>);
struct AuthenticationExtensionsPrfOutputsVisitor<const RELAXED: bool, const REGISTRATION: bool, Prf>(
PhantomData<fn() -> Prf>,
);
impl<'d, const REL: bool, const REG: bool, Prf> Visitor<'d>
for AuthenticationExtensionsPrfOutputsVisitor<REL, REG, Prf>
where
Prf: for<'a> Deserialize<'a>,
{
type Value = AuthenticationExtensionsPrfOutputsHelper<REL, REG, Prf>;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("AuthenticationExtensionsPrfOutputs")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'d>,
{
enum Field<const IGNORE_UNKNOWN: bool, const REGI: bool> {
Enabled,
Results,
Other,
}
impl<'e, const I: bool, const R: bool> Deserialize<'e> for Field<I, R> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'e>,
{
struct FieldVisitor<const IGNORE_UNKNOWN: bool, const REGI: bool>;
impl<const IG: bool, const RE: bool> Visitor<'_> for FieldVisitor<IG, RE> {
type Value = Field<IG, RE>;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
if RE {
write!(formatter, "'{ENABLED}' or '{RESULTS}'")
} else {
write!(formatter, "'{RESULTS}'")
}
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
match v {
ENABLED => {
if RE {
Ok(Field::Enabled)
} else {
Err(E::unknown_field(v, PRF_AUTH_OUTPUTS_FIELDS))
}
}
RESULTS => Ok(Field::Results),
_ => {
if IG {
Ok(Field::Other)
} else if RE {
Err(E::unknown_field(v, PRF_REG_OUTPUTS_FIELDS))
} else {
Err(E::unknown_field(v, PRF_AUTH_OUTPUTS_FIELDS))
}
}
}
}
}
deserializer.deserialize_identifier(FieldVisitor)
}
}
let mut enabled = None;
let mut results = None;
while let Some(key) = map.next_key::<Field<REL, REG>>()? {
match key {
Field::Enabled => {
if enabled.is_some() {
return Err(Error::duplicate_field(ENABLED));
}
enabled = map.next_value().map(Some)?;
}
Field::Results => {
if results.is_some() {
return Err(Error::duplicate_field(RESULTS));
}
results = map.next_value::<Option<Prf>>().map(Some)?;
}
Field::Other => map.next_value::<IgnoredAny>().map(|_| ())?,
}
}
if REG {
enabled.ok_or_else(|| Error::missing_field(ENABLED)).and_then(|e| {
if e || results.is_none() {
Ok(())
} else {
Err(Error::custom("prf must not have 'results', including a null 'results', if 'enabled' is false"))
}
})
} else {
Ok(())
}.map(|()| AuthenticationExtensionsPrfOutputsHelper(enabled, PhantomData))
}
}
const ENABLED: &str = "enabled";
const RESULTS: &str = "results";
const PRF_REG_OUTPUTS_FIELDS: &[&str; 2] = &[ENABLED, RESULTS];
const PRF_AUTH_OUTPUTS_FIELDS: &[&str; 1] = &[RESULTS];
impl<'de, const REL: bool, const REG: bool, Prf> Deserialize<'de>
for AuthenticationExtensionsPrfOutputsHelper<REL, REG, Prf>
where
for<'a> Prf: Deserialize<'a>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_struct(
"AuthenticationExtensionsPrfOutputs",
if REG {
PRF_REG_OUTPUTS_FIELDS
} else {
PRF_AUTH_OUTPUTS_FIELDS
},
AuthenticationExtensionsPrfOutputsVisitor::<REL, REG, Prf>(PhantomData),
)
}
}