minisql/server/src/config.rs

41 lines
1 KiB
Rust

use clap::Parser;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::PathBuf;
use std::time::Duration;
const LOCAL_IPV4: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
#[derive(Debug, Parser)]
#[command(author, version, about)]
pub struct Configuration {
#[arg(short, long, default_value_t = LOCAL_IPV4, help = "IP address for the server to listen on")]
address: IpAddr,
#[arg(
short,
long,
default_value = "5432",
help = "Port for the server to listen on"
)]
port: u16,
#[arg(short, long, help = "Path to the data file")]
file: PathBuf,
#[arg(short, long, help = "Delay between rows in milliseconds")]
throttle: Option<u64>,
}
impl Configuration {
#[inline]
pub fn get_socket_address(&self) -> SocketAddr {
SocketAddr::new(self.address, self.port)
}
#[inline]
pub fn get_file_path(&self) -> &PathBuf {
&self.file
}
#[inline]
pub fn get_throttle(&self) -> Option<Duration> {
self.throttle.map(|d| Duration::from_millis(d))
}
}