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/read.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/read.cc')
| -rw-r--r-- | nihil.config/read.cc | 35 |
1 files changed, 11 insertions, 24 deletions
diff --git a/nihil.config/read.cc b/nihil.config/read.cc index 0a5fcad..e7def91 100644 --- a/nihil.config/read.cc +++ b/nihil.config/read.cc @@ -11,15 +11,15 @@ module; #include <iterator> #include <string> +module nihil.config; + import nihil; import nihil.ucl; -module nihil.config; - namespace nihil::config { auto read_from(std::filesystem::path const &filename) - -> std::expected<void, nihil::error> + -> std::expected<void, error> { // TODO: nihil.ucl should have a way to load UCL from a filename. @@ -29,33 +29,20 @@ auto read_from(std::filesystem::path const &filename) // Ignore ENOENT, it simply means we haven't created the // config file yet, so default values will be used. if (err.error().root_cause() == std::errc::no_such_file_or_directory) - return {}; + co_return {}; auto errstr = std::format("cannot read {}", filename.string()); - return std::unexpected(nihil::error(errstr, err.error())); + co_return std::unexpected(error(errstr, err.error())); } // Parse the UCL. - try { - auto uclconfig = ucl::parse(config_text); - - for (auto &&[key, value] : uclconfig) { - auto &opt = store::get().fetch(key); - opt.from_ucl(value); - } - } catch (unknown_option const &) { - // This is probably an old option which has been removed; - // ignore it, and we'll remove the bad option next time - // we write the config. - } catch (error const &err) { - // Include the filename in any other config errors. - throw error(std::format("{}: {}", filename.string(), - err.what())); - } catch (ucl::error const &err) { - throw error(std::format("{}: {}", filename.string(), - err.what())); + auto uclconfig = co_await ucl::parse(config_text); + + for (auto &&[key, value] : uclconfig) { + auto opt = co_await store::get().fetch(key); + co_await opt->ucl(value); } - return {}; + co_return {}; } } // namespace nihil::config |
