From 3a002fb9c23fc9a6384bc1b30a8e364924bb574e Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Tue, 7 Oct 2025 21:46:03 +0200 Subject: cfg: Make load less magic Don't try to guess from name, instead, have one method for only loading one config from a file (that might be relative) and loading a config from config dirs using a name (that might contain slash). --- src/cfg.cc | 16 +++++++++------- src/cfg.hh | 13 +++++++++---- test/cfg.cc | 12 ++++++------ 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/cfg.cc b/src/cfg.cc index 0196bde..b65246f 100644 --- a/src/cfg.cc +++ b/src/cfg.cc @@ -182,16 +182,18 @@ std::optional Config::get_bool(std::string_view name) const { return std::nullopt; } -std::unique_ptr Config::load(std::string_view name, - std::vector& errors) { - if (name.empty() || name.front() == '/') { - auto ret = std::make_unique(); - ret->load(name, errors); - return ret; - } +std::unique_ptr load_all(std::string_view name, + std::vector& errors) { auto ret = std::make_unique(); ret->load(name, errors); return ret; } +std::unique_ptr load_one(std::filesystem::path const& path, + std::vector& errors) { + auto ret = std::make_unique(); + ret->load(path, errors); + return ret; +} + } // namespace cfg diff --git a/src/cfg.hh b/src/cfg.hh index a4a7865..3be9a00 100644 --- a/src/cfg.hh +++ b/src/cfg.hh @@ -1,6 +1,7 @@ #ifndef CFG_HH #define CFG_HH +#include #include #include #include @@ -24,10 +25,6 @@ class Config { [[nodiscard]] std::optional get_bool(std::string_view name) const; - [[nodiscard]] - static std::unique_ptr load(std::string_view name, - std::vector& errors); - protected: Config() = default; @@ -35,6 +32,14 @@ class Config { Config& operator=(Config const&) = delete; }; +[[nodiscard]] +std::unique_ptr load_all(std::string_view name, + std::vector& errors); + +[[nodiscard]] +std::unique_ptr load_one(std::filesystem::path const& path, + std::vector& errors); + } // namespace cfg #endif // CFG_HH diff --git a/test/cfg.cc b/test/cfg.cc index 80ebba9..0c27989 100644 --- a/test/cfg.cc +++ b/test/cfg.cc @@ -22,7 +22,7 @@ class ConfigTest : public TestEnv { TEST_F(ConfigTest, empty) { auto does_not_exist = dir_.path() / "does-not-exist"; std::vector errors; - auto cfg = cfg::Config::load(does_not_exist.string(), errors); + auto cfg = cfg::load_one(does_not_exist, errors); ASSERT_TRUE(cfg); EXPECT_EQ(1, errors.size()); @@ -46,7 +46,7 @@ TEST_F(ConfigTest, values) { << "i2 = -12313\n"; } std::vector errors; - auto cfg = cfg::Config::load(file.string(), errors); + auto cfg = cfg::load_one(file, errors); ASSERT_TRUE(cfg); EXPECT_EQ(0, errors.size()); @@ -90,7 +90,7 @@ TEST_F(ConfigTest, bools) { << "key5=ja\n"; } std::vector errors; - auto cfg = cfg::Config::load(file.string(), errors); + auto cfg = cfg::load_one(file, errors); ASSERT_TRUE(cfg); EXPECT_EQ(0, errors.size()); @@ -110,7 +110,7 @@ TEST_F(ConfigTest, errors) { << "key=duplicate\n"; } std::vector errors; - auto cfg = cfg::Config::load(file.string(), errors); + auto cfg = cfg::load_one(file, errors); ASSERT_TRUE(cfg); EXPECT_EQ(2, errors.size()); } @@ -137,7 +137,7 @@ TEST_F(ConfigTest, merge) { << "key3 = value3"; } std::vector errors; - auto cfg = cfg::Config::load("file", errors); + auto cfg = cfg::load_all("file", errors); ASSERT_TRUE(cfg); EXPECT_EQ(0, errors.size()); @@ -173,7 +173,7 @@ TEST_F(ConfigTest, merge_errors) { out << "invalid line"; } std::vector errors; - auto cfg = cfg::Config::load("file", errors); + auto cfg = cfg::load_all("file", errors); ASSERT_TRUE(cfg); EXPECT_EQ(1, errors.size()); -- cgit v1.2.3-70-g09d2