From 257b6719ed89104018f6a521f6141ecd3eec3fd9 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Wed, 25 Jun 2025 21:15:34 +0100 Subject: nihil: add an error type --- nihil.config/read.cc | 13 ++++++++----- nihil.config/read.ccm | 8 ++++++-- nihil.config/write.cc | 15 +++++++++++---- nihil.config/write.ccm | 11 +++++++---- 4 files changed, 32 insertions(+), 15 deletions(-) (limited to 'nihil.config') 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 #include #include #include @@ -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 { // 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 #include +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; } // 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 #include #include #include @@ -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 try { auto uclconfig = ucl::map(); @@ -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 #include +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; }; -- cgit v1.2.3