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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use crate::{Meta, Relationships, Links, GenericObject, ResourceObject, Attributes};
use serde::{Serialize as SerializeTrait, de::DeserializeOwned};
use serde_derive::{Serialize, Deserialize};
use super::ObjectConversionError;

/// A resource identifier object
///
/// Basically just the identifying information needed to refer to a `ResourceObject`
///
/// See the [JSON:API docs](https://jsonapi.org/format/#document-resource-identifier-objects)
/// for more information
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Identifier {
    /// The identifier of the object which, along with the object's type, MUST refer to a single
    /// unique resource
    pub id: String,
    /// The object's type, renamed here to kind due to keyword limitations
    #[serde(rename = "type")]
    pub kind: String,
    /// Non-standard meta information
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta: Option<Meta>,
}

impl Identifier {
    pub fn new(id: String, kind: String) -> Self {
        Self {
            id,
            kind,
            meta: None,
        }
    }
}

impl From<GenericObject> for Identifier {
    fn from(go: GenericObject) -> Self {
        Self {
            id: go.id,
            kind: go.kind,
            meta: go.meta,
        }
    }
}

impl From<&GenericObject> for Identifier {
    fn from(go: &GenericObject) -> Self {
        Self {
            id: go.id.clone(),
            kind: go.kind.clone(),
            meta: go.meta.clone(),
        }
    }
}

impl<A> From<ResourceObject<A>> for Identifier 
where A: Attributes + SerializeTrait + DeserializeOwned {
    fn from(ro: ResourceObject<A>) -> Self {
        Self {
            id: ro.id,
            kind: A::kind(),
            meta: ro.meta,
        }
    }
}

impl<A> From<&ResourceObject<A>> for Identifier
where A: Attributes + SerializeTrait + DeserializeOwned {
    fn from(ro: &ResourceObject<A>) -> Self {
        Self {
            id: ro.id.clone(),
            kind: A::kind(),
            meta: ro.meta.clone(),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use serde_json;

    #[test]
    fn serde() {
        let id1 = Identifier{
            id: "a".into(),
            kind: "b".into(),
            meta: None,
        };
        let s = serde_json::to_string(&id1).unwrap();
        assert_eq!(s, "{\"id\":\"a\",\"type\":\"b\"}");
        let id2 = serde_json::from_str(&s).unwrap();
        assert_eq!(id1, id2);
    }

    #[test]
    fn from_go() {
        let mut meta = Meta::new();
        meta.insert("c".into(), serde_json::json!("d"));
        let go = GenericObject {
            id: "a".into(),
            kind: "b".into(),
            attributes: None,
            relationships: None,
            links: None,
            meta: Some(meta.clone()),
        };

        let id = (&go).into();
        assert_eq!(Identifier {
            id: "a".into(),
            kind: "b".into(),
            meta: Some(meta.clone()),
        }, id);

        let id = go.into();
        assert_eq!(Identifier {
            id: "a".into(),
            kind: "b".into(),
            meta: Some(meta.clone()),
        }, id);
    }

    #[test] 
    fn from_ro() {
        #[derive(Serialize, Deserialize, Eq, PartialEq, Clone)]
        struct Attr;
        impl Attributes for Attr{
            fn kind() -> String { "b".into() }
        }

        let mut meta = Meta::new();
        meta.insert("c".into(), serde_json::json!("d"));
        let ro : ResourceObject<Attr> = ResourceObject {
            id: "a".into(),
            attributes: None,
            relationships: None,
            links: None,
            meta: Some(meta.clone()),
        };

        let id = (&ro).into();
        assert_eq!(Identifier {
            id: "a".into(),
            kind: "b".into(),
            meta: Some(meta.clone()),
        }, id);

        let id = ro.into();
        assert_eq!(Identifier {
            id: "a".into(),
            kind: "b".into(),
            meta: Some(meta.clone()),
        }, id);
    }
}