summaryrefslogtreecommitdiff
path: root/src/server/address.rs
blob: 1cf49acd8bf745bd05a4aa2b228c85854b49b0a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

use std::path::PathBuf;
use std::net::SocketAddr;
use std::str::FromStr;
use crate::{
	Result,
	aerr,
	util::AnyError
};
use super::tcpserver::TcpServer;
use super::unixserver::UnixServer;
use super::Server;

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Address {
	Inet(SocketAddr),
	Unix(PathBuf)
}

impl Address {
	pub fn to_server(&self) -> Result<Box<dyn Server>> {
		match self {
			Address::Inet(addr) => Ok(Box::new(TcpServer::new(addr)?)),
			Address::Unix(path) => Ok(Box::new(UnixServer::new(path)?)),
		}
	}
}

impl FromStr for Address {
	type Err = AnyError;
	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
		let parts: Vec<&str> = s.splitn(2, ':').collect();
		if parts.len() != 2 {
			return Err(aerr!("Address string has the wrong length!"));
		}
		let typename = parts[0];
		let text = parts[1];
		match typename {
			"inet" => Ok(Address::Inet(text.parse()?)),
			"unix" => Ok(Address::Unix(PathBuf::new().join(text))),
			"abstract" => Ok(Address::Unix(PathBuf::new().join(&format!("\0{}", text)))),
			_ => Err(aerr!("'{}' is not a valid address type", typename))
		}
	}
}