1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use std::fmt::Display;

use actix_web::{Error as ActixWebError, ResponseError};

pub type GuardResult<GuardCallErr> = Result<(), GuardError<GuardCallErr>>;

#[derive(Debug)]
pub enum GuardError<GuardCallErr> {
    FromRequest(ActixWebError),
    GuardCall(GuardCallErr),
}

impl<GuardCallErr> Display for GuardError<GuardCallErr>
where
    GuardCallErr: Display,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GuardError::FromRequest(from_request_error) => from_request_error.fmt(f),
            GuardError::GuardCall(guard_call_error) => guard_call_error.fmt(f),
        }
    }
}

impl<GuardCallErr> ResponseError for GuardError<GuardCallErr>
where
    GuardCallErr: ResponseError,
{
    fn status_code(&self) -> actix_web::http::StatusCode {
        match self {
            GuardError::FromRequest(from_request_error) => from_request_error.as_response_error().status_code(),
            GuardError::GuardCall(guard_call_error) => guard_call_error.status_code(),
        }
    }

    fn error_response(&self) -> actix_web::HttpResponse<actix_web::body::BoxBody> {
        match self {
            GuardError::FromRequest(from_request_error) => from_request_error.error_response(),
            GuardError::GuardCall(guard_call_error) => guard_call_error.error_response(),
        }
    }
}