diff options
| -rw-r--r-- | src/cfg.cc | 16 | ||||
| -rw-r--r-- | src/cfg.hh | 13 | ||||
| -rw-r--r-- | test/cfg.cc | 12 |
3 files changed, 24 insertions, 17 deletions
@@ -182,16 +182,18 @@ std::optional<bool> Config::get_bool(std::string_view name) const { return std::nullopt; } -std::unique_ptr<Config> Config::load(std::string_view name, - std::vector<std::string>& errors) { - if (name.empty() || name.front() == '/') { - auto ret = std::make_unique<ConfigSingleImpl>(); - ret->load(name, errors); - return ret; - } +std::unique_ptr<Config> load_all(std::string_view name, + std::vector<std::string>& errors) { auto ret = std::make_unique<ConfigXdgImpl>(); ret->load(name, errors); return ret; } +std::unique_ptr<Config> load_one(std::filesystem::path const& path, + std::vector<std::string>& errors) { + auto ret = std::make_unique<ConfigSingleImpl>(); + ret->load(path, errors); + return ret; +} + } // namespace cfg @@ -1,6 +1,7 @@ #ifndef CFG_HH #define CFG_HH +#include <filesystem> #include <memory> #include <optional> #include <string_view> @@ -24,10 +25,6 @@ class Config { [[nodiscard]] std::optional<bool> get_bool(std::string_view name) const; - [[nodiscard]] - static std::unique_ptr<Config> load(std::string_view name, - std::vector<std::string>& errors); - protected: Config() = default; @@ -35,6 +32,14 @@ class Config { Config& operator=(Config const&) = delete; }; +[[nodiscard]] +std::unique_ptr<Config> load_all(std::string_view name, + std::vector<std::string>& errors); + +[[nodiscard]] +std::unique_ptr<Config> load_one(std::filesystem::path const& path, + std::vector<std::string>& 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<std::string> 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<std::string> 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<std::string> 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<std::string> 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<std::string> 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<std::string> errors; - auto cfg = cfg::Config::load("file", errors); + auto cfg = cfg::load_all("file", errors); ASSERT_TRUE(cfg); EXPECT_EQ(1, errors.size()); |
