use std::net::SocketAddr;
use socket2::{SockAddr, Type};
use crate::ICMP;
#[derive(Debug)]
pub struct Config {
pub sock_type_hint: Type,
pub kind: ICMP,
pub bind: Option<SockAddr>,
pub interface: Option<String>,
pub ttl: Option<u32>,
pub fib: Option<u32>,
}
impl Default for Config {
fn default() -> Self {
Self {
sock_type_hint: Type::DGRAM,
kind: ICMP::default(),
bind: None,
interface: None,
ttl: None,
fib: None,
}
}
}
impl Config {
pub fn new() -> Self {
Self::default()
}
pub fn builder() -> ConfigBuilder {
ConfigBuilder::default()
}
}
#[derive(Debug)]
pub struct ConfigBuilder {
sock_type_hint: Type,
kind: ICMP,
bind: Option<SockAddr>,
interface: Option<String>,
ttl: Option<u32>,
fib: Option<u32>,
}
impl Default for ConfigBuilder {
fn default() -> Self {
Self {
sock_type_hint: Type::DGRAM,
kind: ICMP::default(),
bind: None,
interface: None,
ttl: None,
fib: None,
}
}
}
impl ConfigBuilder {
pub fn bind(mut self, bind: SocketAddr) -> Self {
self.bind = Some(SockAddr::from(bind));
self
}
pub fn interface(mut self, interface: &str) -> Self {
self.interface = Some(interface.to_string());
self
}
pub fn ttl(mut self, ttl: u32) -> Self {
self.ttl = Some(ttl);
self
}
pub fn fib(mut self, fib: u32) -> Self {
self.fib = Some(fib);
self
}
pub fn kind(mut self, kind: ICMP) -> Self {
self.kind = kind;
self
}
pub fn sock_type_hint(mut self, typ: Type) -> Self {
self.sock_type_hint = typ;
self
}
pub fn build(self) -> Config {
Config {
sock_type_hint: self.sock_type_hint,
kind: self.kind,
bind: self.bind,
interface: self.interface,
ttl: self.ttl,
fib: self.fib,
}
}
}