You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.2 KiB

//! Socket handling
use super::*;
use std::str;
use std::path::{
Path, PathBuf
};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SocketAddrUnix
{
pub path: PathBuf,
}
impl str::FromStr for SocketAddrUnix
{
type Err = AddrParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let path = Path::new(s);
if path.exists() && !path.is_dir() {
Ok(Self{path: path.into()})
} else {
Err(AddrParseError)
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum SocketAddr
{
Unix(SocketAddrUnix),
IP(std::net::SocketAddr),
}
impl From<std::net::SocketAddr> for SocketAddr
{
fn from(from: std::net::SocketAddr) -> Self
{
Self::IP(from)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AddrParseError;
impl From<std::net::AddrParseError> for AddrParseError
{
fn from(_: std::net::AddrParseError) -> Self
{
Self
}
}
const UNIX_SOCK_PREFIX: &str = "unix:/";
impl str::FromStr for SocketAddr
{
type Err = AddrParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(if s.starts_with(UNIX_SOCK_PREFIX) {
Self::Unix(s[(UNIX_SOCK_PREFIX.len())..].parse()?)
} else {
Self::IP(s.parse()?)
})
}
}