use super::{CheckError, InfoCheckError, InfoError, MappedCheckError, Token, TokenInfo};
use crate::{
Error,
content_source::{ContentError, SourceMap},
ess::SigningAttributes,
};
use core::convert::Infallible;
use structured_asn1::Reader;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProfileSource {
Envelope,
Information,
}
#[derive(Debug)]
pub struct ProfileFailure<E> {
pub source: ProfileSource,
pub cause: E,
}
pub struct ProfileInput<'view, 'document, 'envelope, 'information> {
pub token: &'view Token<'document>,
pub parent: &'view Reader<'envelope>,
pub envelope: Option<&'view SourceMap>,
pub ess: &'view SigningAttributes<'envelope>,
pub info: &'view TokenInfo<'information>,
pub reader: &'view Reader<'information>,
pub source: &'view SourceMap,
}
#[derive(Debug)]
pub enum ProfileCheckError<SE, PE, RE, FE> {
Common(CheckError<SE, PE, RE>),
Profile {
source: ProfileFailure<FE>,
context: SourceMap,
},
}
#[derive(Debug)]
pub enum MappedProfileCheckError<SE, PE, RE, FE> {
Preflight(CheckError<SE, PE, RE>),
Provenance(Error),
Execution {
envelope: SourceMap,
source: ProfileCheckError<SE, PE, RE, FE>,
},
}
pub(super) enum InformationFailure<SE, PE, RE, FE> {
Common(InfoCheckError<SE, PE, RE>),
Profile(ProfileFailure<FE>),
}
impl<SE, PE, RE, FE> From<CheckError<SE, PE, RE>> for ProfileCheckError<SE, PE, RE, FE> {
fn from(value: CheckError<SE, PE, RE>) -> Self {
Self::Common(value)
}
}
impl<SE, PE, RE, FE> From<InfoCheckError<SE, PE, RE>> for InformationFailure<SE, PE, RE, FE> {
fn from(value: InfoCheckError<SE, PE, RE>) -> Self {
Self::Common(value)
}
}
impl<SE, PE, RE> ProfileCheckError<SE, PE, RE, Infallible> {
pub(super) fn into_common(self) -> CheckError<SE, PE, RE> {
match self {
Self::Common(source) => source,
Self::Profile { source, .. } => match source.cause {},
}
}
}
impl<SE, PE, RE> MappedProfileCheckError<SE, PE, RE, Infallible> {
pub(super) fn into_common(self) -> MappedCheckError<SE, PE, RE> {
match self {
Self::Preflight(source) => MappedCheckError::Preflight(source),
Self::Provenance(source) => MappedCheckError::Provenance(source),
Self::Execution { envelope, source } => MappedCheckError::Execution {
envelope,
source: source.into_common(),
},
}
}
}
pub(super) fn information_error<SE, PE, RE, FE>(
error: ContentError<InfoError<InformationFailure<SE, PE, RE, FE>>>,
) -> ProfileCheckError<SE, PE, RE, FE> {
let common = |source| ProfileCheckError::Common(CheckError::Information(source));
match error {
ContentError::Processing(source) => common(ContentError::Processing(source)),
ContentError::Decoding { source, context } => {
common(ContentError::Decoding { source, context })
}
ContentError::Callback {
source: InfoError::Processing(source),
context,
} => common(ContentError::Callback {
source: InfoError::Processing(source),
context,
}),
ContentError::Callback {
source: InfoError::Callback(InformationFailure::Common(source)),
context,
} => common(ContentError::Callback {
source: InfoError::Callback(source),
context,
}),
ContentError::Callback {
source: InfoError::Callback(InformationFailure::Profile(source)),
context,
} => ProfileCheckError::Profile { source, context },
}
}