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
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;

use serde::de::{Deserialize, Deserializer, Error as DeError};
use serde::ser::{Serialize, Serializer};

use error::Error;
use value::{Map, Stringify};

/// Information about this implementation of the specification.
///
/// For more information, check out the *[JSON API object]* section of the JSON API
/// specification.
///
/// [JSON API object]: https://goo.gl/hZUcEt
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct JsonApi {
    /// Non-standard meta information. If this value of this field is empty, it will not
    /// be included if the object is serialized. For more information, check out the
    /// *[meta information]* section of the JSON API specification.
    ///
    /// [meta information]: https://goo.gl/LyrGF8
    #[serde(default, skip_serializing_if = "Map::is_empty")]
    pub meta: Map,

    /// The latest version of the JSON API specification that is supported by
    /// this implementation. Defaults to the latest available version.
    pub version: Version,

    /// Private field for backwards compatibility.
    #[serde(skip)]
    _ext: (),
}

impl JsonApi {
    /// Returns a new `JsonApi` with the specified `version`.
    ///
    /// # Example
    ///
    /// ```
    /// # extern crate json_api;
    /// #
    /// # fn main() {
    /// use json_api::doc::{JsonApi, Version};
    /// assert_eq!(JsonApi::default(), JsonApi::new(Version::V1));
    /// # }
    /// ```
    pub fn new(version: Version) -> Self {
        JsonApi {
            version,
            meta: Default::default(),
            _ext: (),
        }
    }
}

/// The version of the specification.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Version {
    /// Version 1.0
    V1,
}

impl Default for Version {
    fn default() -> Self {
        Version::V1
    }
}

impl Display for Version {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.write_str(match *self {
            Version::V1 => "1.0",
        })
    }
}

impl FromStr for Version {
    type Err = Error;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "1.0" => Ok(Version::V1),
            v => Err(Error::unsupported_version(v)),
        }
    }
}

impl<'de> Deserialize<'de> for Version {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        value.parse().map_err(D::Error::custom)
    }
}

impl Serialize for Version {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(match *self {
            Version::V1 => "1.0",
        })
    }
}

impl Stringify for Version {
    fn to_bytes(&self) -> Vec<u8> {
        let mut bytes = Vec::with_capacity(3);

        match *self {
            Version::V1 => {
                bytes.push(b'1');
                bytes.push(b'.');
                bytes.push(b'0');
            }
        }

        bytes
    }
}