aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.config
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-23 19:45:54 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-23 19:45:54 +0100
commit2a3f19ff67bd0292a2812993b2b19c292d113959 (patch)
tree81fa477e6c1470f77a3ddd12ee5804c7a315f2c3 /nihil.config
parent504c02086832dcaab5cfa042ee7a488470284424 (diff)
downloadnihil-2a3f19ff67bd0292a2812993b2b19c292d113959.tar.gz
nihil-2a3f19ff67bd0292a2812993b2b19c292d113959.tar.bz2
config: add separate implementation for read, write
Diffstat (limited to 'nihil.config')
-rw-r--r--nihil.config/CMakeLists.txt2
-rw-r--r--nihil.config/nihil.config.ccm1
-rw-r--r--nihil.config/read.cc57
-rw-r--r--nihil.config/read.ccm47
-rw-r--r--nihil.config/write.cc39
-rw-r--r--nihil.config/write.ccm29
6 files changed, 103 insertions, 72 deletions
diff --git a/nihil.config/CMakeLists.txt b/nihil.config/CMakeLists.txt
index 6b1073d..2aa0dae 100644
--- a/nihil.config/CMakeLists.txt
+++ b/nihil.config/CMakeLists.txt
@@ -15,8 +15,10 @@ target_sources(nihil.config
PRIVATE
option.cc
+ read.cc
store.cc
string.cc
+ write.cc
)
if(NIHIL_TESTS)
diff --git a/nihil.config/nihil.config.ccm b/nihil.config/nihil.config.ccm
index 4ada81c..d20c3fe 100644
--- a/nihil.config/nihil.config.ccm
+++ b/nihil.config/nihil.config.ccm
@@ -8,6 +8,7 @@ export module nihil.config;
export import :error;
export import :option;
+export import :read;
export import :store;
export import :string;
export import :write;
diff --git a/nihil.config/read.cc b/nihil.config/read.cc
new file mode 100644
index 0000000..d55703d
--- /dev/null
+++ b/nihil.config/read.cc
@@ -0,0 +1,57 @@
+/*
+ * This source code is released into the public domain.
+ */
+
+module;
+
+#include <filesystem>
+#include <format>
+#include <iterator>
+#include <string>
+
+import nihil;
+import nihil.ucl;
+
+module nihil.config;
+
+namespace nihil::config {
+
+auto read_from(std::filesystem::path const &filename) -> void
+{
+ // TODO: nihil.ucl should have a way to load UCL from a filename.
+
+ std::string config_text;
+ auto err = read_file(filename, std::back_inserter(config_text));
+ if (!err) {
+ // Ignore ENOENT, it simply means we haven't created the
+ // config file yet, so default values will be used.
+ if (err.error() == std::errc::no_such_file_or_directory)
+ return;
+ throw error(std::format("{}: {}",
+ filename.string(),
+ err.error().message()));
+ }
+
+ // Parse the UCL.
+ try {
+ auto uclconfig = ucl::parse(config_text);
+
+ for (auto &&[key, value] : uclconfig) {
+ auto &opt = store::get().fetch(key);
+ opt.from_ucl(value);
+ }
+ } catch (unknown_option const &) {
+ // This is probably an old option which has been removed;
+ // ignore it, and we'll remove the bad option next time
+ // we write the config.
+ } catch (error const &err) {
+ // Include the filename in any other config errors.
+ throw error(std::format("{}: {}", filename.string(),
+ err.what()));
+ } catch (ucl::error const &err) {
+ throw error(std::format("{}: {}", filename.string(),
+ err.what()));
+ }
+}
+
+} // namespace nihil::config
diff --git a/nihil.config/read.ccm b/nihil.config/read.ccm
index 8d6c202..74b0bc0 100644
--- a/nihil.config/read.ccm
+++ b/nihil.config/read.ccm
@@ -5,59 +5,14 @@
module;
#include <filesystem>
-#include <format>
-#include <iterator>
-#include <string>
export module nihil.config:read;
-import nihil;
-import nihil.ucl;
-
-import :error;
-import :store;
-
namespace nihil::config {
/*
* Load the configuration from a file. Throws config::error on failure.
*/
-export auto read_from(std::filesystem::path const &filename) -> void
-{
- // TODO: nihil.ucl should have a way to load UCL from a filename.
-
- std::string config_text;
- auto err = read_file(filename, std::back_inserter(config_text));
- if (!err) {
- // Ignore ENOENT, it simply means we haven't created the
- // config file yet, so default values will be used.
- if (err.error() == std::errc::no_such_file_or_directory)
- return;
- throw error(std::format("{}: {}",
- filename.string(),
- err.error().message()));
- }
-
- // Parse the UCL.
- try {
- auto uclconfig = ucl::parse(config_text);
-
- for (auto &&[key, value] : uclconfig) {
- auto &opt = store::get().fetch(key);
- opt.from_ucl(value);
- }
- } catch (unknown_option const &) {
- // This is probably an old option which has been removed;
- // ignore it, and we'll remove the bad option next time
- // we write the config.
- } catch (error const &err) {
- // Include the filename in any other config errors.
- throw error(std::format("{}: {}", filename.string(),
- err.what()));
- } catch (ucl::error const &err) {
- throw error(std::format("{}: {}", filename.string(),
- err.what()));
- }
-}
+export auto read_from(std::filesystem::path const &filename) -> void;
} // namespace nihil::config
diff --git a/nihil.config/write.cc b/nihil.config/write.cc
new file mode 100644
index 0000000..4b2a232
--- /dev/null
+++ b/nihil.config/write.cc
@@ -0,0 +1,39 @@
+/*
+ * This source code is released into the public domain.
+ */
+
+module;
+
+#include <filesystem>
+#include <format>
+#include <utility>
+
+import nihil;
+import nihil.ucl;
+
+module nihil.config;
+
+namespace nihil::config {
+
+auto write_to(std::filesystem::path const &filename) -> void
+try {
+ auto uclconfig = ucl::map<ucl::object>();
+
+ // Add all the options to the UCL object.
+ for (auto const &option : store::get().all()) {
+ if (option.is_default())
+ continue;
+
+ uclconfig.insert({option.name(), option.to_ucl()});
+ }
+
+ auto ucl_text = std::format("{:c}", uclconfig);
+ auto ret = safe_write_file(filename, ucl_text);
+ if (!ret)
+ throw error(std::format("{}: {}", filename.string(),
+ ret.error().message()));
+} catch (ucl::error const &exc) {
+ throw error(std::format("{}: {}", filename.string(), exc.what()));
+}
+
+};
diff --git a/nihil.config/write.ccm b/nihil.config/write.ccm
index 947c7ee..71cdbb3 100644
--- a/nihil.config/write.ccm
+++ b/nihil.config/write.ccm
@@ -5,38 +5,15 @@
module;
#include <filesystem>
-#include <format>
-#include <utility>
export module nihil.config:write;
-import nihil.ucl;
-import :store;
-
namespace nihil::config {
/*
- * Write all config values (except defaults) to disk.
+ * Write all config values (except defaults) to disk. Throws config::error
+ * on failure.
*/
-auto write_to(std::filesystem::path const &filename) -> void
-try {
- auto uclconfig = ucl::map<ucl::object>();
-
- // Add all the options to the UCL object.
- for (auto const &option : store::get().all()) {
- if (option.is_default())
- continue;
-
- uclconfig.insert({option.name(), option.to_ucl()});
- }
-
- auto ucl_text = std::format("{:c}", uclconfig);
- auto ret = safe_write_file(filename, ucl_text);
- if (!ret)
- throw error(std::format("{}: {}", filename.string(),
- ret.error().message()));
-} catch (ucl::error const &exc) {
- throw error(std::format("{}: {}", filename.string(), exc.what()));
-}
+auto write_to(std::filesystem::path const &filename) -> void;
};