json_api/doc/
relationship.rs

1use std::iter::FromIterator;
2
3use doc::{Data, Identifier, Link};
4use value::{Key, Map};
5
6/// Represents a resource's relationship to another.
7///
8/// For more information, check out the *[relationships]* section of the JSON API
9/// specification.
10///
11/// [relationships]: https://goo.gl/ZQw9Xr
12#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
13pub struct Relationship {
14    /// Contains resource linkage. For more information, checkout the
15    /// *[resource linkage]* section of the JSON API specification.
16    ///
17    /// [resource linkage]: https://goo.gl/evZF8m
18    pub data: Data<Identifier>,
19
20    /// Contains relevant links. If this value of this field is empty, it will not be
21    /// serialized. For more information, check out the *[links]* section of the JSON
22    /// API specification.
23    ///
24    /// [links]: https://goo.gl/E4E6Vt
25    #[serde(default, skip_serializing_if = "Map::is_empty")]
26    pub links: Map<Key, Link>,
27
28    /// Non-standard meta information. If this value of this field is empty, it will not
29    /// be serialized. For more information, check out the *[meta information]* section
30    /// of the JSON API specification.
31    ///
32    /// [meta information]: https://goo.gl/LyrGF8
33    #[serde(default, skip_serializing_if = "Map::is_empty")]
34    pub meta: Map,
35
36    /// Private field for backwards compatibility.
37    #[serde(skip)]
38    _ext: (),
39}
40
41impl Relationship {
42    /// Returns a new `Relationship`.
43    ///
44    /// # Example
45    ///
46    /// ```
47    /// # extern crate json_api;
48    /// #
49    /// # use json_api::Error;
50    /// #
51    /// # fn example() -> Result<(), Error> {
52    /// use json_api::doc::{Data, Identifier, Relationship};
53    ///
54    /// let ident = Identifier::new("users".parse()?, "1".to_owned());
55    /// let data = Data::Member(Box::new(Some(ident)));
56    /// let mut relationship = Relationship::new(data);
57    /// # Ok(())
58    /// # }
59    /// #
60    /// # fn main() {
61    /// # example().unwrap();
62    /// # }
63    /// ```
64    pub fn new(data: Data<Identifier>) -> Self {
65        Relationship {
66            data,
67            links: Default::default(),
68            meta: Default::default(),
69            _ext: (),
70        }
71    }
72}
73
74impl From<Option<Identifier>> for Relationship {
75    fn from(value: Option<Identifier>) -> Self {
76        let data = Data::Member(Box::new(value));
77        Relationship::new(data)
78    }
79}
80
81impl From<Vec<Identifier>> for Relationship {
82    fn from(value: Vec<Identifier>) -> Self {
83        let data = Data::Collection(value);
84        Relationship::new(data)
85    }
86}
87
88impl From<Identifier> for Relationship {
89    fn from(value: Identifier) -> Self {
90        Relationship::from(Some(value))
91    }
92}
93
94impl FromIterator<Identifier> for Relationship {
95    fn from_iter<I>(iter: I) -> Self
96    where
97        I: IntoIterator<Item = Identifier>,
98    {
99        let data = Data::from_iter(iter);
100        Relationship::new(data)
101    }
102}