From 90aa957ca9b7c217af7569009d1675e0f3ff8e9b Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Thu, 26 Jun 2025 20:47:45 +0100 Subject: ucl, config: use monadic error handling more --- nihil.config/store.cc | 92 +++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 44 deletions(-) (limited to 'nihil.config/store.cc') diff --git a/nihil.config/store.cc b/nihil.config/store.cc index 2ec8ade..6f93677 100644 --- a/nihil.config/store.cc +++ b/nihil.config/store.cc @@ -5,6 +5,7 @@ module; #include +#include #include #include #include @@ -15,75 +16,78 @@ import nihil; namespace nihil::config { -unknown_option::unknown_option(std::string_view option_name) - : error(std::format("unknown configuration variable '{}'", - option_name)) - , _option_name(option_name) -{ -} +store::store() = default; -auto unknown_option::option_name(this unknown_option const &self) --> std::string_view +auto store::get() -> store & { - return self._option_name; -} - -auto store::get() --> store& -{ - if (instance == nullptr) - instance = new store; - - return *instance; + static auto instance = store(); + return instance; } auto store::register_option(this store &self, option *object) --> void + -> std::expected { - auto [it, okay] = self.options.insert( + auto [it, okay] = self.m_options.insert( std::pair{object->name(), object}); - if (!okay) - throw error(std::format( - "INTERNAL ERROR: attempt to register " - "duplicate config value '{0}'", - object->name())); + if (okay) + return {}; + + return std::unexpected(error(std::format( + "attempt to register duplicate " + "configuration option '{0}'", + object->name()))); } auto store::unregister_option(this store &self, option *object) --> void + -> std::expected { - auto it = self.options.find(object->name()); - if (it == self.options.end()) - throw error(std::format( - "INTERNAL ERROR: attempt to unregister " - "non-existent config value '{}'", - object->name())); - - self.options.erase(it); + auto it = self.m_options.find(object->name()); + if (it == self.m_options.end()) + return std::unexpected(error(std::format( + "attempt to unregister non-existent " + "configuration option '{}'", + object->name()))); + + self.m_options.erase(it); + return {}; } auto store::fetch(this store const &self, std::string_view name) --> option & + -> std::expected