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 {
48 template <SomeProcessor... Processors>
49 FieldSpec(std::string const& key, Processors&&... processors)
50 : processor_{impl::makeFieldProcessor<Processors...>(key, std::forward<Processors>(processors)...)}
51 , checker_{impl::kEMPTY_FIELD_CHECKER}
52 {
53 }
54
62 template <SomeCheck... Checks>
63 FieldSpec(std::string const& key, Checks&&... checks)
64 : processor_{impl::kEMPTY_FIELD_PROCESSOR}
65 , checker_{impl::makeFieldChecker<Checks...>(key, std::forward<Checks>(checks)...)}
66 {
67 }
68
75 [[nodiscard]] MaybeError
76 process(boost::json::value& value) const;
77
84 [[nodiscard]] check::Warnings
85 check(boost::json::value const& value) const;
86
87private:
88 impl::FieldSpecProcessor processor_;
89 impl::FieldChecker checker_;
90};
91
98struct RpcSpec final {
104 RpcSpec(std::initializer_list<FieldSpec> fields) : fields_{fields}
105 {
106 }
107
114 RpcSpec(RpcSpec const& other, std::initializer_list<FieldSpec> additionalFields) : fields_{other.fields_}
115 {
116 for (auto& f : additionalFields)
117 fields_.push_back(f);
118 }
119
126 [[nodiscard]] MaybeError
127 process(boost::json::value& value) const;
128
135 [[nodiscard]] boost::json::array
136 check(boost::json::value const& value) const;
137
138private:
139 std::vector<FieldSpec> fields_;
140};
141
146
147} // 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:36
RpcSpec const & RpcSpecConstRef
An alias for a const reference to RpcSpec.
Definition Specs.hpp:145
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
Represents a Specification for one field of an RPC command.
Definition Specs.hpp:40
FieldSpec(std::string const &key, Checks &&... checks)
Construct a field specification out of a set of checkers.
Definition Specs.hpp:63
MaybeError process(boost::json::value &value) const
Processes the passed JSON value using the stored processors.
Definition Specs.cpp:37
FieldSpec(std::string const &key, Processors &&... processors)
Construct a field specification out of a set of processors.
Definition Specs.hpp:49
Represents a Specification of an entire RPC command.
Definition Specs.hpp:98
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:104
RpcSpec(RpcSpec const &other, std::initializer_list< FieldSpec > additionalFields)
Construct a full RPC request specification from another spec and additional fields.
Definition Specs.hpp:114