aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.config/write.cc
blob: 8c02c435c601a809685ea8e935c9a6ddfd01aaa8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * This source code is released into the public domain.
 */

module;

#include <coroutine>
#include <expected>
#include <filesystem>
#include <format>
#include <utility>

module nihil.config;

import nihil;
import nihil.ucl;

namespace nihil::config {

auto write_to(std::filesystem::path const &filename)
	-> std::expected<void, error>
{
	auto uclconfig = ucl::map<ucl::object>();

	// 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 {};
}

};