aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.config/store.cc
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-26 20:47:45 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-26 20:47:45 +0100
commit90aa957ca9b7c217af7569009d1675e0f3ff8e9b (patch)
treee6a61ca2b6928e6414372b9b1484ce80fa2fb0b3 /nihil.config/store.cc
parent1db86c401df11423c945634d8b2a483e97afa878 (diff)
downloadnihil-90aa957ca9b7c217af7569009d1675e0f3ff8e9b.tar.gz
nihil-90aa957ca9b7c217af7569009d1675e0f3ff8e9b.tar.bz2
ucl, config: use monadic error handling more
Diffstat (limited to 'nihil.config/store.cc')
-rw-r--r--nihil.config/store.cc92
1 files changed, 48 insertions, 44 deletions
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 <coroutine>
+#include <expected>
#include <filesystem>
#include <format>
#include <map>
@@ -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<void, error>
{
- 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<void, error>
{
- 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<option const *, error>
{
- if (auto it = self.options.find(name); it != self.options.end())
- return *it->second;
+ if (auto it = self.m_options.find(name); it != self.m_options.end())
+ return it->second;
- throw unknown_option(name);
+ return std::unexpected(error(std::format(
+ "unknown configuration option '{}'",
+ name)));
+}
+
+auto store::fetch(this store &self, std::string_view name)
+ -> std::expected<option *, error>
+{
+ auto opt = co_await static_cast<store const &>(self).fetch(name);
+ co_return const_cast<option *>(opt);
+}
+
+auto store::all(this store const &self) -> nihil::generator<option const *>
+{
+ for (auto &&it : self.m_options)
+ co_yield it.second;
}
-auto store::all(this store const &self)
--> nihil::generator<option const &>
+auto store::all(this store &self) -> nihil::generator<option *>
{
- for (auto &&it : self.options)
- co_yield *it.second;
+ for (auto &&it : self.m_options)
+ co_yield it.second;
}
auto get_option(std::string_view option_name)
--> option &
+ -> std::expected<option *, error>
{
- return store::get().fetch(option_name);
+ co_return co_await store::get().fetch(option_name);
}
} // namespace nihil::config