use crate::common::cbor_values::ByteString;
use crate::Scope;
#[cfg(not(feature = "std"))]
use {alloc::string::String, alloc::vec::Vec};
#[cfg(test)]
mod tests;
#[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Builder)]
#[builder(
no_std,
setter(into, strip_option),
default,
derive(Debug, PartialEq, Eq),
build_fn(validate = "Self::validate")
)]
pub struct AuthServerRequestCreationHint {
pub auth_server: Option<String>,
pub kid: Option<ByteString>,
pub audience: Option<String>,
pub scope: Option<Scope>,
pub client_nonce: Option<Vec<u8>>,
}
#[allow(clippy::unused_self, clippy::unnecessary_wraps)]
mod builder {
use super::*;
impl AuthServerRequestCreationHint {
#[must_use]
pub fn builder() -> AuthServerRequestCreationHintBuilder {
AuthServerRequestCreationHintBuilder::default()
}
}
impl AuthServerRequestCreationHintBuilder {
pub(crate) fn validate(&self) -> Result<(), AuthServerRequestCreationHintBuilderError> {
Ok(())
}
}
}
mod conversion {
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
use ciborium::value::Value;
use erased_serde::Serialize as ErasedSerialize;
use crate::common::cbor_map::{cbor_map_vec, decode_scope, ToCborMap};
use crate::common::constants::cbor_abbreviations::creation_hint;
use crate::error::TryFromCborMapError;
use super::*;
impl ToCborMap for AuthServerRequestCreationHint {
fn to_cbor_map(&self) -> Vec<(i128, Option<Box<dyn ErasedSerialize + '_>>)> {
cbor_map_vec! {
creation_hint::AS => self.auth_server.as_ref(),
creation_hint::KID => self.kid.as_ref(),
creation_hint::AUDIENCE => self.audience.as_ref(),
creation_hint::SCOPE => self.scope.as_ref(),
creation_hint::CNONCE => self.client_nonce.as_ref().map(|v| Value::Bytes(v.clone()))
}
}
fn try_from_cbor_map(map: Vec<(i128, Value)>) -> Result<Self, TryFromCborMapError>
where
Self: Sized + ToCborMap,
{
let mut hint = AuthServerRequestCreationHint::builder();
for entry in map {
match (u8::try_from(entry.0)?, entry.1) {
(creation_hint::AS, Value::Text(x)) => hint.auth_server(x),
(creation_hint::KID, Value::Bytes(x)) => hint.kid(x),
(creation_hint::AUDIENCE, Value::Text(x)) => hint.audience(x),
(creation_hint::SCOPE, v) => hint.scope(decode_scope(v)?),
(creation_hint::CNONCE, Value::Bytes(x)) => hint.client_nonce(x),
(key, _) => return Err(TryFromCborMapError::unknown_field(key)),
};
}
hint.build()
.map_err(|x| TryFromCborMapError::build_failed("AuthServerRequestCreationHint", x))
}
}
}