summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2025-10-07 21:46:03 +0200
committerJoel Klinghed <the_jk@spawned.biz>2025-10-19 00:13:47 +0200
commit3a002fb9c23fc9a6384bc1b30a8e364924bb574e (patch)
tree7cfb59964bc20bbb14d157426b2aad4481deb90d
parent62a4abb9bf6551417130c3c6f9bba147930895ef (diff)
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).
-rw-r--r--src/cfg.cc16
-rw-r--r--src/cfg.hh13
-rw-r--r--test/cfg.cc12
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<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
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 <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());