asyncapi/
reference.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
4#[serde(untagged)]
5pub enum ReferenceOr<T> {
6    /// A simple object to allow referencing other components in the specification,
7    /// internally and externally.
8    ///
9    /// The Reference Object is defined by
10    /// [JSON Reference](https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03)
11    /// and follows the same structure,
12    /// behavior and rules. A JSON Reference SHALL only be used to refer to a schema that
13    /// is formatted in either JSON or YAML. In the case of a YAML-formatted Schema,
14    /// the JSON Reference SHALL be applied to the JSON representation of that schema.
15    /// The JSON representation SHALL be made by applying the conversion described
16    /// [here](https://www.asyncapi.com/docs/specifications/v2.3.0#format).
17    ///
18    /// For this specification, reference resolution is done as defined by the
19    /// JSON Reference specification and not by the JSON Schema specification.
20    Reference {
21        #[serde(rename = "$ref")]
22        reference: String,
23    },
24    Item(T),
25}
26
27impl<T> ReferenceOr<T> {
28    pub fn ref_(r: &str) -> Self {
29        ReferenceOr::Reference {
30            reference: r.to_owned(),
31        }
32    }
33    pub fn boxed_item(item: T) -> ReferenceOr<Box<T>> {
34        ReferenceOr::Item(Box::new(item))
35    }
36}
37
38impl<T> ReferenceOr<Box<T>> {
39    pub fn unbox(self) -> ReferenceOr<T> {
40        match self {
41            ReferenceOr::Reference { reference } => ReferenceOr::Reference { reference },
42            ReferenceOr::Item(boxed) => ReferenceOr::Item(*boxed),
43        }
44    }
45}