diff options
| author | Lexi Winter <lexi@le-fay.org> | 2025-06-23 16:28:11 +0100 |
|---|---|---|
| committer | Lexi Winter <lexi@le-fay.org> | 2025-06-23 16:28:11 +0100 |
| commit | d5963532328ce5f1c9f266bf7e760b7d18a60c15 (patch) | |
| tree | 28e8d4b98f2f3adbd2f02bcc656ad74e626677c9 /nihil.config/read.ccm | |
| parent | 0fa623093366351ad47583f47add6e51f56a56d8 (diff) | |
| download | nihil-d5963532328ce5f1c9f266bf7e760b7d18a60c15.tar.gz nihil-d5963532328ce5f1c9f266bf7e760b7d18a60c15.tar.bz2 | |
various updates
Diffstat (limited to 'nihil.config/read.ccm')
| -rw-r--r-- | nihil.config/read.ccm | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/nihil.config/read.ccm b/nihil.config/read.ccm new file mode 100644 index 0000000..8d6c202 --- /dev/null +++ b/nihil.config/read.ccm @@ -0,0 +1,63 @@ +/* + * This source code is released into the public domain. + */ + +module; + +#include <filesystem> +#include <format> +#include <iterator> +#include <string> + +export module nihil.config:read; + +import nihil; +import nihil.ucl; + +import :error; +import :store; + +namespace nihil::config { + +/* + * Load the configuration from a file. Throws config::error on failure. + */ +export auto read_from(std::filesystem::path const &filename) -> void +{ + // TODO: nihil.ucl should have a way to load UCL from a filename. + + std::string config_text; + 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() == std::errc::no_such_file_or_directory) + return; + throw error(std::format("{}: {}", + filename.string(), + err.error().message())); + } + + // 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())); + } +} + +} // namespace nihil::config |
