use crate::requests::common::wire_header_1_0::WireHeader as Raw;
use crate::requests::ResponseStatus;
use crate::requests::{AuthType, BodyType, Opcode, ProviderId};
#[cfg(feature = "fuzz")]
use arbitrary::Arbitrary;
use num::FromPrimitive;
use std::convert::TryFrom;
#[cfg_attr(feature = "fuzz", derive(Arbitrary))]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct RequestHeader {
pub provider: ProviderId,
pub session: u64,
pub content_type: BodyType,
pub accept_type: BodyType,
pub auth_type: AuthType,
pub opcode: Opcode,
}
impl RequestHeader {
#[cfg(feature = "testing")]
pub(crate) fn new() -> RequestHeader {
RequestHeader {
provider: ProviderId::Core,
session: 0,
content_type: BodyType::Protobuf,
accept_type: BodyType::Protobuf,
auth_type: AuthType::Direct,
opcode: Opcode::Ping,
}
}
}
impl TryFrom<Raw> for RequestHeader {
type Error = ResponseStatus;
fn try_from(header: Raw) -> ::std::result::Result<Self, Self::Error> {
let content_type: BodyType = match FromPrimitive::from_u8(header.content_type) {
Some(content_type) => content_type,
None => return Err(ResponseStatus::ContentTypeNotSupported),
};
let accept_type: BodyType = match FromPrimitive::from_u8(header.accept_type) {
Some(accept_type) => accept_type,
None => return Err(ResponseStatus::AcceptTypeNotSupported),
};
let auth_type: AuthType = match FromPrimitive::from_u8(header.auth_type) {
Some(auth_type) => auth_type,
None => return Err(ResponseStatus::AuthenticatorDoesNotExist),
};
let opcode: Opcode = match FromPrimitive::from_u32(header.opcode) {
Some(opcode) => opcode,
None => return Err(ResponseStatus::OpcodeDoesNotExist),
};
Ok(RequestHeader {
provider: ProviderId::try_from(header.provider)?,
session: header.session,
content_type,
accept_type,
auth_type,
opcode,
})
}
}
impl From<RequestHeader> for Raw {
fn from(header: RequestHeader) -> Self {
Raw {
flags: 0,
provider: header.provider as u8,
session: header.session,
content_type: header.content_type as u8,
accept_type: header.accept_type as u8,
auth_type: header.auth_type as u8,
body_len: 0,
auth_len: 0,
opcode: header.opcode as u32,
status: 0, reserved1: 0,
reserved2: 0,
}
}
}