use crate::{ContextoidId, IdentificationValue};
use alloc::string::String;
use core::error::Error;
use core::fmt::{Display, Formatter};
#[derive(Debug, Clone, PartialEq)]
pub struct ProjectionError(pub ProjectionErrorEnum);
impl Error for ProjectionError {}
#[derive(Debug, Clone, PartialEq)]
pub enum ProjectionErrorEnum {
WrongVariant {
id: ContextoidId,
expected: &'static str,
found: &'static str,
},
WrongPayload {
id: ContextoidId,
expected: &'static str,
found: &'static str,
},
MissingField {
id: ContextoidId,
field: &'static str,
},
Unrecordable {
id: ContextoidId,
kind: &'static str,
},
Scalar { id: ContextoidId, value: f64 },
Identity {
id: IdentificationValue,
rule: &'static str,
},
Version { found: u16, supported: u16 },
Rejected { id: ContextoidId, rule: String },
Frozen { id: IdentificationValue },
}
impl ProjectionError {
pub const fn new(kind: ProjectionErrorEnum) -> Self {
Self(kind)
}
pub const fn kind(&self) -> &ProjectionErrorEnum {
&self.0
}
#[allow(non_snake_case)]
pub const fn WrongVariant(
id: ContextoidId,
expected: &'static str,
found: &'static str,
) -> Self {
Self(ProjectionErrorEnum::WrongVariant {
id,
expected,
found,
})
}
#[allow(non_snake_case)]
pub const fn WrongPayload(
id: ContextoidId,
expected: &'static str,
found: &'static str,
) -> Self {
Self(ProjectionErrorEnum::WrongPayload {
id,
expected,
found,
})
}
#[allow(non_snake_case)]
pub const fn MissingField(id: ContextoidId, field: &'static str) -> Self {
Self(ProjectionErrorEnum::MissingField { id, field })
}
#[allow(non_snake_case)]
pub const fn Unrecordable(id: ContextoidId, kind: &'static str) -> Self {
Self(ProjectionErrorEnum::Unrecordable { id, kind })
}
#[allow(non_snake_case)]
pub const fn Scalar(id: ContextoidId, value: f64) -> Self {
Self(ProjectionErrorEnum::Scalar { id, value })
}
#[allow(non_snake_case)]
pub const fn Identity(id: IdentificationValue, rule: &'static str) -> Self {
Self(ProjectionErrorEnum::Identity { id, rule })
}
#[allow(non_snake_case)]
pub const fn Version(found: u16, supported: u16) -> Self {
Self(ProjectionErrorEnum::Version { found, supported })
}
#[allow(non_snake_case)]
pub const fn Rejected(id: ContextoidId, rule: String) -> Self {
Self(ProjectionErrorEnum::Rejected { id, rule })
}
#[allow(non_snake_case)]
pub const fn Frozen(id: IdentificationValue) -> Self {
Self(ProjectionErrorEnum::Frozen { id })
}
}
impl Display for ProjectionError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match &self.0 {
ProjectionErrorEnum::WrongVariant {
id,
expected,
found,
} => write!(
f,
"ProjectionError: node {id} expected variant {expected}, found {found}"
),
ProjectionErrorEnum::WrongPayload {
id,
expected,
found,
} => write!(
f,
"ProjectionError: node {id} expected payload {expected}, found {found}"
),
ProjectionErrorEnum::MissingField { id, field } => {
write!(f, "ProjectionError: node {id} lacks field {field}")
}
ProjectionErrorEnum::Unrecordable { id, kind } => {
write!(f, "ProjectionError: node {id} of kind {kind} has no record")
}
ProjectionErrorEnum::Scalar { id, value } => write!(
f,
"ProjectionError: node {id} cannot hold the scalar {value}"
),
ProjectionErrorEnum::Identity { id, rule } => {
write!(
f,
"ProjectionError: identifier {id} breaks the rule: {rule}"
)
}
ProjectionErrorEnum::Version { found, supported } => write!(
f,
"ProjectionError: record version {found} is newer than the supported {supported}"
),
ProjectionErrorEnum::Rejected { id, rule } => {
write!(f, "ProjectionError: node {id} breaks the rule: {rule}")
}
ProjectionErrorEnum::Frozen { id } => write!(
f,
"ProjectionError: context {id} is frozen; unfreeze it before applying events"
),
}
}
}