use std::{error::Error, fmt};
use crate::TrustedProxyRule;
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ClientIpConfigBuildError {
OverlappingTrustedProxyRules {
left: Box<TrustedProxyRule>,
right: Box<TrustedProxyRule>,
},
}
impl fmt::Display for ClientIpConfigBuildError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::OverlappingTrustedProxyRules {
left,
right,
} => write!(
f,
"trusted proxy CIDRs overlap but use different client IP headers: {} ({}) \
overlaps {} ({})",
left.cidr(),
describe_client_ip_header(left),
right.cidr(),
describe_client_ip_header(right),
),
}
}
}
fn describe_client_ip_header(rule: &TrustedProxyRule) -> &str {
match rule.client_ip_header() {
Some(header) => header.as_str(),
None => "no client IP header",
}
}
impl Error for ClientIpConfigBuildError {}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum ClientIpRejection {
MissingConfig,
MissingRemoteAddr,
}
impl fmt::Display for ClientIpRejection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MissingConfig => {
write!(f, "no ClientIpConfig is managed; pass one to rocket::build().manage()")
},
Self::MissingRemoteAddr => write!(f, "the request has no remote address"),
}
}
}
impl Error for ClientIpRejection {}