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
//! A transcription of types from the [`OpenRPC` Specification](https://spec.open-rpc.org/).
//!
//! This library does NOT perform more complicated validation of the spec, including:
//! - unique method names
//! - unique error codes
//! - reference idents
//!
//! `Link` objects are not currently supported.
//!
//! > When quoted, the specification will appear as blockquoted text, like so.

use schemars::schema::Schema;
use semver::{BuildMetadata, Prerelease, Version};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::BTreeMap;
use url::Url;

pub use resolver::{resolve_within, BrokenReference};

pub mod resolved;
mod resolver;

/// The version of the OpenRPC specification that this library was written against.
pub const OPEN_RPC_SPECIFICATION_VERSION: Version = Version {
    major: 1,
    minor: 3,
    patch: 2,
    pre: Prerelease::EMPTY,
    build: BuildMetadata::EMPTY,
};

/// > This is the root object of the OpenRPC document.
/// > The contents of this object represent a whole OpenRPC document.
/// > How this object is constructed or stored is outside the scope of the OpenRPC Specification.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OpenRPC {
    /// > REQUIRED.
    /// > This string MUST be the semantic version number of the OpenRPC Specification version that the OpenRPC document uses.
    /// > The openrpc field SHOULD be used by tooling specifications and clients to interpret the OpenRPC document.
    /// > This is not related to the API info.version string.
    pub openrpc: Version,
    /// > REQUIRED.
    /// > Provides metadata about the API.
    /// > The metadata MAY be used by tooling as required.
    pub info: Info,
    /// > An array of Server Objects,
    /// > which provide connectivity information to a target server.
    /// > If the servers property is not provided, or is an empty array,
    /// > the default value would be a Server Object with a url value of `localhost`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub servers: Option<Vec<Server>>,
    /// > REQUIRED.
    /// > The available methods for the API.
    /// > While it is required, the array may be empty (to handle security filtering, for example).
    pub methods: Vec<ReferenceOr<Method>>,
    /// > An element to hold various schemas for the specification.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub components: Option<Components>,
    /// > Additional external documentation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub external_docs: Option<ExternalDocumentation>,
    #[serde(flatten)]
    pub extensions: SpecificationExtensions,
}

impl Default for OpenRPC {
    fn default() -> Self {
        Self {
            openrpc: OPEN_RPC_SPECIFICATION_VERSION,
            info: Default::default(),
            servers: Default::default(),
            methods: Default::default(),
            components: Default::default(),
            external_docs: Default::default(),
            extensions: Default::default(),
        }
    }
}

/// > The object provides metadata about the API.
/// > The metadata MAY be used by the clients if needed,
/// > and MAY be presented in editing or documentation generation tools for convenience.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Info {
    /// > REQUIRED.
    /// > The title of the application.
    pub title: String,
    /// > A verbose description of the application.
    /// > GitHub Flavored Markdown syntax MAY be used for rich text representation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// > A URL to the Terms of Service for the API.
    /// > MUST be in the format of a URL.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub terms_of_service: Option<Url>,
    /// > The contact information for the exposed API.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contact: Option<Contact>,
    /// > The license information for the exposed API.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub license: Option<License>,
    /// > REQUIRED.
    /// > The version of the OpenRPC document
    /// > (which is distinct from the OpenRPC Specification version or the API implementation version).
    pub version: String,
    #[serde(flatten)]
    pub extensions: SpecificationExtensions,
}

/// > Contact information for the exposed API.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct Contact {
    /// > The identifying name of the contact person/organization.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// > The URL pointing to the contact information.
    /// > MUST be in the format of a URL.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<Url>,
    /// > The email address of the contact person/organization.
    /// > MUST be in the format of an email address.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
    #[serde(flatten)]
    pub extensions: SpecificationExtensions,
}

/// > License information for the exposed API.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct License {
    /// > REQUIRED.
    /// > The license name used for the API.
    pub name: String,
    /// > A URL to the license used for the API.
    /// > MUST be in the format of a URL.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<Url>,
    #[serde(flatten)]
    pub extensions: SpecificationExtensions,
}

/// > An object representing a Server.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct Server {
    /// > REQUIRED.
    /// > A name to be used as the cannonical name for the server.
    pub name: String,
    /// > REQUIRED.
    /// > A URL to the target host.
    /// > This URL supports Server Variables and MAY be relative,
    /// > to indicate that the host location is relative to the location where the OpenRPC document is being served.
    /// > Server Variables are passed into the Runtime Expression to produce a server URL.
    pub url: String,
    /// > A short summary of what the server is.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub summary: Option<String>,
    /// > An optional string describing the host designated by the URL.
    /// > GitHub Flavored Markdown syntax MAY be used for rich text representation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// > A map between a variable name and its value.
    /// > The value is passed into the Runtime Expression to produce a server URL.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub variables: Option<BTreeMap<String, ServerVariable>>,
    #[serde(flatten)]
    pub extensions: SpecificationExtensions,
}
/// > An object representing a Server Variable for server URL template substitution.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct ServerVariable {
    /// > An enumeration of string values to be used if the substitution options are from a limited set.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#enum: Option<Vec<String>>,
    /// > REQUIRED.
    /// > The default value to use for substitution,
    /// > which SHALL be sent if an alternate value is not supplied.
    /// > Note this behavior is different than the Schema Object’s treatment of default values,
    /// > because in those cases parameter values are optional.
    pub default: String,
    /// > An optional description for the server variable. GitHub Flavored Markdown syntax MAY be used for rich text representation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(flatten)]
    pub extensions: SpecificationExtensions,
}

/// > Describes the interface for the given method name.
/// > The method name is used as the method field of the JSON-RPC body.
/// > It therefore MUST be unique.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Method {
    /// > REQUIRED.
    /// > The cannonical name for the method.
    /// > The name MUST be unique within the methods array.
    pub name: String,
    /// > A list of tags for API documentation control.
    /// > Tags can be used for logical grouping of methods by resources or any other qualifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<ReferenceOr<Tag>>>,
    /// > A short summary of what the method does.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub summary: Option<String>,
    /// > A verbose explanation of the method behavior.
    /// > GitHub Flavored Markdown syntax MAY be used for rich text representation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// > Additional external documentation for this method.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub external_docs: Option<ExternalDocumentation>,
    /// > REQUIRED.
    /// > A list of parameters that are applicable for this method.
    /// > The list MUST NOT include duplicated parameters and therefore require name to be unique.
    /// > The list can use the Reference Object to link to parameters that are defined by the Content Descriptor Object.
    /// > All optional params (content descriptor objects with “required”: false) MUST be positioned after all required params in the list.
    pub params: Vec<ReferenceOr<ContentDescriptor>>,
    /// > The description of the result returned by the method.
    /// > If defined, it MUST be a Content Descriptor or Reference Object.
    /// > If undefined, the method MUST only be used as a notification.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<ReferenceOr<ContentDescriptor>>,
    /// > Declares this method to be deprecated.
    /// > Consumers SHOULD refrain from usage of the declared method.
    /// > Default value is `false`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deprecated: Option<bool>,
    /// > An alternative servers array to service this method.
    /// > If an alternative servers array is specified at the Root level,
    /// > it will be overridden by this value.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub servers: Option<Vec<Server>>,
    /// > A list of custom application defined errors that MAY be returned.
    /// > The Errors MUST have unique error codes.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub errors: Option<Vec<ReferenceOr<Error>>>,
    // /// > A list of possible links from this method call.
    // pub links: Option<Vec<ReferenceOr<Link>>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub param_structure: Option<ParamStructure>,
    /// > Array of Example Pairing Objects where each example includes a valid params-to-result Content Descriptor pairing.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub examples: Option<Vec<ReferenceOr<ExamplePairing>>>,
    #[serde(flatten)]
    pub extensions: SpecificationExtensions,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ContentDescriptor {
    /// > REQUIRED.
    /// > Name of the content that is being described.
    /// > If the content described is a method parameter assignable by-name, this field SHALL define the parameter’s key (ie name).
    pub name: String,
    /// > A short summary of the content that is being described.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub summary: Option<String>,
    /// > A verbose explanation of the content descriptor behavior.
    /// > GitHub Flavored Markdown syntax MAY be used for rich text representation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// > Determines if the content is a required field.
    /// > Default value is `false`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub required: Option<bool>,
    /// > REQUIRED.
    /// > Schema that describes the content.
    ///
    /// > The Schema Object allows the definition of input and output data types.
    /// > The Schema Objects MUST follow the specifications outline in the JSON Schema Specification 7 Alternatively,
    /// > any time a Schema Object can be used, a Reference Object can be used in its place.
    /// > This allows referencing definitions instead of defining them inline.
    ///
    /// > This object MAY be extended with Specification Extensions.
    pub schema: Schema,
    /// > Specifies that the content is deprecated and SHOULD be transitioned out of usage.
    /// > Default value is `false`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deprecated: Option<bool>,
    #[serde(flatten)]
    pub extensions: SpecificationExtensions,
}

impl Default for ContentDescriptor {
    fn default() -> Self {
        Self {
            name: Default::default(),
            summary: Default::default(),
            description: Default::default(),
            required: Default::default(),
            schema: Schema::Bool(false),
            deprecated: Default::default(),
            extensions: Default::default(),
        }
    }
}

/// > The Example Pairing object consists of a set of example params and result.
/// > The result is what you can expect from the JSON-RPC service given the exact params.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct ExamplePairing {
    /// > REQUIRED Name for the example pairing.
    pub name: String,
    /// > A verbose explanation of the example pairing.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// > Short description for the example pairing.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub summary: Option<String>,
    /// > REQUIRED Example parameters.
    pub params: Vec<ReferenceOr<Example>>,
    /// > Example result.
    /// > When undefined, the example pairing represents usage of the method as a notification.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<ReferenceOr<Example>>,
    #[serde(flatten)]
    pub extensions: SpecificationExtensions,
}

/// > The Example object is an object that defines an example that is intended to match the schema of a given Content Descriptor.
/// >
/// > In all cases, the example value is expected to be compatible with the type schema of its associated value.
/// > Tooling implementations MAY choose to validate compatibility automatically,
/// > and reject the example value(s) if incompatible.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Example {
    /// Cannonical name of the example.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Short description for the example.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub summary: Option<String>,
    /// > A verbose explanation of the example.
    /// > GitHub Flavored Markdown syntax MAY be used for rich text representation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(flatten)]
    pub value: ExampleValue,
    #[serde(flatten)]
    pub extensions: SpecificationExtensions,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub enum ExampleValue {
    /// > A URL that points to the literal example.
    /// > This provides the capability to reference examples that cannot easily be included in JSON documents.
    /// > The value field and externalValue field are mutually exclusive.
    #[serde(rename = "externalValue")]
    External(String),
    /// > Embedded literal example.
    /// > The value field and externalValue field are mutually exclusive.
    /// > To represent examples of media types that cannot naturally represented in JSON,
    /// > use a string value to contain the example, escaping where necessary.
    #[serde(rename = "value")]
    Embedded(Value),
}

#[test]
fn example() {
    let json = serde_json::json!({
        "name": "foo",
        "value": { "foo": "bar" },
        "x-tension": "extension"
    });
    let actual = serde_json::from_value::<Example>(json.clone()).unwrap();
    assert_eq!(serde_json::to_value(actual).unwrap(), json);
}

// pub struct Link {} // TODO

/// > Defines an application level error.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Error {
    /// > REQUIRED.
    /// > A Number that indicates the error type that occurred.
    /// > This MUST be an integer.
    /// > The error codes from and including -32768 to -32000 are reserved for pre-defined errors.
    /// > These pre-defined errors SHOULD be assumed to be returned from any JSON-RPC api.
    pub code: i64,
    /// > REQUIRED.
    /// > A String providing a short description of the error.
    /// > The message SHOULD be limited to a concise single sentence.
    pub message: String,
    /// > A Primitive or Structured value that contains additional information about the error.
    /// > This may be omitted.
    /// > The value of this member is defined by the Server (e.g. detailed error information, nested errors etc.).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<Value>,
}

/// > Holds a set of reusable objects for different aspects of the OpenRPC.
/// > All objects defined within the components object will have no effect on the
/// > API unless they are explicitly referenced from properties outside the components object.
/// > All the fixed fields declared above are objects that MUST use keys that match the regular expression: ^[a-zA-Z0-9\.\-_]+$
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]

pub struct Components {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_descriptors: Option<BTreeMap<String, ContentDescriptor>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub schemas: Option<BTreeMap<String, Schema>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub examples: Option<BTreeMap<String, Example>>,
    // #[serde(skip_serializing_if = "Option::is_none")]
    // pub links: Option<BTreeMap<String, Link>>, // TODO
    #[serde(skip_serializing_if = "Option::is_none")]
    pub errors: Option<BTreeMap<String, Error>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub example_pairing_objects: Option<BTreeMap<String, ExamplePairing>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<BTreeMap<String, Tag>>,
    #[serde(flatten)]
    pub extensions: SpecificationExtensions,
}

/// > Adds metadata to a single tag that is used by the Method Object.
/// > It is not mandatory to have a Tag Object per tag defined in the Method Object instances.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Tag {
    /// > REQUIRED.
    /// > The name of the tag.
    pub name: String,
    /// > A short summary of the tag.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub summary: Option<String>,
    /// > A verbose explanation for the tag.
    /// > GitHub Flavored Markdown syntax MAY be used for rich text representation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// > Additional external documentation for this tag.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub external_docs: Option<ExternalDocumentation>,
    #[serde(flatten)]
    pub extensions: SpecificationExtensions,
}

/// > Allows referencing an external resource for extended documentation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ExternalDocumentation {
    /// > A verbose explanation of the target documentation.
    /// > GitHub Flavored Markdown syntax MAY be used for rich text representation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// > The URL for the target documentation.
    /// > Value MUST be in the format of a URL.
    pub url: Url,
    #[serde(flatten)]
    pub extensions: SpecificationExtensions,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ReferenceOr<T> {
    /// > A simple object to allow referencing other components in the specification, internally and externally.
    /// > The Reference Object is defined by JSON Schema and follows the same structure, behavior and rules.
    Reference(String),
    Item(T),
}

/// > While the OpenRPC Specification tries to accommodate most use cases,
/// > additional data can be added to extend the specification at certain points.
/// >
/// > The extensions properties are implemented as patterned fields that are always prefixed by "x-".
/// >
/// > The extensions may or may not be supported by the available tooling,
/// > but those may be extended as well to add requested support
/// > (if tools are internal or open-sourced).
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
#[serde(transparent)]
pub struct SpecificationExtensions(pub BTreeMap<String, Value>);

impl<'de> Deserialize<'de> for SpecificationExtensions {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        struct Visitor;

        impl<'de> serde::de::Visitor<'de> for Visitor {
            type Value = BTreeMap<String, Value>;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("a map with string keys starting with `x-`")
            }

            fn visit_map<A: serde::de::MapAccess<'de>>(
                self,
                mut map: A,
            ) -> Result<Self::Value, A::Error> {
                let mut ret = Self::Value::default();
                loop {
                    match map.next_key::<String>() {
                        Err(_) => (),
                        Ok(None) => break,
                        Ok(Some(key)) if key.starts_with("x-") => {
                            let _ = ret.insert(key, map.next_value()?);
                        }
                        Ok(Some(_)) => {
                            let _ = map.next_value::<serde::de::IgnoredAny>()?;
                        }
                    }
                }

                Ok(ret)
            }
        }
        deserializer.deserialize_any(Visitor).map(Self)
    }
}

/// > The expected format of the parameters.
/// > As per the JSON-RPC 2.0 specification,
/// > the params of a JSON-RPC request object may be an array, object, or either
/// > (represented as by-position, by-name, and either respectively).
/// > When a method has a paramStructure value of by-name,
/// > callers of the method MUST send a JSON-RPC request object whose params field is an object.
/// > Further, the key names of the params object MUST be the same as the contentDescriptor.names for the given method.
/// > Defaults to "either".
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
pub enum ParamStructure {
    ByName,
    ByPosition,
    #[default]
    Either,
}

#[derive(Serialize, Deserialize)]
#[serde(untagged, expecting = "a reference or an inline item")]
enum _ReferenceOr<T> {
    Reference {
        #[serde(rename = "$ref")]
        reference: String,
    },
    Item(T),
}

impl<T> ReferenceOr<T> {
    pub fn map_item<U>(self, f: impl FnOnce(T) -> U) -> ReferenceOr<U> {
        match self {
            ReferenceOr::Reference(it) => ReferenceOr::Reference(it),
            ReferenceOr::Item(it) => ReferenceOr::Item(f(it)),
        }
    }
}

impl<T: Serialize> Serialize for ReferenceOr<T> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            ReferenceOr::Reference(it) => _ReferenceOr::Reference {
                reference: it.clone(),
            },
            ReferenceOr::Item(it) => _ReferenceOr::Item(it),
        }
        .serialize(serializer)
    }
}

impl<'de, T: Deserialize<'de>> Deserialize<'de> for ReferenceOr<T> {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        _ReferenceOr::deserialize(deserializer).map(|it| match it {
            _ReferenceOr::Reference { reference } => ReferenceOr::Reference(reference),
            _ReferenceOr::Item(it) => ReferenceOr::Item(it),
        })
    }
}

impl Default for resolved::OpenRPC {
    fn default() -> Self {
        Self {
            openrpc: OPEN_RPC_SPECIFICATION_VERSION,
            info: Default::default(),
            servers: Default::default(),
            methods: Default::default(),
            components: Default::default(),
            external_docs: Default::default(),
            extensions: Default::default(),
        }
    }
}

#[cfg(test)]
mod tests {
    use regex::Regex;
    use std::borrow::Cow;
    use syn::{spanned::Spanned, Item};

    // It's just easier this way
    #[test]
    fn generate_resolved() {
        let lib = syn::parse_file(include_str!("lib.rs")).unwrap();
        let regex = Regex::new("ReferenceOr<(?<item>[[:alnum:]]+?)>").unwrap();
        let mut rewritten = String::from(
            "\
// This file is @generated by library tests

//! Parallel types where [`ReferenceOr<T>`] is replaced by item `T`.

use crate::*;
use semver::Version;
use serde::{Deserialize, Serialize};

",
        );

        for item in lib.items {
            if let Item::Struct(strukt) = item {
                let source = strukt.span().source_text().unwrap();
                match regex.replace_all(&source, "$item") {
                    Cow::Borrowed(_) => {}
                    Cow::Owned(replaced) => {
                        rewritten.push_str(&replaced);
                        rewritten.push('\n');
                    }
                }
            }
        }
        expect_test::expect_file!["./resolved.rs"].assert_eq(&rewritten);
    }
}