From 1b1b41d646be85b0efdbac6fddc6171f36ec2b08 Mon Sep 17 00:00:00 2001 From: Denis Drakhnia Date: Sun, 28 Jan 2024 08:01:42 +0200 Subject: [PATCH] master: helper funcs with const for default config values --- master/src/config.rs | 54 ++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/master/src/config.rs b/master/src/config.rs index afa52b5..79821eb 100644 --- a/master/src/config.rs +++ b/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_PORT: u16 = 27010; 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_HASH_LEN: usize = admin::HASH_LEN; + +macro_rules! impl_helpers { + ($($f:ident: $t:ty),+$(,)?) => ( + $(const fn $f() -> $t { N })+ + ); +} + +impl_helpers! { + default_u16: u16, + default_u32: u32, + default_usize: usize, +} + #[derive(Debug, Error)] pub enum Error { #[error(transparent)] @@ -65,7 +79,7 @@ impl Default for LogConfig { pub struct ServerConfig { #[serde(default = "default_server_ip")] pub ip: IpAddr, - #[serde(default = "default_server_port")] + #[serde(default = "default_u16::")] pub port: u16, #[serde(default)] pub timeout: TimeoutConfig, @@ -75,7 +89,7 @@ impl Default for ServerConfig { fn default() -> Self { Self { ip: default_server_ip(), - port: default_server_port(), + port: DEFAULT_MASTER_SERVER_PORT, timeout: Default::default(), } } @@ -84,20 +98,20 @@ impl Default for ServerConfig { #[derive(Deserialize, Debug)] #[serde(deny_unknown_fields)] pub struct TimeoutConfig { - #[serde(default = "default_challenge_timeout")] + #[serde(default = "default_u32::")] pub challenge: u32, - #[serde(default = "default_timeout")] + #[serde(default = "default_u32::")] pub server: u32, - #[serde(default = "default_admin_timeout")] + #[serde(default = "default_u32::")] pub admin: u32, } impl Default for TimeoutConfig { fn default() -> Self { Self { - challenge: default_challenge_timeout(), - server: default_timeout(), - admin: default_admin_timeout(), + challenge: DEFAULT_CHALLENGE_TIMEOUT, + server: DEFAULT_SERVER_TIMEOUT, + admin: DEFAULT_ADMIN_TIMEOUT, } } } @@ -119,7 +133,7 @@ pub struct ClientConfig { #[derive(Deserialize, Default, Debug)] #[serde(deny_unknown_fields)] pub struct HashConfig { - #[serde(default = "default_hash_len")] + #[serde(default = "default_usize::")] pub len: usize, #[serde(default = "default_hash_key")] pub key: Box, @@ -142,26 +156,6 @@ fn default_server_ip() -> IpAddr { 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 { Box::from(admin::HASH_KEY) }