Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Specs.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "rpc/common/Checkers.hpp"
23#include "rpc/common/Concepts.hpp"
24#include "rpc/common/Types.hpp"
25#include "rpc/common/impl/Factories.hpp"
26
27#include <boost/json/array.hpp>
28#include <boost/json/value.hpp>
29
30#include <initializer_list>
31#include <string>
32#include <utility>
33#include <vector>
34
35namespace rpc {
36
40struct FieldSpec final {
49 template <SomeProcessor... Processors>
50 FieldSpec(std::string const& key, Processors&&... processors)
51 : processor_{impl::makeFieldProcessor<Processors...>(
52 key,
53 std::forward<Processors>(processors)...
54 )}
55 , checker_{impl::kEMPTY_FIELD_CHECKER}
56 {
57 }
58
66 template <SomeCheck... Checks>
67 FieldSpec(std::string const& key, Checks&&... checks)
68 : processor_{impl::kEMPTY_FIELD_PROCESSOR}
69 , checker_{impl::makeFieldChecker<Checks...>(key, std::forward<Checks>(checks)...)}
70 {
71 }
72
79 [[nodiscard]] MaybeError
80 process(boost::json::value& value) const;
81
88 [[nodiscard]] check::Warnings
89 check(boost::json::value const& value) const;
90
91private:
92 impl::FieldSpecProcessor processor_;
93 impl::FieldChecker checker_;
94};
95
102struct RpcSpec final {
108 RpcSpec(std::initializer_list<FieldSpec> fields) : fields_{fields}
109 {
110 }
111
118 RpcSpec(RpcSpec const& other, std::initializer_list<FieldSpec> additionalFields)
119 : fields_{other.fields_}
120 {
121 for (auto& f : additionalFields)
122 fields_.push_back(f);
123 }
124
131 [[nodiscard]] MaybeError
132 process(boost::json::value& value) const;
133
140 [[nodiscard]] boost::json::array
141 check(boost::json::value const& value) const;
142
143private:
144 std::vector<FieldSpec> fields_;
145};
146
151
152} // namespace rpc
Specifies what a check used with rpc::FieldSpec must provide.
Definition Concepts.hpp:58
The requirements of a processor to be used with rpc::FieldSpec.
Definition Concepts.hpp:66
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:37
RpcSpec const & RpcSpecConstRef
An alias for a const reference to RpcSpec.
Definition Specs.hpp:150
std::expected< void, Status > MaybeError
Return type used for Validators that can return error but don't have specific value to return.
Definition Types.hpp:55
FieldSpec(std::string const &key, Checks &&... checks)
Construct a field specification out of a set of checkers.
Definition Specs.hpp:67
MaybeError process(boost::json::value &value) const
Processes the passed JSON value using the stored processors.
Definition Specs.cpp:37
check::Warnings check(boost::json::value const &value) const
Checks the passed JSON value using the stored checkers.
Definition Specs.cpp:43
FieldSpec(std::string const &key, Processors &&... processors)
Construct a field specification out of a set of processors.
Definition Specs.hpp:50
Represents a Specification of an entire RPC command.
Definition Specs.hpp:102
MaybeError process(boost::json::value &value) const
Processes the passed JSON value using the stored field specs.
Definition Specs.cpp:49
RpcSpec(std::initializer_list< FieldSpec > fields)
Construct a full RPC request specification.
Definition Specs.hpp:108
RpcSpec(RpcSpec const &other, std::initializer_list< FieldSpec > additionalFields)
Construct a full RPC request specification from another spec and additional fields.
Definition Specs.hpp:118
boost::json::array check(boost::json::value const &value) const
Checks the passed JSON value using the stored field specs.
Definition Specs.cpp:60