1//! Components of a JSON API document.
23mod convert;
4mod ident;
5mod link;
6mod object;
7mod relationship;
8mod specification;
910mod error;
1112use std::iter::FromIterator;
1314use serde::de::DeserializeOwned;
15use serde::ser::Serialize;
1617use error::Error;
18use query::Query;
19use sealed::Sealed;
20use value::{Key, Map, Set, Value};
21use view::Render;
2223pub use self::convert::*;
24pub use self::error::{ErrorObject, ErrorSource};
25pub use self::ident::Identifier;
26pub use self::link::Link;
27pub use self::object::{NewObject, Object};
28pub use self::relationship::Relationship;
29pub use self::specification::{JsonApi, Version};
3031/// A marker trait used to indicate that a type can be the primary data for a
32/// document.
33pub trait PrimaryData: DeserializeOwned + Sealed + Serialize {
34#[doc(hidden)]
35fn flatten(self, &Set<Object>) -> Value;
36}
3738/// Represents a compound JSON API document.
39///
40/// For more information, check out the *[document structure]* section of the JSON API
41/// specification.
42///
43/// [document structure]: https://goo.gl/CXTNmt
44#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
45#[serde(bound = "T: PrimaryData", untagged)]
46pub enum Document<T: PrimaryData> {
47/// Does not contain errors.
48Ok {
49/// The primary data of the document. For more information, check out the
50 /// *[top level]* section of the JSON API specification.
51 ///
52 /// [top level]: https://goo.gl/fQdYgo
53data: Data<T>,
5455/// Included resources, resolved from the `include` query parameter of a client
56 /// request.
57#[serde(default, skip_serializing_if = "Set::is_empty")]
58included: Set<Object>,
5960/// Information about this implementation of the specification that the
61 /// document was created with. For more information, check out the *[JSON API
62 /// object]* section of the JSON API specification.
63 ///
64 /// [JSON API object]: https://goo.gl/hZUcEt
65#[serde(default)]
66jsonapi: JsonApi,
6768/// Contains relevant links. If this value of this field is empty, it will not be
69 /// serialized. For more information, check out the *[links]* section of the JSON
70 /// API specification.
71 ///
72 /// [links]: https://goo.gl/E4E6Vt
73#[serde(default, skip_serializing_if = "Map::is_empty")]
74links: Map<Key, Link>,
7576/// Non-standard meta information. If this value of this field is empty, it will
77 /// not be serialized. For more information, check out the *[meta
78 /// information]* section of the JSON API specification.
79 ///
80 /// [meta information]: https://goo.gl/LyrGF8
81#[serde(default, skip_serializing_if = "Map::is_empty")]
82meta: Map,
83 },
8485/// Contains 1 or more error(s).
86Err {
87 errors: Vec<ErrorObject>,
8889#[serde(default)]
90jsonapi: JsonApi,
9192#[serde(default, skip_serializing_if = "Map::is_empty")]
93links: Map<Key, Link>,
9495#[serde(default, skip_serializing_if = "Map::is_empty")]
96meta: Map,
97 },
98}
99100impl<T: PrimaryData> Document<T> {
101/// Returns `true` if the document does not contain any errors.
102pub fn is_ok(&self) -> bool {
103match *self {
104 Document::Ok { .. } => true,
105 Document::Err { .. } => false,
106 }
107 }
108109/// Returns `true` if the document contains 1 or more error(s).
110pub fn is_err(&self) -> bool {
111match *self {
112 Document::Ok { .. } => true,
113 Document::Err { .. } => false,
114 }
115 }
116}
117118impl<T: PrimaryData> Render<T> for Document<T> {
119fn render(self, _: Option<&Query>) -> Result<Document<T>, Error> {
120Ok(self)
121 }
122}
123124/// Describes the data of a document or resource linkage.
125///
126/// For more information, check out the *[top level]* section of the JSON API
127/// specification.
128///
129/// [top level]: https://goo.gl/fQdYgo
130#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
131#[serde(bound = "T: PrimaryData", untagged)]
132pub enum Data<T: PrimaryData> {
133/// A collection of `T`. Used for requests that target resource collections.
134Collection(Vec<T>),
135136/// An optional `T`. Used for requests that target single resources.
137Member(Box<Option<T>>),
138}
139140impl<T: PrimaryData> From<Option<T>> for Data<T> {
141fn from(value: Option<T>) -> Self {
142 Data::Member(Box::new(value))
143 }
144}
145146impl<T: PrimaryData> From<Vec<T>> for Data<T> {
147fn from(value: Vec<T>) -> Self {
148 Data::Collection(value)
149 }
150}
151152impl<T: PrimaryData> From<T> for Data<T> {
153fn from(value: T) -> Self {
154 Data::Member(Box::new(Some(value)))
155 }
156}
157158impl<T: PrimaryData> FromIterator<T> for Data<T> {
159fn from_iter<I>(iter: I) -> Self
160where
161I: IntoIterator<Item = T>,
162 {
163 Data::Collection(Vec::from_iter(iter))
164 }
165}