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

module;

#include <format>
#include <string>

export module nihil.config:string;

import nihil;
import nihil.ucl;
import :option;
import :store;

namespace nihil::config {

export struct string final : option
{
	string(std::string &storage,
	       std::string_view name,
	       std::string_view description) noexcept
		: option(name, description)
		, _storage(storage)
	{
		store::get().register_option(this);
	}

	~string()
	{
		store::get().unregister_option(this);
	}

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

	auto set_string(std::string_view new_value) -> void override
	{
		_storage = new_value;
	}

	auto to_ucl() const -> ucl::object override
	{
		return ucl::string(_storage);
	}

	auto from_ucl(ucl::object const &uclobj) -> void override
	{
		try {
			_storage = object_cast<ucl::string>(uclobj).value();
			is_default(false);
		} catch (ucl::type_mismatch const &exc) {
			throw error(std::format("'{}': expected string, not {}",
				    name(), str(exc.actual_type())));
		}
	}

private:
	std::string &_storage;
};

} // namespace nihil::config