From 9b8485c4947dca62b2cade041cef14bc6e2b91ad Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 25 Jun 2025 12:46:53 +0300 Subject: [PATCH] validate unique arguments count --- src/session/storage.rs | 2 +- src/session/storage/list_config.rs | 32 +++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/session/storage.rs b/src/session/storage.rs index 2e38e40..af22636 100644 --- a/src/session/storage.rs +++ b/src/session/storage.rs @@ -31,7 +31,7 @@ impl Storage { bail!("Symlinks yet not supported!"); } Ok(Self { - list_config: ListConfig::init(config), + list_config: ListConfig::init(config)?, public_dir, read_chunk: config.read_chunk, }) diff --git a/src/session/storage/list_config.rs b/src/session/storage/list_config.rs index 55320cf..044912b 100644 --- a/src/session/storage/list_config.rs +++ b/src/session/storage/list_config.rs @@ -34,8 +34,34 @@ pub struct ListConfig { } impl ListConfig { - pub fn init(config: &crate::config::Config) -> Self { - Self { + pub fn init(config: &crate::config::Config) -> anyhow::Result { + use anyhow::bail; + fn is_unique(args: &[bool]) -> bool { + let mut c = 0; + for a in args { + if *a { + c += 1 + } + } + c <= 1 + } + if !is_unique(&[ + config.list_dir_sort_accessed, + config.list_dir_sort_created, + config.list_dir_sort_modified, + config.list_dir_sort_count, + ]) { + bail!("Dir sort option should be unique!") + } + if !is_unique(&[ + config.list_file_sort_accessed, + config.list_file_sort_created, + config.list_file_sort_modified, + config.list_file_sort_size, + ]) { + bail!("File sort option should be unique!") + } + Ok(Self { dir: Dir { time: Time { is_accessed: config.list_dir_accessed, @@ -71,6 +97,6 @@ impl ListConfig { }, }, time_format: config.list_time_format.clone(), - } + }) } }