aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.config/option.cc
blob: 588a48fc16df974328781804132456094d60d2ab (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * This source code is released into the public domain.
 */

module;

#include <coroutine>
#include <expected>
#include <iostream>
#include <string>

module nihil.config;

import nihil;
import nihil.ucl;

namespace nihil::config {

option::option(std::string_view name, std::string_view description)
	: m_name(name)
	, m_description(description)
{
	auto okay = store::get().register_option(this);
	if (okay)
		return;

	std::print(std::cerr,
		   "INTERNAL ERROR: failed to register "
		   "configuration option '{}': {}",
		   m_name, okay.error());
	std::exit(1);
}

option::~option()
{
	std::ignore = store::get().unregister_option(this);
}

auto option::name(this option const &self) noexcept
	-> std::string_view
{
	return self.m_name;
}

auto option::description(this option const &self) noexcept
	-> std::string_view
{
	return self.m_description;
}

auto option::is_default(this option const &self) noexcept
	-> bool
{
	return self.m_is_default;
}

auto option::is_default(this option  &self, bool b) -> void
{
	self.m_is_default = b;
}

auto option::string(this option const &self) -> std::string
{
	return self.get_string();
}

auto option::string(this option &self, std::string_view value)
	-> std::expected<void, error>
{
	co_await self.set_string(value);
	self.is_default(false);
	co_return {};
}

auto option::ucl(this option const &self)
	-> std::expected<nihil::ucl::object, error>
{
	return self.get_ucl();
}

auto option::ucl(this option &self, nihil::ucl::object const &value)
	-> std::expected<void, error>
{
	co_await self.set_ucl(value);
	self.is_default(false);
	co_return {};
}

auto operator<<(std::ostream &strm, option const &opt)
-> std::ostream &
{
	return strm << "<" << opt.name() << "=" << opt.string() << ">";
}

} // namespace nihil