INNER CODE UNIT · Rust
parse_endpoint
CluvexStudio/Aether · aether/src/lib.rs:206
fn parse_endpoint(raw: &str) -> Result<SocketAddr> {
let text = raw.trim();
if let Ok(peer) = text.parse::<SocketAddr>() {
return Ok(peer);
}
// An address with the port left off is the likely slip, so name what is
// missing rather than calling the whole thing unreadable.
let portless = text.parse::<IpAddr>().ok().or_else(|| {
text.strip_prefix('[')
.and_then(|rest| rest.strip_suffix(']'))
.and_then(|inner| inner.parse::<IpAddr>().ok())
});
if let Some(address) = portless {
return Err(AetherError::Other(format!(
"{text} carries no port, and the port is required; write it out, as in {}",