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/string.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/string.cc')
| -rw-r--r-- | nihil.config/string.cc | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/nihil.config/string.cc b/nihil.config/string.cc index 6b201ae..7d0c038 100644 --- a/nihil.config/string.cc +++ b/nihil.config/string.cc @@ -4,11 +4,14 @@ module; +#include <coroutine> +#include <expected> #include <format> #include <string> module nihil.config; +import nihil; import nihil.ucl; namespace nihil::config { @@ -18,40 +21,41 @@ string::string( std::string_view name, std::string_view description) noexcept : option(name, description) - , _storage(storage) + , m_storage(storage) { - store::get().register_option(this); } -string::~string() -{ - store::get().unregister_option(this); -} +string::~string() = default; auto string::get_string() const -> std::string { - return _storage; + return m_storage; } -auto string::set_string(std::string_view new_value) -> void +auto string::set_string(std::string_view new_value) + -> std::expected<void, error> { - _storage = new_value; + m_storage = new_value; + return {}; } -auto string::to_ucl() const -> ucl::object +auto string::get_ucl() const -> std::expected<ucl::object, error> { - return ucl::string(_storage); + return ucl::string(m_storage); } -auto string::from_ucl(ucl::object const &uclobj) -> void +auto string::set_ucl(ucl::object const &uclobj) -> std::expected<void, error> { - try { - _storage = object_cast<ucl::string>(uclobj).value(); - is_default(false); - } catch (ucl::type_mismatch const &exc) { - throw error(std::format("'{}': expected string, not {}", - name(), str(exc.actual_type()))); - } + auto obj = co_await object_cast<ucl::string>(uclobj) + .transform_error([&] (ucl::type_mismatch const &m) { + return error(std::format( + "'{}': expected string, not {}", + name(), str(m.actual_type()))); + }); + + m_storage = obj.value(); + is_default(false); + co_return {}; } } // namespace nihil::config |
