json_api/doc/
specification.rs

1use std::fmt::{self, Display, Formatter};
2use std::str::FromStr;
3
4use serde::de::{Deserialize, Deserializer, Error as DeError};
5use serde::ser::{Serialize, Serializer};
6
7use error::Error;
8use value::{Map, Stringify};
9
10/// Information about this implementation of the specification.
11///
12/// For more information, check out the *[JSON API object]* section of the JSON API
13/// specification.
14///
15/// [JSON API object]: https://goo.gl/hZUcEt
16#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
17pub struct JsonApi {
18    /// Non-standard meta information. If this value of this field is empty, it will not
19    /// be included if the object is serialized. For more information, check out the
20    /// *[meta information]* section of the JSON API specification.
21    ///
22    /// [meta information]: https://goo.gl/LyrGF8
23    #[serde(default, skip_serializing_if = "Map::is_empty")]
24    pub meta: Map,
25
26    /// The latest version of the JSON API specification that is supported by
27    /// this implementation. Defaults to the latest available version.
28    pub version: Version,
29
30    /// Private field for backwards compatibility.
31    #[serde(skip)]
32    _ext: (),
33}
34
35impl JsonApi {
36    /// Returns a new `JsonApi` with the specified `version`.
37    ///
38    /// # Example
39    ///
40    /// ```
41    /// # extern crate json_api;
42    /// #
43    /// # fn main() {
44    /// use json_api::doc::{JsonApi, Version};
45    /// assert_eq!(JsonApi::default(), JsonApi::new(Version::V1));
46    /// # }
47    /// ```
48    pub fn new(version: Version) -> Self {
49        JsonApi {
50            version,
51            meta: Default::default(),
52            _ext: (),
53        }
54    }
55}
56
57/// The version of the specification.
58#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
59pub enum Version {
60    /// Version 1.0
61    V1,
62}
63
64impl Default for Version {
65    fn default() -> Self {
66        Version::V1
67    }
68}
69
70impl Display for Version {
71    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
72        f.write_str(match *self {
73            Version::V1 => "1.0",
74        })
75    }
76}
77
78impl FromStr for Version {
79    type Err = Error;
80
81    fn from_str(value: &str) -> Result<Self, Self::Err> {
82        match value {
83            "1.0" => Ok(Version::V1),
84            v => Err(Error::unsupported_version(v)),
85        }
86    }
87}
88
89impl<'de> Deserialize<'de> for Version {
90    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
91    where
92        D: Deserializer<'de>,
93    {
94        let value = String::deserialize(deserializer)?;
95        value.parse().map_err(D::Error::custom)
96    }
97}
98
99impl Serialize for Version {
100    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
101    where
102        S: Serializer,
103    {
104        serializer.serialize_str(match *self {
105            Version::V1 => "1.0",
106        })
107    }
108}
109
110impl Stringify for Version {
111    fn to_bytes(&self) -> Vec<u8> {
112        let mut bytes = Vec::with_capacity(3);
113
114        match *self {
115            Version::V1 => {
116                bytes.push(b'1');
117                bytes.push(b'.');
118                bytes.push(b'0');
119            }
120        }
121
122        bytes
123    }
124}