diff options
| author | Lexi Winter <lexi@le-fay.org> | 2025-06-26 20:47:45 +0100 |
|---|---|---|
| committer | Lexi Winter <lexi@le-fay.org> | 2025-06-26 20:47:45 +0100 |
| commit | 90aa957ca9b7c217af7569009d1675e0f3ff8e9b (patch) | |
| tree | e6a61ca2b6928e6414372b9b1484ce80fa2fb0b3 /nihil.config/option.cc | |
| parent | 1db86c401df11423c945634d8b2a483e97afa878 (diff) | |
| download | nihil-90aa957ca9b7c217af7569009d1675e0f3ff8e9b.tar.gz nihil-90aa957ca9b7c217af7569009d1675e0f3ff8e9b.tar.bz2 | |
ucl, config: use monadic error handling more
Diffstat (limited to 'nihil.config/option.cc')
| -rw-r--r-- | nihil.config/option.cc | 70 |
1 files changed, 49 insertions, 21 deletions
diff --git a/nihil.config/option.cc b/nihil.config/option.cc index 9bf77c9..588a48f 100644 --- a/nihil.config/option.cc +++ b/nihil.config/option.cc @@ -4,58 +4,86 @@ module; +#include <coroutine> +#include <expected> #include <iostream> #include <string> module nihil.config; +import nihil; +import nihil.ucl; + namespace nihil::config { +option::option(std::string_view name, std::string_view description) + : m_name(name) + , m_description(description) +{ + auto okay = store::get().register_option(this); + if (okay) + return; + + std::print(std::cerr, + "INTERNAL ERROR: failed to register " + "configuration option '{}': {}", + m_name, okay.error()); + std::exit(1); +} + +option::~option() +{ + std::ignore = store::get().unregister_option(this); +} + auto option::name(this option const &self) noexcept --> std::string_view + -> std::string_view { - return self._name; + return self.m_name; } -// Human-readable description of this option. auto option::description(this option const &self) noexcept --> std::string_view + -> std::string_view { - return self._description; + return self.m_description; } -// If true, this option is set to its default value. auto option::is_default(this option const &self) noexcept --> bool + -> bool +{ + return self.m_is_default; +} + +auto option::is_default(this option &self, bool b) -> void { - return self._is_default; + self.m_is_default = b; } -// Get or set this option as a string. -auto option::string(this option const &self) --> std::string +auto option::string(this option const &self) -> std::string { return self.get_string(); } auto option::string(this option &self, std::string_view value) --> void + -> std::expected<void, error> { - self.set_string(value); - self._is_default = false; + co_await self.set_string(value); + self.is_default(false); + co_return {}; } -option::option(std::string_view name, - std::string_view description) - : _name(name) - , _description(description) +auto option::ucl(this option const &self) + -> std::expected<nihil::ucl::object, error> { + return self.get_ucl(); } -auto option::is_default(bool b) --> void +auto option::ucl(this option &self, nihil::ucl::object const &value) + -> std::expected<void, error> { - _is_default = b; + co_await self.set_ucl(value); + self.is_default(false); + co_return {}; } auto operator<<(std::ostream &strm, option const &opt) |
