diff options
| author | Lexi Winter <lexi@le-fay.org> | 2025-07-01 17:07:04 +0100 |
|---|---|---|
| committer | Lexi Winter <lexi@le-fay.org> | 2025-07-01 17:07:04 +0100 |
| commit | 2e2d1bd3b6c7776b77c33b94f30ead89367a71e6 (patch) | |
| tree | 54d37ffadf8e677938d9b7a28e4e9b71be1e75c1 /nihil.config/read.ccm | |
| parent | 36427c0966faa7aecd586b397ed9b845f18172f5 (diff) | |
| download | nihil-2e2d1bd3b6c7776b77c33b94f30ead89367a71e6.tar.gz nihil-2e2d1bd3b6c7776b77c33b94f30ead89367a71e6.tar.bz2 | |
add nihil.std
Diffstat (limited to 'nihil.config/read.ccm')
| -rw-r--r-- | nihil.config/read.ccm | 47 |
1 files changed, 33 insertions, 14 deletions
diff --git a/nihil.config/read.ccm b/nihil.config/read.ccm index 9cf28c9..7065492 100644 --- a/nihil.config/read.ccm +++ b/nihil.config/read.ccm @@ -1,22 +1,41 @@ -/* - * This source code is released into the public domain. - */ - -module; - -#include <expected> -#include <filesystem> - +// This source code is released into the public domain. export module nihil.config:read; +import nihil.std; import nihil.error; +import nihil.monad; +import nihil.posix; +import nihil.ucl; +import :option; +import :store; namespace nihil::config { -/* - * Load the configuration from a file. - */ -export [[nodiscard]] auto read_from(std::filesystem::path const &filename) - -> std::expected<void, error>; +// Load the configuration from a file. +export [[nodiscard]] auto +read_from(std::filesystem::path const &filename) -> std::expected<void, error> +{ + // TODO: nihil.ucl should have a way to load UCL from a filename. + + auto config_text = std::string(); + auto err = read_file(filename, std::back_inserter(config_text)); + if (!err) { + // 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) + co_return {}; + co_return std::unexpected(error(std::format("cannot read {}", filename.string()), err.error())); + } + + // Parse the UCL. + 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); + } + + co_return {}; +} } // namespace nihil::config |
