use crate::error::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum CustomDialingConflict {
ResolvedDestinations,
LocalAddressOrSocketOptions,
UnixDomainRouting,
Http3,
BuiltInProxyRouting,
}
impl CustomDialingConflict {
pub(super) fn message(self) -> &'static str {
match self {
Self::ResolvedDestinations => {
"custom dialing is incompatible with caller-supplied resolved destinations"
}
Self::LocalAddressOrSocketOptions => {
"custom dialing is incompatible with local-address or socket-option routing"
}
Self::UnixDomainRouting => "custom dialing is incompatible with Unix-domain routing",
Self::Http3 => "custom dialing is incompatible with HTTP/3",
Self::BuiltInProxyRouting => {
"custom dialing is incompatible with built-in proxy routing"
}
}
}
pub(super) fn error(self) -> Error {
Error::Unsupported(self.message().into())
}
}
#[allow(
clippy::struct_excessive_bools,
reason = "the ladder arms are independent routing facts; named fields keep the call sites readable"
)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(super) struct CustomDialingState {
pub(super) has_resolved_target: bool,
pub(super) has_local_address_or_socket_options: bool,
pub(super) has_uds: bool,
pub(super) is_http3_only: bool,
pub(super) has_built_in_proxy: Option<bool>,
}
pub(super) fn custom_dialing_conflict(state: CustomDialingState) -> Option<CustomDialingConflict> {
if state.has_resolved_target {
return Some(CustomDialingConflict::ResolvedDestinations);
}
if state.has_local_address_or_socket_options {
return Some(CustomDialingConflict::LocalAddressOrSocketOptions);
}
if state.has_uds {
return Some(CustomDialingConflict::UnixDomainRouting);
}
if state.is_http3_only {
return Some(CustomDialingConflict::Http3);
}
if state.has_built_in_proxy == Some(true) {
return Some(CustomDialingConflict::BuiltInProxyRouting);
}
None
}
#[cfg(test)]
mod tests {
use super::{custom_dialing_conflict, CustomDialingConflict, CustomDialingState};
fn state() -> CustomDialingState {
CustomDialingState::default()
}
#[test]
fn no_conflict_without_any_arm() {
assert_eq!(custom_dialing_conflict(state()), None);
}
#[test]
fn resolved_destinations_win_over_every_other_arm() {
let mut arms = state();
arms.has_resolved_target = true;
arms.has_local_address_or_socket_options = true;
arms.has_uds = true;
arms.is_http3_only = true;
arms.has_built_in_proxy = Some(true);
assert_eq!(
custom_dialing_conflict(arms),
Some(CustomDialingConflict::ResolvedDestinations)
);
}
#[test]
fn local_address_wins_over_uds_and_http3() {
let mut arms = state();
arms.has_local_address_or_socket_options = true;
arms.has_uds = true;
arms.is_http3_only = true;
arms.has_built_in_proxy = Some(true);
assert_eq!(
custom_dialing_conflict(arms),
Some(CustomDialingConflict::LocalAddressOrSocketOptions)
);
}
#[test]
fn uds_wins_over_http3_and_built_in_proxy() {
let mut arms = state();
arms.has_uds = true;
arms.is_http3_only = true;
arms.has_built_in_proxy = Some(true);
assert_eq!(
custom_dialing_conflict(arms),
Some(CustomDialingConflict::UnixDomainRouting)
);
}
#[test]
fn http3_wins_over_built_in_proxy() {
let mut arms = state();
arms.is_http3_only = true;
arms.has_built_in_proxy = Some(true);
assert_eq!(
custom_dialing_conflict(arms),
Some(CustomDialingConflict::Http3)
);
}
#[test]
fn built_in_proxy_is_last() {
let mut arms = state();
arms.has_built_in_proxy = Some(true);
assert_eq!(
custom_dialing_conflict(arms),
Some(CustomDialingConflict::BuiltInProxyRouting)
);
}
#[test]
fn absent_built_in_proxy_arm_never_conflicts() {
let mut arms = state();
arms.has_built_in_proxy = None;
assert_eq!(custom_dialing_conflict(arms), None);
}
#[test]
fn disabled_built_in_proxy_arm_never_conflicts() {
let mut arms = state();
arms.has_built_in_proxy = Some(false);
assert_eq!(custom_dialing_conflict(arms), None);
}
#[test]
fn every_arm_keeps_its_own_message() {
let messages = [
CustomDialingConflict::ResolvedDestinations.message(),
CustomDialingConflict::LocalAddressOrSocketOptions.message(),
CustomDialingConflict::UnixDomainRouting.message(),
CustomDialingConflict::Http3.message(),
CustomDialingConflict::BuiltInProxyRouting.message(),
];
for (index, message) in messages.iter().enumerate() {
for other in &messages[index + 1..] {
assert_ne!(message, other, "each arm keeps its own diagnostic");
}
}
}
}