From d27d1302d1fa1b96bf8f53f17fce947f19d21330 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Sat, 21 Jun 2025 17:18:57 +0100 Subject: add nihil.config (incomplete) --- nihil.config/CMakeLists.txt | 11 +++ nihil.config/error.ccm | 26 ++++++ nihil.config/nihil.config.ccm | 12 +++ nihil.config/option.ccm | 84 +++++++++++++++++++ nihil.config/store.ccm | 183 ++++++++++++++++++++++++++++++++++++++++++ nihil.config/string.ccm | 54 +++++++++++++ 6 files changed, 370 insertions(+) create mode 100644 nihil.config/CMakeLists.txt create mode 100644 nihil.config/error.ccm create mode 100644 nihil.config/nihil.config.ccm create mode 100644 nihil.config/option.ccm create mode 100644 nihil.config/store.ccm create mode 100644 nihil.config/string.ccm (limited to 'nihil.config') diff --git a/nihil.config/CMakeLists.txt b/nihil.config/CMakeLists.txt new file mode 100644 index 0000000..d96b116 --- /dev/null +++ b/nihil.config/CMakeLists.txt @@ -0,0 +1,11 @@ +# This source code is released into the public domain. + +add_library(nihil.config STATIC) +target_sources(nihil.config PUBLIC + FILE_SET modules TYPE CXX_MODULES FILES + nihil.config.ccm + error.ccm + store.ccm + option.ccm + string.ccm) +target_link_libraries(nihil.config PUBLIC nihil nihil.ucl) diff --git a/nihil.config/error.ccm b/nihil.config/error.ccm new file mode 100644 index 0000000..0da91cb --- /dev/null +++ b/nihil.config/error.ccm @@ -0,0 +1,26 @@ +/* + * This source code is released into the public domain. + */ + +module; + +#include +#include + +export module nihil.config:error; + +import nihil; + +namespace nihil::config { + +/* + * Exception thrown when an issue occurs with the configuration. + */ +export struct error : generic_error { + template + error(std::format_string fmt, Args &&...args) + : generic_error(fmt, std::forward(args)...) + {} +}; + +} // namespace nihil::config diff --git a/nihil.config/nihil.config.ccm b/nihil.config/nihil.config.ccm new file mode 100644 index 0000000..0b12885 --- /dev/null +++ b/nihil.config/nihil.config.ccm @@ -0,0 +1,12 @@ +/* + * This source code is released into the public domain. + */ + +module; + +export module nihil.config; + +export import :error; +export import :option; +export import :store; +export import :string; diff --git a/nihil.config/option.ccm b/nihil.config/option.ccm new file mode 100644 index 0000000..207eb65 --- /dev/null +++ b/nihil.config/option.ccm @@ -0,0 +1,84 @@ +/* + * This source code is released into the public domain. + */ + +module; + +#include + +#include + +export module nihil.config:option; + +import nihil; +import :error; + +namespace nihil::config { + +/* + * Base class for options; this is what config_store interacts with. + */ + +export struct option +{ + // Short name of this option. + auto name(this option const &self) noexcept -> std::string_view + { + return self._name; + } + + // Human-readable description of this option. + auto description(this option const &self) noexcept -> std::string_view + { + return self._description; + } + + // If true, this option is set to its default value. + auto is_default(this option const &self) noexcept -> bool + { + return self._is_default; + } + + // Get or set this option as a string. + auto string(this option const &self) -> std::string + { + return self.get_string(); + } + + void string(this option &self, std::string_view value) + { + self.set_string(value); + self._is_default = false; + } + + /* + * Add this option to a UCL object. This is used when writing the + * configuration file. + */ + virtual void add_to_ucl(ucl_object_t *) const = 0; + + // Not copyable or movable. + option(option const &) = delete; + auto operator=(option const &) -> option& = delete; + +protected: + option(std::string_view name, + std::string_view description) + : _name(name) + , _description(description) + { + } + + /* + * Get or set this option as a string. + */ + virtual auto get_string() const -> std::string = 0; + virtual auto set_string(std::string_view) -> void = 0; + +private: + std::string _name; + std::string _description; + bool _is_default = true; +}; + +} // namespace nihil diff --git a/nihil.config/store.ccm b/nihil.config/store.ccm new file mode 100644 index 0000000..7ed4ccb --- /dev/null +++ b/nihil.config/store.ccm @@ -0,0 +1,183 @@ +/* + * This source code is released into the public domain. + */ + +module; + +/* + * The configuration store. There should only be one of these. + */ + +#include +#include +#include + +export module nihil.config:store; + +import nihil; +import :error; +import :option; + +namespace nihil::config { + +// Exception thrown on an attempt to fetch an undefined option. +export struct unknown_option final : error { + std::string varname; + + unknown_option(std::string_view varname_) + : error("unknown configuration variable '{}'", varname_) + , varname(varname_) + {} +}; + +export struct store final { + /* + * Get the global config store. + */ + static auto get() -> store& { + if (instance == nullptr) + instance = new store; + + return *instance; + } + + /* + * Initialise the global config store. + */ +#if 0 + void init(context const &ctx) { + std::string config_text; + + // Load the configuration text. + auto config_path = ctx.dbdir / "config.ucl"; + try { + read_file(config_path, std::back_inserter(config_text)); + } catch (io_error const &exc) { + // Ignore ENOENT, it simply means we haven't created the + // config file yet, so default values will be used. + if (exc.error == std::errc::no_such_file_or_directory) + return; + throw; + } + + // Parse the UCL. + + std::string err; + auto uclconfig = ucl::Ucl::parse(config_text, err); + + if (!uclconfig) + throw error("{0}: {1}", config_path, err); + + auto const &cfg = get(); + for (auto const &uclvalue : uclconfig) { + auto &value = cfg.fetch(uclvalue.key()); + + switch (uclvalue.type()) { + case UCL_INT: + value.integer(uclvalue.int_value()); + break; + case UCL_STRING: + value.string(uclvalue.string_value()); + break; + default: + throw error( + "INTERNAL ERROR: unknown value type {0}", + static_cast(uclvalue.type())); + } + } + } +#endif + + /* + * Register a new value with the config store. + */ + auto register_option(this store &self, option *object) -> void + { + auto [it, okay] = self.options.insert( + std::pair{object->name(), object}); + + if (okay) + return; + + throw error("INTERNAL ERROR: attempt to register " + "duplicate config value '{0}'", + object->name()); + } + + /* + * Fetch an existing value in the config store. + */ + auto fetch(this store const &self, std::string_view name) + -> option & + { + if (auto it = self.options.find(name); it != self.options.end()) + return *it->second; + + throw unknown_option(name); + } + + /* + * Fetch all values in the configuration store. + */ + auto all(this auto &&self) -> nihil::generator