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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
use schemars::gen::SchemaSettings;
use schemars::schema::RootSchema;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
extern crate serde_json;
#[derive(Serialize, Deserialize, Clone)]
pub enum Openrpc {
#[serde(rename = "1.2.6")]
V26,
#[serde(rename = "1.2.5")]
V25,
#[serde(rename = "1.2.4")]
V24,
#[serde(rename = "1.2.3")]
V23,
#[serde(rename = "1.2.2")]
V22,
#[serde(rename = "1.2.1")]
V21,
#[serde(rename = "1.2.0")]
V20,
#[serde(rename = "1.1.12")]
V112,
#[serde(rename = "1.1.11")]
V111,
#[serde(rename = "1.1.10")]
V110,
#[serde(rename = "1.1.9")]
V19,
#[serde(rename = "1.1.8")]
V18,
#[serde(rename = "1.1.7")]
V17,
#[serde(rename = "1.1.6")]
V16,
#[serde(rename = "1.1.5")]
V15,
#[serde(rename = "1.1.4")]
V14,
#[serde(rename = "1.1.3")]
V13,
#[serde(rename = "1.1.2")]
V12,
#[serde(rename = "1.1.1")]
V11,
#[serde(rename = "1.1.0")]
V10,
#[serde(rename = "1.0.0")]
V00,
#[serde(rename = "1.0.0-rc1")]
V00Rc1,
#[serde(rename = "1.0.0-rc0")]
V00Rc0,
}
pub type InfoObjectProperties = String;
pub type InfoObjectDescription = String;
pub type InfoObjectTermsOfService = String;
pub type InfoObjectVersion = String;
pub type ContactObjectName = String;
pub type ContactObjectEmail = String;
pub type ContactObjectUrl = String;
pub type SpecificationExtension = serde_json::Value;
#[derive(Serialize, Deserialize, Clone)]
pub struct ContactObject {
pub name: Option<ContactObjectName>,
pub email: Option<ContactObjectEmail>,
pub url: Option<ContactObjectUrl>,
}
pub type LicenseObjectName = String;
pub type LicenseObjectUrl = String;
#[derive(Serialize, Deserialize, Clone)]
pub struct LicenseObject {
pub name: Option<LicenseObjectName>,
pub url: Option<LicenseObjectUrl>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct InfoObject {
pub title: InfoObjectProperties,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<InfoObjectDescription>,
#[serde(rename = "termsOfService")]
#[serde(skip_serializing_if = "Option::is_none")]
pub terms_of_service: Option<InfoObjectTermsOfService>,
pub version: InfoObjectVersion,
#[serde(skip_serializing_if = "Option::is_none")]
pub contact: Option<ContactObject>,
#[serde(skip_serializing_if = "Option::is_none")]
pub license: Option<LicenseObject>,
}
pub type ExternalDocumentationObjectDescription = String;
pub type ExternalDocumentationObjectUrl = String;
#[derive(Serialize, Deserialize, Clone)]
pub struct ExternalDocumentationObject {
pub description: Option<ExternalDocumentationObjectDescription>,
pub url: ExternalDocumentationObjectUrl,
}
pub type ServerObjectUrl = String;
pub type ServerObjectName = String;
pub type ServerObjectDescription = String;
pub type ServerObjectSummary = String;
pub type ServerObjectVariableDefault = String;
pub type ServerObjectVariableDescription = String;
pub type ServerObjectVariableEnumItem = String;
pub type ServerObjectVariableEnum = Vec<ServerObjectVariableEnumItem>;
#[derive(Serialize, Deserialize, Clone)]
pub struct ServerObjectVariable {
pub default: ServerObjectVariableDefault,
pub description: Option<ServerObjectVariableDescription>,
#[serde(rename = "enum")]
pub variable_enum: Option<ServerObjectVariableEnum>,
}
pub type ServerObjectVariables = HashMap<String, Option<serde_json::Value>>;
#[derive(Serialize, Deserialize, Clone)]
pub struct ServerObject {
pub url: ServerObjectUrl,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<ServerObjectName>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<ServerObjectDescription>,
#[serde(skip_serializing_if = "Option::is_none")]
pub summary: Option<ServerObjectSummary>,
#[serde(skip_serializing_if = "Option::is_none")]
pub variables: Option<ServerObjectVariables>,
}
pub type Servers = Vec<ServerObject>;
pub type MethodObjectName = String;
pub type MethodObjectDescription = String;
pub type MethodObjectSummary = String;
pub type TagObjectName = String;
pub type TagObjectDescription = String;
#[derive(Serialize, Deserialize, Clone)]
pub struct TagObject {
pub name: TagObjectName,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<TagObjectDescription>,
#[serde(rename = "externalDocs")]
#[serde(skip_serializing_if = "Option::is_none")]
pub external_docs: Option<ExternalDocumentationObject>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct ReferenceObject {
#[serde(rename = "$ref")]
pub reference: String,
}
#[derive(Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum TagOrReference {
TagObject(TagObject),
ReferenceObject(ReferenceObject),
}
pub type MethodObjectTags = Vec<TagOrReference>;
#[derive(Serialize, Deserialize, Clone)]
pub enum MethodObjectParamStructure {
#[serde(rename = "by-position")]
ByPosition,
#[serde(rename = "by-name")]
ByName,
#[serde(rename = "either")]
Either,
}
pub type ContentDescriptorObjectName = String;
pub type ContentDescriptorObjectDescription = String;
pub type ContentDescriptorObjectSummary = String;
pub type Id = String;
pub type Schema = String;
pub type Comment = String;
pub type Title = String;
pub type Description = String;
type AlwaysTrue = serde_json::Value;
pub type ReadOnly = bool;
pub type Examples = Vec<AlwaysTrue>;
pub type MultipleOf = f64;
pub type Maximum = f64;
pub type ExclusiveMaximum = f64;
pub type Minimum = f64;
pub type ExclusiveMinimum = f64;
pub type NonNegativeInteger = i64;
pub type NonNegativeIntegerDefaultZero = i64;
pub type Pattern = String;
pub type SchemaArray = Vec<JSONSchema>;
#[derive(Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum Items {
JSONSchema(JSONSchema),
SchemaArray(SchemaArray),
}
pub type UniqueItems = bool;
pub type StringDoaGddGA = String;
pub type StringArray = Vec<StringDoaGddGA>;
pub type Definitions = HashMap<String, Option<serde_json::Value>>;
pub type Properties = HashMap<String, Option<serde_json::Value>>;
pub type PatternProperties = HashMap<String, Option<serde_json::Value>>;
#[derive(Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum DependenciesSet {
JSONSchema(JSONSchema),
StringArray(StringArray),
}
pub type Dependencies = HashMap<String, Option<serde_json::Value>>;
pub type Enum = Vec<AlwaysTrue>;
pub type SimpleTypes = serde_json::Value;
pub type ArrayOfSimpleTypes = Vec<SimpleTypes>;
#[derive(Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum Type {
SimpleTypes(SimpleTypes),
ArrayOfSimpleTypes(ArrayOfSimpleTypes),
}
pub type Format = String;
pub type ContentMediaType = String;
pub type ContentEncoding = String;
#[derive(Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum JSONSchema {
JsonSchemaObject(RootSchema),
JSONSchemaBoolean(bool),
}
pub type ContentDescriptorObjectRequired = bool;
pub type ContentDescriptorObjectDeprecated = bool;
#[derive(Serialize, Deserialize, Clone)]
pub struct ContentDescriptorObject {
pub name: ContentDescriptorObjectName,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<ContentDescriptorObjectDescription>,
#[serde(skip_serializing_if = "Option::is_none")]
pub summary: Option<ContentDescriptorObjectSummary>,
pub schema: JSONSchema,
#[serde(skip_serializing_if = "Option::is_none")]
pub required: Option<ContentDescriptorObjectRequired>,
#[serde(skip_serializing_if = "Option::is_none")]
pub deprecated: Option<ContentDescriptorObjectDeprecated>,
}
#[derive(Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum ContentDescriptorOrReference {
ContentDescriptorObject(ContentDescriptorObject),
ReferenceObject(ReferenceObject),
}
pub type MethodObjectParams = Vec<ContentDescriptorOrReference>;
pub type ErrorObjectCode = i64;
pub type ErrorObjectMessage = String;
pub type ErrorObjectData = serde_json::Value;
#[derive(Serialize, Deserialize, Clone)]
pub struct ErrorObject {
pub code: ErrorObjectCode,
pub message: ErrorObjectMessage,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<ErrorObjectData>,
}
#[derive(Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum ErrorOrReference {
ErrorObject(ErrorObject),
ReferenceObject(ReferenceObject),
}
pub type MethodObjectErrors = Vec<ErrorOrReference>;
pub type LinkObjectName = String;
pub type LinkObjectSummary = String;
pub type LinkObjectMethod = String;
pub type LinkObjectDescription = String;
pub type LinkObjectParams = serde_json::Value;
#[derive(Serialize, Deserialize, Clone)]
pub struct LinkObjectServer {
pub url: ServerObjectUrl,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<ServerObjectName>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<ServerObjectDescription>,
#[serde(skip_serializing_if = "Option::is_none")]
pub summary: Option<ServerObjectSummary>,
#[serde(skip_serializing_if = "Option::is_none")]
pub variables: Option<ServerObjectVariables>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct LinkObject {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<LinkObjectName>,
#[serde(skip_serializing_if = "Option::is_none")]
pub summary: Option<LinkObjectSummary>,
#[serde(skip_serializing_if = "Option::is_none")]
pub method: Option<LinkObjectMethod>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<LinkObjectDescription>,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<LinkObjectParams>,
#[serde(skip_serializing_if = "Option::is_none")]
pub server: Option<LinkObjectServer>,
}
#[derive(Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum LinkOrReference {
LinkObject(LinkObject),
ReferenceObject(ReferenceObject),
}
pub type MethodObjectLinks = Vec<LinkOrReference>;
pub type ExamplePairingObjectName = String;
pub type ExamplePairingObjectDescription = String;
pub type ExampleObjectSummary = String;
pub type ExampleObjectValue = serde_json::Value;
pub type ExampleObjectDescription = String;
pub type ExampleObjectName = String;
#[derive(Serialize, Deserialize, Clone)]
pub struct ExampleObject {
#[serde(skip_serializing_if = "Option::is_none")]
pub summary: Option<ExampleObjectSummary>,
pub value: ExampleObjectValue,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<ExampleObjectDescription>,
pub name: ExampleObjectName,
}
#[derive(Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum ExampleOrReference {
ExampleObject(ExampleObject),
ReferenceObject(ReferenceObject),
}
pub type ExamplePairingObjectParams = Vec<ExampleOrReference>;
#[derive(Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum ExamplePairingObjectResult {
ExampleObject(ExampleObject),
ReferenceObject(ReferenceObject),
}
#[derive(Serialize, Deserialize, Clone)]
pub struct ExamplePairingObject {
pub name: ExamplePairingObjectName,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<ExamplePairingObjectDescription>,
pub params: ExamplePairingObjectParams,
pub result: ExamplePairingObjectResult,
}
#[derive(Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum ExamplePairingOrReference {
ExampleObject(ExampleObject),
ReferenceObject(ReferenceObject),
}
pub type MethodObjectExamples = Vec<ExamplePairingOrReference>;
pub type MethodObjectDeprecated = bool;
#[derive(Serialize, Deserialize, Clone)]
pub struct MethodObject {
pub name: MethodObjectName,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<MethodObjectDescription>,
#[serde(skip_serializing_if = "Option::is_none")]
pub summary: Option<MethodObjectSummary>,
#[serde(skip_serializing_if = "Option::is_none")]
pub servers: Option<Servers>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<MethodObjectTags>,
#[serde(rename = "paramStructure")]
#[serde(skip_serializing_if = "Option::is_none")]
pub param_structure: Option<MethodObjectParamStructure>,
pub params: MethodObjectParams,
pub result: ContentDescriptorOrReference,
#[serde(skip_serializing_if = "Option::is_none")]
pub errors: Option<MethodObjectErrors>,
#[serde(skip_serializing_if = "Option::is_none")]
pub links: Option<MethodObjectLinks>,
#[serde(skip_serializing_if = "Option::is_none")]
pub examples: Option<MethodObjectExamples>,
#[serde(skip_serializing_if = "Option::is_none")]
pub deprecated: Option<MethodObjectDeprecated>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "externalDocs")]
pub external_docs: Option<ExternalDocumentationObject>,
}
pub type Methods = Vec<MethodObject>;
pub type SchemaComponents = HashMap<String, Option<serde_json::Value>>;
pub type LinkComponents = HashMap<String, Option<serde_json::Value>>;
pub type ErrorComponents = HashMap<String, Option<serde_json::Value>>;
pub type ExampleComponents = HashMap<String, Option<serde_json::Value>>;
pub type ExamplePairingComponents = HashMap<String, Option<serde_json::Value>>;
pub type ContentDescriptorComponents = HashMap<String, Option<serde_json::Value>>;
pub type TagComponents = HashMap<String, Option<serde_json::Value>>;
#[derive(Serialize, Deserialize, Clone)]
pub struct Components {
#[serde(skip_serializing_if = "Option::is_none")]
pub schemas: Option<SchemaComponents>,
#[serde(skip_serializing_if = "Option::is_none")]
pub links: Option<LinkComponents>,
#[serde(skip_serializing_if = "Option::is_none")]
pub errors: Option<ErrorComponents>,
#[serde(skip_serializing_if = "Option::is_none")]
pub examples: Option<ExampleComponents>,
#[serde(rename = "examplePairings")]
pub example_pairings: Option<ExamplePairingComponents>,
#[serde(rename = "contentDescriptors")]
#[serde(skip_serializing_if = "Option::is_none")]
pub content_descriptors: Option<ContentDescriptorComponents>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<TagComponents>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct OpenrpcDocument {
pub openrpc: Openrpc,
pub info: InfoObject,
#[serde(rename = "externalDocs")]
#[serde(skip_serializing_if = "Option::is_none")]
pub external_docs: Option<ExternalDocumentationObject>,
#[serde(skip_serializing_if = "Option::is_none")]
pub servers: Option<Servers>,
pub methods: Methods,
#[serde(skip_serializing_if = "Option::is_none")]
pub components: Option<Components>,
}
impl Default for OpenrpcDocument {
fn default() -> Self {
OpenrpcDocument {
openrpc: Openrpc::V26,
info: InfoObject {
title: "".to_string(),
description: None,
terms_of_service: None,
version: "".to_string(),
contact: None,
license: None,
},
external_docs: None,
servers: None,
methods: vec![],
components: None,
}
}
}
impl OpenrpcDocument {
pub fn set_info(mut self, info: InfoObject) -> Self {
self.info = info;
self
}
pub fn add_object_method(&mut self, method: MethodObject) {
self.methods.push(method)
}
}
impl ContentDescriptorOrReference {
pub fn new_content_descriptor<T: ?Sized + JsonSchema>(
name: ContactObjectName,
description: Option<Description>,
) -> Self {
let mut setting = SchemaSettings::draft07();
setting.inline_subschemas = true;
let schema = schemars::gen::SchemaGenerator::new(setting).into_root_schema_for::<T>();
let json_schema = JSONSchema::JsonSchemaObject(schema);
ContentDescriptorOrReference::ContentDescriptorObject(ContentDescriptorObject {
name,
description,
summary: None,
schema: json_schema,
required: None,
deprecated: None,
})
}
}
impl MethodObject {
pub fn new(name: MethodObjectName, description: Option<Description>) -> Self {
Self {
name,
description,
summary: None,
servers: None,
tags: None,
param_structure: None,
params: vec![],
result: ContentDescriptorOrReference::ReferenceObject(ReferenceObject {
reference: "".to_string(),
}),
errors: None,
links: None,
examples: None,
deprecated: None,
external_docs: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(JsonSchema)]
pub struct MyType([u8; 8]);
#[derive(JsonSchema)]
pub struct MyParam {
pub my_int: i32,
pub my_bool: bool,
pub my_type: Box<MyType>,
}
#[derive(JsonSchema)]
pub struct MyRet {
pub success: Box<bool>,
}
#[test]
fn test_openrpc_document() {
let mut document = OpenrpcDocument::default();
let mut method = MethodObject::new("method1".to_string(), None);
let param = ContentDescriptorOrReference::new_content_descriptor::<MyParam>(
"first_param".to_string(),
Some("no desc".to_string()),
);
method.params.push(param);
method.result =
ContentDescriptorOrReference::new_content_descriptor::<MyRet>("ret".to_string(), None);
document.add_object_method(method);
let j = serde_json::to_string_pretty(&document).unwrap();
println!("{}", j);
}
}