From 2e2d1bd3b6c7776b77c33b94f30ead89367a71e6 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Tue, 1 Jul 2025 17:07:04 +0100 Subject: add nihil.std --- nihil.config/write.ccm | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'nihil.config/write.ccm') diff --git a/nihil.config/write.ccm b/nihil.config/write.ccm index 564bb20..a7eddd5 100644 --- a/nihil.config/write.ccm +++ b/nihil.config/write.ccm @@ -1,22 +1,34 @@ -/* - * This source code is released into the public domain. - */ - -module; - -#include -#include - +// This source code is released into the public domain. export module nihil.config:write; +import nihil.std; import nihil.error; +import nihil.monad; +import nihil.posix; +import nihil.ucl; +import :option; +import :store; namespace nihil::config { -/* - * Write all config values (except defaults) to disk. - */ -export [[nodiscard]] auto write_to(std::filesystem::path const &filename) -> - std::expected; +// Write all config values (except defaults) to disk. +export [[nodiscard]] auto +write_to(std::filesystem::path const &filename) -> std::expected +{ + auto uclconfig = ucl::map(); + + // Add all the options to the UCL object. + for (auto const &option : store::get().all()) { + if (option->is_default()) + continue; + + auto uobj = co_await option->ucl(); + uclconfig.insert({option->name(), uobj}); + } + + auto ucl_text = std::format("{:c}", uclconfig); + co_await safe_write_file(filename, ucl_text); + co_return {}; +} -}; +}; // namespace nihil::config -- cgit v1.2.3