aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.config/read.cc
blob: d55703d17993f06a7fa5149550c2d34760c1b1a6 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * This source code is released into the public domain.
 */

module;

#include <filesystem>
#include <format>
#include <iterator>
#include <string>

import nihil;
import nihil.ucl;

module nihil.config;

namespace nihil::config {

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