minisql/server/src/config.rs
2024-02-05 21:59:33 +01:00

41 lines
1.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 folder for database data")]
folder: 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_folder_path(&self) -> &PathBuf {
&self.folder
}
#[inline]
pub fn get_throttle(&self) -> Option<Duration> {
self.throttle.map(|d| Duration::from_millis(d))
}
}