aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.config/string.cc
blob: 7d0c038b2c6ee1c5eb6a4885f7eb3d0386cb2a58 (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
/*
 * This source code is released into the public domain.
 */

module;

#include <coroutine>
#include <expected>
#include <format>
#include <string>

module nihil.config;

import nihil;
import nihil.ucl;

namespace nihil::config {

string::string(
	std::string &storage,
	std::string_view name,
	std::string_view description) noexcept
	: option(name, description)
	, m_storage(storage)
{
}

string::~string() = default;

auto string::get_string() const -> std::string
{
	return m_storage;
}

auto string::set_string(std::string_view new_value)
	-> std::expected<void, error>
{
	m_storage = new_value;
	return {};
}

auto string::get_ucl() const -> std::expected<ucl::object, error>
{
	return ucl::string(m_storage);
}

auto string::set_ucl(ucl::object const &uclobj) -> std::expected<void, error>
{
	auto obj = co_await object_cast<ucl::string>(uclobj)
		.transform_error([&] (ucl::type_mismatch const &m) {
			return error(std::format(
				"'{}': expected string, not {}",
				name(), str(m.actual_type())));
		});

	m_storage = obj.value();
	is_default(false);
	co_return {};
}

} // namespace nihil::config