aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.ucl/emit.ccm
blob: b88f8e7a9544dea75979827a4a7bdb8ccd19f226 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * This source code is released into the public domain.
 */

module;

#include <array>
#include <charconv>
#include <cstdlib>
#include <format>
#include <iterator>
#include <iosfwd>
#include <span>
#include <string>
#include <utility>

#include <ucl.h>

export module nihil.ucl:emit;

import :object;

namespace nihil::ucl {

export enum struct emitter {
	configuration = UCL_EMIT_CONFIG,
	compact_json = UCL_EMIT_JSON_COMPACT,
	json = UCL_EMIT_JSON,
	yaml = UCL_EMIT_YAML,
};

/*
 * Wrap ucl_emitter_functions for a particular output iterator type.
 *
 * We can't throw exceptions here since we're called from C code.  The emit
 * functions return an integer value, but it's not really clear what this is
 * for and the C API seems to mostly ignore it.  So, we just eat errors and
 * keep going.
 */
template<std::output_iterator<char> Iterator>
struct emit_wrapper {
	emit_wrapper(Iterator iterator_)
		: iterator(std::move(iterator_))
	{}

	static auto append_character(unsigned char c, std::size_t nchars,
				     void *ud)
		noexcept -> int
	try {
		auto *self = static_cast<emit_wrapper *>(ud);

		while (nchars--)
			*self->iterator++ = static_cast<char>(c);

		return 0;
	} catch (...) {
		return 0;
	}

	static auto append_len(unsigned char const *str, std::size_t len,
			       void *ud) 
		noexcept -> int
	try {
		auto *self = static_cast<emit_wrapper *>(ud);

		for (auto c : std::span(str, len))
			*self->iterator++ = static_cast<char>(c);

		return 0;
	} catch (...) {
		return 0;
	}

	static auto append_int(std::int64_t value, void *ud)
		noexcept -> int
	try {
		auto constexpr bufsize =
			std::numeric_limits<std::int64_t>::digits10;
		auto buf = std::array<char, bufsize>();

		auto *self = static_cast<emit_wrapper *>(ud);
		auto result = std::to_chars(buf.data(), buf.data() + buf.size(),
					    value, 10);

		if (result.ec == std::errc())
			for (auto c : std::span(buf.data(), result.ptr))
				*self->iterator++ = c;

		return 0;
	} catch (...) {
		return 0;
	}

	static auto append_double(double value, void *ud)
		noexcept -> int
	try {
		auto constexpr bufsize =
			std::numeric_limits<double>::digits10;
		auto buf = std::array<char, bufsize>();

		auto *self = static_cast<emit_wrapper *>(ud);
		auto result = std::to_chars(buf.data(), buf.data() + buf.size(),
					    value);

		if (result.ec == std::errc())
			for (auto c : std::span(buf.data(), result.ptr))
				*self->iterator++ = c;

		return 0;
	} catch (...) {
		return 0;
	}

	auto get_functions(this emit_wrapper &self) -> ucl_emitter_functions
	{
		auto ret = ucl_emitter_functions{};

		ret.ucl_emitter_append_character = &emit_wrapper::append_character;
		ret.ucl_emitter_append_len = &emit_wrapper::append_len;
		ret.ucl_emitter_append_int = &emit_wrapper::append_int;
		ret.ucl_emitter_append_double = &emit_wrapper::append_double;
		ret.ud = &self;

		return ret;
	}

private:
	Iterator iterator{};
};

export auto emit(object const &object, emitter format,
		 std::output_iterator<char> auto &&it)
	-> void
{
	auto ucl_format = static_cast<ucl_emitter>(format);
	auto wrapper = emit_wrapper(it);
	auto functions = wrapper.get_functions();

	::ucl_object_emit_full(object.get_ucl_object(), ucl_format,
			       &functions, nullptr);
}

/*
 * Basic ostream printer for UCL; default to JSON since it's probably what
 * most people expect.
 */
export auto operator<<(std::ostream &, object const &) -> std::ostream &;

} // namespace nihil::ucl

/*
 * Specialisation of std::formatter<> for object.
 */
template<std::derived_from<nihil::ucl::object> T>
struct std::formatter<T, char>
{
	nihil::ucl::emitter emitter = nihil::ucl::emitter::json;

	template<class ParseContext>
	constexpr ParseContext::iterator parse(ParseContext& ctx)
	{
		auto it = ctx.begin();
		auto end = ctx.end();

		while (it != end) {
			switch (*it) {
			case 'j':
				emitter = nihil::ucl::emitter::json;
				break;
			case 'J':
				emitter = nihil::ucl::emitter::compact_json;
				break;
			case 'c':
				emitter = nihil::ucl::emitter::configuration;
				break;
			case 'y':
				emitter = nihil::ucl::emitter::yaml;
				break;
			case '}':
				return it;
			default:
				throw std::format_error("Invalid format string "
							"for UCL object");
			}

			++it;
		}

		return it;
	}

	template<class FmtContext>
	FmtContext::iterator format(nihil::ucl::object const &o,
				    FmtContext& ctx) const
	{
		// We can't use emit() here since the context iterator is not
		// an std::output_iterator.

		auto out = ctx.out();

		auto ucl_format = static_cast<::ucl_emitter>(emitter);
		auto wrapper = nihil::ucl::emit_wrapper(out);
		auto functions = wrapper.get_functions();

		::ucl_object_emit_full(o.get_ucl_object(), ucl_format,
				       &functions, nullptr);
		return out;
	}
};