Browse Source

master: helper funcs with const for default config values

ipv6
Denis Drakhnia 10 months ago
parent
commit
1b1b41d646
  1. 54
      master/src/config.rs

54
master/src/config.rs

@ -17,9 +17,23 @@ pub const DEFAULT_CONFIG_PATH: &str = "config/main.toml";
pub const DEFAULT_MASTER_SERVER_IP: IpAddr = IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)); pub const DEFAULT_MASTER_SERVER_IP: IpAddr = IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0));
pub const DEFAULT_MASTER_SERVER_PORT: u16 = 27010; pub const DEFAULT_MASTER_SERVER_PORT: u16 = 27010;
pub const DEFAULT_CHALLENGE_TIMEOUT: u32 = 10; pub const DEFAULT_CHALLENGE_TIMEOUT: u32 = 10;
pub const DEFAULT_TIMEOUT: u32 = 300; pub const DEFAULT_SERVER_TIMEOUT: u32 = 300;
pub const DEFAULT_ADMIN_TIMEOUT: u32 = 10; pub const DEFAULT_ADMIN_TIMEOUT: u32 = 10;
pub const DEFAULT_HASH_LEN: usize = admin::HASH_LEN;
macro_rules! impl_helpers {
($($f:ident: $t:ty),+$(,)?) => (
$(const fn $f<const N: $t>() -> $t { N })+
);
}
impl_helpers! {
default_u16: u16,
default_u32: u32,
default_usize: usize,
}
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum Error { pub enum Error {
#[error(transparent)] #[error(transparent)]
@ -65,7 +79,7 @@ impl Default for LogConfig {
pub struct ServerConfig { pub struct ServerConfig {
#[serde(default = "default_server_ip")] #[serde(default = "default_server_ip")]
pub ip: IpAddr, pub ip: IpAddr,
#[serde(default = "default_server_port")] #[serde(default = "default_u16::<DEFAULT_MASTER_SERVER_PORT>")]
pub port: u16, pub port: u16,
#[serde(default)] #[serde(default)]
pub timeout: TimeoutConfig, pub timeout: TimeoutConfig,
@ -75,7 +89,7 @@ impl Default for ServerConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
ip: default_server_ip(), ip: default_server_ip(),
port: default_server_port(), port: DEFAULT_MASTER_SERVER_PORT,
timeout: Default::default(), timeout: Default::default(),
} }
} }
@ -84,20 +98,20 @@ impl Default for ServerConfig {
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct TimeoutConfig { pub struct TimeoutConfig {
#[serde(default = "default_challenge_timeout")] #[serde(default = "default_u32::<DEFAULT_CHALLENGE_TIMEOUT>")]
pub challenge: u32, pub challenge: u32,
#[serde(default = "default_timeout")] #[serde(default = "default_u32::<DEFAULT_SERVER_TIMEOUT>")]
pub server: u32, pub server: u32,
#[serde(default = "default_admin_timeout")] #[serde(default = "default_u32::<DEFAULT_ADMIN_TIMEOUT>")]
pub admin: u32, pub admin: u32,
} }
impl Default for TimeoutConfig { impl Default for TimeoutConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
challenge: default_challenge_timeout(), challenge: DEFAULT_CHALLENGE_TIMEOUT,
server: default_timeout(), server: DEFAULT_SERVER_TIMEOUT,
admin: default_admin_timeout(), admin: DEFAULT_ADMIN_TIMEOUT,
} }
} }
} }
@ -119,7 +133,7 @@ pub struct ClientConfig {
#[derive(Deserialize, Default, Debug)] #[derive(Deserialize, Default, Debug)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct HashConfig { pub struct HashConfig {
#[serde(default = "default_hash_len")] #[serde(default = "default_usize::<DEFAULT_HASH_LEN>")]
pub len: usize, pub len: usize,
#[serde(default = "default_hash_key")] #[serde(default = "default_hash_key")]
pub key: Box<str>, pub key: Box<str>,
@ -142,26 +156,6 @@ fn default_server_ip() -> IpAddr {
DEFAULT_MASTER_SERVER_IP DEFAULT_MASTER_SERVER_IP
} }
fn default_server_port() -> u16 {
DEFAULT_MASTER_SERVER_PORT
}
fn default_challenge_timeout() -> u32 {
DEFAULT_CHALLENGE_TIMEOUT
}
fn default_timeout() -> u32 {
DEFAULT_TIMEOUT
}
fn default_admin_timeout() -> u32 {
DEFAULT_ADMIN_TIMEOUT
}
fn default_hash_len() -> usize {
admin::HASH_LEN
}
fn default_hash_key() -> Box<str> { fn default_hash_key() -> Box<str> {
Box::from(admin::HASH_KEY) Box::from(admin::HASH_KEY)
} }

Loading…
Cancel
Save