#[derive(Debug, Clone)]
#[allow(missing_docs)]
#[non_exhaustive]
pub enum BindError {
MissingCompileTimeFeature {
reason: &'static str,
feature: &'static str,
},
MissingPlatformSupport {
reason: &'static str,
feature: &'static str,
},
EvnVarError {
reason: &'static str,
var: &'static str,
fault: &'static str,
},
MultiBindWithoutAddresses,
InvalidUserOption { name: &'static str },
}
impl std::error::Error for BindError {}
impl std::fmt::Display for BindError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BindError::MissingCompileTimeFeature { reason, feature } => write!(f, "tokio-listener: cannot {reason} because of {feature} was not enabled at compile time"),
BindError::MissingPlatformSupport { reason, feature } => write!(f, "tokio-listener: cannot {reason} because of it is not a {feature}"),
BindError::EvnVarError { var, fault, reason } => write!(f, "tokio-listener: cannot {reason} due to problem with environment variable {var}: {fault}"),
BindError::MultiBindWithoutAddresses => write!(f, "tokio-listener: no addresses specified to bind to"),
BindError::InvalidUserOption { name } => write!(f, "tokio-listener: invalid value for user option {name}"),
}
}
}
impl BindError {
pub(crate) fn ioerr<T>(self) -> Result<T, std::io::Error> {
Err(std::io::Error::new(std::io::ErrorKind::Other, self))
}
}
#[allow(dead_code)]
pub(crate) fn get_envvar(
reason: &'static str,
var: &'static str,
) -> Result<String, std::io::Error> {
match std::env::var(var) {
Ok(x) => Ok(x),
Err(e) => match e {
std::env::VarError::NotPresent => BindError::EvnVarError {
reason,
var,
fault: "not present",
}
.ioerr(),
std::env::VarError::NotUnicode(..) => BindError::EvnVarError {
reason,
var,
fault: "not unicode",
}
.ioerr(),
},
}
}
#[derive(Debug, Clone)]
#[allow(missing_docs)]
#[non_exhaustive]
pub enum AcceptError {
InetdPseudosocketAlreadyTaken,
}
impl std::error::Error for AcceptError {}
impl std::fmt::Display for AcceptError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AcceptError::InetdPseudosocketAlreadyTaken => write!(
f,
"tokio-listener: Cannot serve a second connection in inetd mode"
),
}
}
}