From 1e9e182a326a24a9b2e2b426d0f30e3733e31762 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 10 Jul 2024 16:24:19 +0200 Subject: [PATCH] config/Data: merge the two duration parser methods, pass minimum value --- src/StateFileConfig.cxx | 3 ++- src/config/Data.cxx | 25 +++++-------------------- src/config/Data.hxx | 7 ++----- 3 files changed, 9 insertions(+), 26 deletions(-) diff --git a/src/StateFileConfig.cxx b/src/StateFileConfig.cxx index ac54052b1..22788d86e 100644 --- a/src/StateFileConfig.cxx +++ b/src/StateFileConfig.cxx @@ -10,7 +10,8 @@ StateFileConfig::StateFileConfig(const ConfigData &config) :path(config.GetPath(ConfigOption::STATE_FILE)), - interval(config.GetUnsigned(ConfigOption::STATE_FILE_INTERVAL, + interval(config.GetDuration(ConfigOption::STATE_FILE_INTERVAL, + std::chrono::seconds{1}, DEFAULT_INTERVAL)), restore_paused(config.GetBool(ConfigOption::RESTORE_PAUSED, false)) { diff --git a/src/config/Data.cxx b/src/config/Data.cxx index 202f2b674..72da3ba81 100644 --- a/src/config/Data.cxx +++ b/src/config/Data.cxx @@ -86,32 +86,17 @@ ConfigData::GetPositive(ConfigOption option, unsigned default_value) const } std::chrono::steady_clock::duration -ConfigData::GetUnsigned(ConfigOption option, +ConfigData::GetDuration(ConfigOption option, + std::chrono::steady_clock::duration min_value, std::chrono::steady_clock::duration default_value) const { - return With(option, [default_value](const char *s){ + return With(option, [min_value, default_value](const char *s){ if (s == nullptr) return default_value; auto value = ParseDuration(s); - if (value < std::chrono::steady_clock::duration{}) - throw std::runtime_error("Value must not be negative"); - - return value; - }); -} - -std::chrono::steady_clock::duration -ConfigData::GetPositive(ConfigOption option, - std::chrono::steady_clock::duration default_value) const -{ - return With(option, [default_value](const char *s){ - if (s == nullptr) - return default_value; - - auto value = ParseDuration(s); - if (value <= std::chrono::steady_clock::duration{}) - throw std::runtime_error("Value must be positive"); + if (value < min_value) + throw std::runtime_error{"Value is too small"}; return value; }); diff --git a/src/config/Data.hxx b/src/config/Data.hxx index 7718d71f1..cac7f3f4e 100644 --- a/src/config/Data.hxx +++ b/src/config/Data.hxx @@ -60,15 +60,12 @@ struct ConfigData { unsigned GetUnsigned(ConfigOption option, unsigned default_value) const; - std::chrono::steady_clock::duration - GetUnsigned(ConfigOption option, - std::chrono::steady_clock::duration default_value) const; - unsigned GetPositive(ConfigOption option, unsigned default_value) const; std::chrono::steady_clock::duration - GetPositive(ConfigOption option, + GetDuration(ConfigOption option, + std::chrono::steady_clock::duration min_value, std::chrono::steady_clock::duration default_value) const; bool GetBool(ConfigOption option, bool default_value) const;