-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathopenrpc.rs
More file actions
184 lines (161 loc) · 4.84 KB
/
Copy pathopenrpc.rs
File metadata and controls
184 lines (161 loc) · 4.84 KB
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
use std::borrow::Cow;
use documented::Documented;
use jsonrpsee::core::{JsonValue, RpcResult};
use schemars::{JsonSchema, Schema, SchemaGenerator, generate::SchemaSettings};
use serde::Serialize;
// Imports to work around deficiencies in the build script.
#[cfg(zallet_build = "wallet")]
use super::{super::asyncop::OperationId, recover_accounts, z_send_many};
// See `generate_rpc_help()` in `build.rs` for how this is generated.
include!(concat!(env!("OUT_DIR"), "/rpc_openrpc.rs"));
/// Response to an `rpc.discover` RPC request.
pub(crate) type Response = RpcResult<ResultType>;
pub(crate) type ResultType = OpenRpc;
pub(crate) fn call() -> Response {
let mut generator = Generator::new();
let methods = METHODS
.into_iter()
.map(|(name, method)| method.generate(&mut generator, name))
.collect();
Ok(OpenRpc {
openrpc: "1.3.2",
info: Info {
title: "Zallet",
description: crate::build::PKG_DESCRIPTION,
version: crate::build::PKG_VERSION,
},
methods,
components: generator.into_components(),
})
}
/// Static information about a Zallet JSON-RPC method.
pub(super) struct RpcMethod {
pub(super) description: &'static str,
params: fn(&mut Generator) -> Vec<ContentDescriptor>,
result: fn(&mut Generator) -> ContentDescriptor,
deprecated: bool,
}
impl RpcMethod {
fn generate(&self, generator: &mut Generator, name: &'static str) -> Method {
let description = self.description.trim();
Method {
name,
summary: description
.split_once('\n')
.map(|(summary, _)| summary)
.unwrap_or(description),
description,
params: (self.params)(generator),
result: (self.result)(generator),
deprecated: self.deprecated,
}
}
}
/// An OpenRPC document generator.
pub(super) struct Generator {
inner: SchemaGenerator,
}
impl Generator {
fn new() -> Self {
Self {
inner: SchemaSettings::draft07()
.with(|s| {
s.definitions_path = "#/components/schemas/".into();
})
.into_generator(),
}
}
/// Constructs the descriptor for a JSON-RPC method parameter.
pub(super) fn param<T: JsonSchema>(
&mut self,
name: &'static str,
description: &'static str,
required: bool,
) -> ContentDescriptor {
ContentDescriptor {
name,
summary: description
.split_once('\n')
.map(|(summary, _)| summary)
.unwrap_or(description),
description,
required,
schema: self.inner.subschema_for::<T>(),
deprecated: false,
}
}
/// Constructs the descriptor for a JSON-RPC method's result type.
pub(super) fn result<T: Documented + JsonSchema>(
&mut self,
name: &'static str,
) -> ContentDescriptor {
ContentDescriptor {
name,
summary: T::DOCS
.split_once('\n')
.map(|(summary, _)| summary)
.unwrap_or(T::DOCS),
description: T::DOCS,
required: false,
schema: self.inner.subschema_for::<T>(),
deprecated: false,
}
}
fn into_components(mut self) -> Components {
Components {
schemas: self.inner.take_definitions(false),
}
}
}
/// An OpenRPC document.
#[derive(Clone, Debug, Serialize, Documented)]
pub(crate) struct OpenRpc {
openrpc: &'static str,
info: Info,
methods: Vec<Method>,
components: Components,
}
impl JsonSchema for OpenRpc {
fn schema_name() -> Cow<'static, str> {
Cow::Borrowed("OpenRPC Schema")
}
fn json_schema(_: &mut SchemaGenerator) -> Schema {
Schema::new_ref(
"https://raw.githubusercontent.com/open-rpc/meta-schema/master/schema.json".into(),
)
}
}
#[derive(Clone, Debug, Serialize)]
struct Info {
title: &'static str,
description: &'static str,
version: &'static str,
}
#[derive(Clone, Debug, Serialize)]
pub(super) struct Method {
name: &'static str,
summary: &'static str,
description: &'static str,
params: Vec<ContentDescriptor>,
result: ContentDescriptor,
#[serde(skip_serializing_if = "is_false")]
deprecated: bool,
}
#[derive(Clone, Debug, Serialize)]
pub(super) struct ContentDescriptor {
name: &'static str,
summary: &'static str,
description: &'static str,
#[serde(skip_serializing_if = "is_false")]
required: bool,
schema: Schema,
#[serde(skip_serializing_if = "is_false")]
deprecated: bool,
}
#[derive(Clone, Debug, Serialize)]
struct Components {
schemas: serde_json::Map<String, JsonValue>,
}
fn is_false(b: &bool) -> bool {
!b
}