aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.config
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-25 21:15:34 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-25 21:15:34 +0100
commit257b6719ed89104018f6a521f6141ecd3eec3fd9 (patch)
tree590217da5f5131494ee3f002b44da25fa600a96d /nihil.config
parent0fc4b95e63370b9131c4e76f5f7137a4764dc341 (diff)
downloadnihil-257b6719ed89104018f6a521f6141ecd3eec3fd9.tar.gz
nihil-257b6719ed89104018f6a521f6141ecd3eec3fd9.tar.bz2
nihil: add an error type
Diffstat (limited to 'nihil.config')
-rw-r--r--nihil.config/read.cc13
-rw-r--r--nihil.config/read.ccm8
-rw-r--r--nihil.config/write.cc15
-rw-r--r--nihil.config/write.ccm11
4 files changed, 32 insertions, 15 deletions
diff --git a/nihil.config/read.cc b/nihil.config/read.cc
index d55703d..3c20566 100644
--- a/nihil.config/read.cc
+++ b/nihil.config/read.cc
@@ -4,6 +4,7 @@
module;
+#include <expected>
#include <filesystem>
#include <format>
#include <iterator>
@@ -16,7 +17,8 @@ module nihil.config;
namespace nihil::config {
-auto read_from(std::filesystem::path const &filename) -> void
+auto read_from(std::filesystem::path const &filename)
+ -> std::expected<void, nihil::error>
{
// TODO: nihil.ucl should have a way to load UCL from a filename.
@@ -26,10 +28,9 @@ auto read_from(std::filesystem::path const &filename) -> void
// 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()));
+ return {};
+ auto errstr = std::format("cannot read {}", filename.string());
+ return std::unexpected(nihil::error(errstr, err.error()));
}
// Parse the UCL.
@@ -52,6 +53,8 @@ auto read_from(std::filesystem::path const &filename) -> void
throw error(std::format("{}: {}", filename.string(),
err.what()));
}
+
+ return {};
}
} // namespace nihil::config
diff --git a/nihil.config/read.ccm b/nihil.config/read.ccm
index 74b0bc0..18f7213 100644
--- a/nihil.config/read.ccm
+++ b/nihil.config/read.ccm
@@ -4,15 +4,19 @@
module;
+#include <expected>
#include <filesystem>
+import nihil;
+
export module nihil.config:read;
namespace nihil::config {
/*
- * Load the configuration from a file. Throws config::error on failure.
+ * Load the configuration from a file.
*/
-export auto read_from(std::filesystem::path const &filename) -> void;
+export [[nodiscard]] auto read_from(std::filesystem::path const &filename)
+ -> std::expected<void, nihil::error>;
} // namespace nihil::config
diff --git a/nihil.config/write.cc b/nihil.config/write.cc
index 4b2a232..2b451bd 100644
--- a/nihil.config/write.cc
+++ b/nihil.config/write.cc
@@ -4,6 +4,7 @@
module;
+#include <expected>
#include <filesystem>
#include <format>
#include <utility>
@@ -15,7 +16,8 @@ module nihil.config;
namespace nihil::config {
-auto write_to(std::filesystem::path const &filename) -> void
+auto write_to(std::filesystem::path const &filename)
+ -> std::expected<void, nihil::error>
try {
auto uclconfig = ucl::map<ucl::object>();
@@ -30,10 +32,15 @@ try {
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()));
+ return std::unexpected(nihil::error(
+ std::format("cannot write {}", filename.string()),
+ ret.error()));
+
+ return {};
} catch (ucl::error const &exc) {
- throw error(std::format("{}: {}", filename.string(), exc.what()));
+ return std::unexpected(nihil::error(
+ "failed to serialize configuration",
+ nihil::error(exc.what())));
}
};
diff --git a/nihil.config/write.ccm b/nihil.config/write.ccm
index 71cdbb3..1a07dd7 100644
--- a/nihil.config/write.ccm
+++ b/nihil.config/write.ccm
@@ -4,16 +4,19 @@
module;
+#include <expected>
#include <filesystem>
+import nihil;
+
export module nihil.config:write;
-namespace nihil::config {
+export namespace nihil::config {
/*
- * Write all config values (except defaults) to disk. Throws config::error
- * on failure.
+ * Write all config values (except defaults) to disk.
*/
-auto write_to(std::filesystem::path const &filename) -> void;
+auto write_to(std::filesystem::path const &filename) ->
+ std::expected<void, nihil::error>;
};