use http::StatusCode;
use crate::{
catalog::Code,
diagnostic::PublicEvidence,
fault::Fault,
http::{CorrelationId, Problem, ProblemOccurrence},
};
use super::{HttpEventContext, NormalizedRoute};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum EventSurface {
Http,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProblemEvent {
code: Code,
surface: EventSurface,
status: StatusCode,
correlation_id: CorrelationId,
request_method: Option<http::Method>,
normalized_route: Option<NormalizedRoute>,
fallback_encoding: bool,
}
impl ProblemEvent {
pub fn for_http(
code: Code,
status: StatusCode,
occurrence: &ProblemOccurrence,
context: &HttpEventContext,
fallback_encoding: bool,
) -> Self {
Self {
code,
surface: EventSurface::Http,
status,
correlation_id: occurrence.correlation_id().clone(),
request_method: context.method().cloned(),
normalized_route: context.route().cloned(),
fallback_encoding,
}
}
pub fn from_problem<E: PublicEvidence>(
problem: &Problem<E>,
context: &HttpEventContext,
fallback_encoding: bool,
) -> Self {
Self::for_http(
problem.code().clone(),
problem.status(),
problem.occurrence(),
context,
fallback_encoding,
)
}
pub const fn code(&self) -> &Code {
&self.code
}
pub const fn surface(&self) -> EventSurface {
self.surface
}
pub const fn status(&self) -> StatusCode {
self.status
}
pub const fn correlation_id(&self) -> &CorrelationId {
&self.correlation_id
}
pub const fn request_method(&self) -> Option<&http::Method> {
self.request_method.as_ref()
}
pub const fn normalized_route(&self) -> Option<&NormalizedRoute> {
self.normalized_route.as_ref()
}
pub const fn used_fallback_encoding(&self) -> bool {
self.fallback_encoding
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FaultEvent(ProblemEvent);
impl FaultEvent {
pub fn for_http(
code: Code,
status: StatusCode,
occurrence: &ProblemOccurrence,
context: &HttpEventContext,
fallback_encoding: bool,
) -> Self {
Self(ProblemEvent::for_http(
code,
status,
occurrence,
context,
fallback_encoding,
))
}
pub fn from_fault<E: PublicEvidence>(
fault: &Fault<E>,
context: &HttpEventContext,
fallback_encoding: bool,
) -> Self {
Self(ProblemEvent::from_problem(
fault.problem(),
context,
fallback_encoding,
))
}
pub const fn problem_metadata(&self) -> &ProblemEvent {
&self.0
}
}