cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
parse_toml_common.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22// IWYU pragma: no_include <type_traits>
23
24#include <chrono> // IWYU pragma: keep
25#include <exception> // IWYU pragma: keep
26#include <string>
27#include <string_view>
28
29#include <fmt/format.h>
30#include <toml++/toml.h>
31
34
35namespace msgpack_rpc::config::toml::impl {
36
43[[noreturn]] inline void throw_error(
44 const ::toml::source_region& source, std::string_view config_key) {
45 const std::string_view source_path =
46 (source.path) ? *source.path : std::string_view("(unknown file)");
47 throw MsgpackRPCException(StatusCode::INVALID_ARGUMENT,
48 fmt::format("Invalid value for {}. (at {}:{})", config_key, source_path,
49 source.begin.line));
50}
51
59[[noreturn]] inline void throw_error(const ::toml::source_region& source,
60 std::string_view config_key, std::string_view error_message) {
61 const std::string_view source_path =
62 (source.path) ? *source.path : std::string_view("(unknown file)");
63 throw MsgpackRPCException(StatusCode::INVALID_ARGUMENT,
64 fmt::format("Invalid value for {}: {} (at {}:{})", config_key,
65 error_message, source_path, source.begin.line));
66}
67
69#define MSGPACK_RPC_PARSE_TOML_VALUE(KEY_STR, CONFIG_FUNCTION, TYPE) \
70 const auto config_value = value.value<TYPE>(); \
71 if (!config_value) { \
72 throw_error(value.source(), KEY_STR); \
73 } \
74 try { \
75 config.CONFIG_FUNCTION(*config_value); \
76 } catch (const std::exception& e) { \
77 throw_error(value.source(), KEY_STR, e.what()); \
78 }
79
81#define MSGPACK_RPC_PARSE_TOML_VALUE_DURATION(KEY_STR, CONFIG_FUNCTION) \
82 const auto config_value = value.value<double>(); \
83 if (!config_value) { \
84 throw_error(value.source(), KEY_STR); \
85 } \
86 try { \
87 config.CONFIG_FUNCTION( \
88 std::chrono::duration_cast<std::chrono::nanoseconds>( \
89 std::chrono::duration<double>(*config_value))); \
90 } catch (const std::exception& e) { \
91 throw_error(value.source(), KEY_STR, e.what()); \
92 }
93
94} // namespace msgpack_rpc::config::toml::impl
Class of exceptions in cpp-msgpack-rpc library.
Definition of MsgpackRPCException class.
void throw_error(const ::toml::source_region &source, std::string_view config_key)
Throw an exception for an error of TOML.
Definition of StatusCode enumeration.