use super::{super::error::*, host::*};
use {
compris::resolve::*,
kutil::{
cli::depict::*,
io::network::ip::*,
std::{immutable::*, sync::*},
},
std::{io, net::*, path::*, vec},
};
#[derive(Clone, Debug, Depict, Resolve)]
pub struct Port {
#[depict(style(number))]
pub index: usize,
#[resolve]
#[depict(style(string))]
pub name: ByteString,
#[resolve]
#[depict(option, as(display), style(string))]
pub address: Option<IpAddr>,
#[resolve]
#[depict(option, style(string))]
pub zone: Option<ByteString>,
#[resolve]
#[depict(option, style(number))]
pub flowinfo: Option<u32>,
#[resolve(key = "include-loopbacks")]
#[depict(style(symbol))]
pub include_loopbacks: bool,
#[resolve]
#[depict(iter(item), as(depict))]
pub hosts: Vec<Host>,
}
static COUNTER: Counter = Counter::new();
impl Default for Port {
fn default() -> Self {
let index = COUNTER.next();
Self {
index,
name: index.to_string().into(),
address: None,
zone: None,
flowinfo: None,
include_loopbacks: true,
hosts: Default::default(),
}
}
}
impl Port {
pub fn validate<PathT>(&mut self, base_path: PathT) -> Result<(), ConfigurationError>
where
PathT: AsRef<Path>,
{
let base_path = base_path.as_ref();
for host in &mut self.hosts {
host.validate(base_path)?;
}
Ok(())
}
pub fn is_tls(&self) -> bool {
for host in &self.hosts {
if host.has_tls() {
return true;
}
}
false
}
pub fn listenable_port_configuration(&self, port: u16) -> ListenablePortConfiguration {
ListenablePortConfiguration::new(
port,
self.address,
self.zone.clone().map(|zone| zone.into()),
self.flowinfo,
false,
self.include_loopbacks,
)
}
pub fn socket_addresses(&self, port: u16) -> io::Result<vec::IntoIter<SocketAddr>> {
self.listenable_port_configuration(port).to_socket_addrs()
}
}