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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
use std::cmp::{Eq, PartialEq}; use std::hash::{Hash, Hasher}; use std::mem; use doc::{Data, Document, Identifier, Link, PrimaryData, Relationship}; use error::Error; use query::Query; use sealed::Sealed; use value::{Key, Map, Set, Value}; use view::Render; /// A preexisting resource. Commonly found in the document of a response or `PATCH` /// request. /// /// Both the [`id`] and [`type`][`kind`] field must be present if an `Object` is /// deserialized. If you need to represent a resource object that does not already have /// an [`id`], you can use [`NewObject`]. For more information, check out the *[resource /// objects]* section of the JSON API specification. /// /// # Equality /// /// Objects are considered to be equal if they have the same [`id`] and [`kind`]. /// /// ``` /// # extern crate json_api; /// # /// # use json_api::Error; /// # /// # fn example() -> Result<(), Error> { /// use json_api::doc::Object; /// use json_api::value::Key; /// /// let person = "person".parse::<Key>()?; /// let hero = "hero".parse::<Key>()?; /// /// let mut bruce = Object::new(person.clone(), "🦇".to_owned()); /// let mut batman = Object::new(person.clone(), "🦇".to_owned()); /// /// bruce.attributes.insert("name".parse()?, "Bruce Wayne".into()); /// batman.attributes.insert("name".parse()?, "Batman".into()); /// /// // Bruce and Batman are equal because they have the same `id` and `kind`. /// assert!(bruce == batman); /// /// // Let's make Batman a "hero" instead of a "person". /// batman.kind = hero.clone(); /// /// // Bruce and Batman are no longer equal. /// assert!(bruce != batman); /// # /// # Ok(()) /// # } /// # /// # fn main() { /// # example().unwrap(); /// # } /// ``` /// /// Since an [`Identifier`] is a subset of `Object` with fields necessary for /// identification, you can compare the two. /// /// ``` /// # extern crate json_api; /// # /// # use json_api::Error; /// # /// # fn example() -> Result<(), Error> { /// # use json_api::doc::Object; /// # use json_api::value::Key; /// # /// # let hero = "hero".parse::<Key>()?; /// # let batman = Object::new(hero.clone(), "🦇".to_owned()); /// # /// use json_api::doc::Identifier; /// assert!(Identifier::from(&batman) == batman); /// # /// # Ok(()) /// # } /// # /// # fn main() { /// # example().unwrap(); /// # } /// ``` /// /// # Hashing /// /// Similar to how equality works, object's are hashed by their [`id`] and [`kind`]. This /// allows for easy and efficient deduplication when embedding related resources in a /// response. /// /// **Note:** The following example is to demonstrate how object's are hashed. /// Deduplication occurs automatically if you use the [`json_api::to_doc`] function with /// a [`Resource`] that was implemented with the [`resource!`] macro. /// /// ``` /// # extern crate json_api; /// # /// # use json_api::Error; /// # /// # fn example() -> Result<(), Error> { /// use json_api::doc::Object; /// use json_api::value::{Key, Set}; /// /// let person = "person".parse::<Key>()?; /// let hero = "hero".parse::<Key>()?; /// /// let mut included = Set::new(); /// /// let mut diana = Object::new(person.clone(), "🛡".to_owned()); /// let mut wonder_woman = Object::new(person.clone(), "🛡".to_owned()); /// /// diana.attributes.insert("name".parse()?, "Diana Prince".into()); /// wonder_woman.attributes.insert("name".parse()?, "Wonder Woman".into()); /// /// included.insert(diana); /// assert_eq!(included.len(), 1); /// /// included.insert(wonder_woman.clone()); /// assert_eq!(included.len(), 1); /// /// // Let's update Wonder Woman's kind to "hero" so we can embed her in the response. /// wonder_woman.kind = hero.clone(); /// /// included.insert(wonder_woman.clone()); /// assert_eq!(included.len(), 2); /// # /// # Ok(()) /// # } /// # /// # fn main() { /// # example().unwrap(); /// # } /// ``` /// /// [`Identifier`]: ./struct.Identifier.html /// [`NewObject`]: ./struct.NewObject.html /// [`Resource`]: ../trait.Resource.html /// [`resource!`]: ../macro.resource.html /// [`json_api::to_doc`]: ../fn.to_doc.html /// [`id`]: #structfield.id /// [`kind`]: #structfield.kind /// [resource objects]: https://goo.gl/55cSP7 #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Object { /// Contains some of the object's data. If this value of this field is empty, it will /// not be serialized. For more information, check out the *[attributes]* section of /// the JSON API specification. /// /// [attributes]: https://goo.gl/TshgH1 #[serde(default, skip_serializing_if = "Map::is_empty")] pub attributes: Map, /// A string that contains a unique identfier for this resource type (`kind`). For /// more information, check out the *[identification]* section of the JSON API /// specification. /// /// [identification]: https://goo.gl/3s681i pub id: String, /// Describes resources that share common attributes and relationships. This field is /// derived from the `type` field if the object is deserialized. For more /// information, check out the *[identification]* section of the JSON API /// specification. /// /// [identification]: https://goo.gl/3s681i #[serde(rename = "type")] pub kind: Key, /// Contains relevant links. If this value of this field is empty, it will not be /// serialized. For more information, check out the *[links]* section of the JSON /// API specification. /// /// [links]: https://goo.gl/E4E6Vt #[serde(default, skip_serializing_if = "Map::is_empty")] pub links: Map<Key, Link>, /// Non-standard meta information. If this value of this field is empty, it will not /// be 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, /// Describes relationships between this object and other resource objects. If this /// value of this field is empty, it will not be serialized. For more information, /// check out the *[relationships]* section of the JSON API specification. /// /// [relationships]: https://goo.gl/Gxghwc #[serde(default, skip_serializing_if = "Map::is_empty")] pub relationships: Map<Key, Relationship>, /// Private field for backwards compatibility. #[serde(skip)] _ext: (), } impl Object { /// Returns a new `Object`. /// /// # Example /// /// ``` /// # extern crate json_api; /// # /// # use json_api::Error; /// # /// # fn example() -> Result<(), Error> { /// use json_api::doc::Object; /// let mut obj = Object::new("users".parse()?, "1".to_owned()); /// # Ok(()) /// # } /// # /// # fn main() { /// # example().unwrap(); /// # } /// ``` pub fn new(kind: Key, id: String) -> Self { Object { id, kind, attributes: Default::default(), links: Default::default(), meta: Default::default(), relationships: Default::default(), _ext: (), } } } impl Eq for Object {} impl Hash for Object { fn hash<H: Hasher>(&self, state: &mut H) { self.id.hash(state); self.kind.hash(state); } } impl PartialEq for Object { fn eq(&self, rhs: &Object) -> bool { self.id == rhs.id && self.kind == rhs.kind } } impl PartialEq<Identifier> for Object { fn eq(&self, rhs: &Identifier) -> bool { self.id == rhs.id && self.kind == rhs.kind } } impl Render<Identifier> for Object { fn render(self, query: Option<&Query>) -> Result<Document<Identifier>, Error> { Identifier::from(self).render(query) } } impl Render<Identifier> for Vec<Object> { fn render(self, _: Option<&Query>) -> Result<Document<Identifier>, Error> { let data = self.into_iter().map(Identifier::from).collect(); Ok(Document::Ok { data, included: Default::default(), jsonapi: Default::default(), links: Default::default(), meta: Default::default(), }) } } impl Render<Object> for Object { fn render(mut self, _: Option<&Query>) -> Result<Document<Object>, Error> { let links = mem::replace(&mut self.links, Default::default()); let meta = mem::replace(&mut self.meta, Default::default()); Ok(Document::Ok { links, meta, data: Data::Member(Box::new(Some(self))), included: Default::default(), jsonapi: Default::default(), }) } } impl Render<Object> for Vec<Object> { fn render(self, _: Option<&Query>) -> Result<Document<Object>, Error> { Ok(Document::Ok { data: Data::Collection(self), included: Default::default(), jsonapi: Default::default(), links: Default::default(), meta: Default::default(), }) } } impl PrimaryData for Object { fn flatten(self, incl: &Set<Object>) -> Value { #[cfg_attr(rustfmt, rustfmt_skip)] let Object { id, attributes, relationships, .. } = self; let mut map = { let size = attributes.len() + relationships.len() + 1; Map::with_capacity(size) }; map.insert(Key::from_raw("id".to_owned()), Value::String(id)); map.extend(attributes); for (key, value) in relationships { let value = match value.data { Data::Member(data) => match *data { Some(item) => item.flatten(incl), None => Value::Null, }, Data::Collection(data) => { let iter = data.into_iter().map(|item| item.flatten(incl)); Value::Array(iter.collect()) } }; map.insert(key, value); } Value::Object(map) } } impl Sealed for Object {} /// A resource that does not already exist. Commonly found in the document of a /// `POST` request. /// /// For more information, check out the *[creating resources]* section of the JSON API /// specification. /// /// [creating resources]: https://goo.gl/KoLQgh #[derive(Clone, Debug, Deserialize, Serialize)] pub struct NewObject { /// Contains some of the object's data. If this value of this field is empty, it will /// not be serialized. For more information, check out the *[attributes]* section of /// the JSON API specification. /// /// [attributes]: https://goo.gl/TshgH1 #[serde(default, skip_serializing_if = "Map::is_empty")] pub attributes: Map, /// An optional string that contains a unique identfier for this resource type /// (`kind`). A `Some` value here should be interpreted as *[client-generated id]*. /// For more information, check out the *[identification]* section of /// the JSON API specification. /// /// [client-generated id]: https://goo.gl/W16smj /// [identification]: https://goo.gl/3s681i pub id: Option<String>, /// Describes resources that share common attributes and relationships. This field /// is derived from the `type` field if the object is deserialized. For more /// information, check out the *[identification]* section of the JSON API /// specification. /// /// [identification]: https://goo.gl/3s681i #[serde(rename = "type")] pub kind: Key, /// Contains relevant links. If this value of this field is empty, it will not be /// serialized. For more information, check out the *[links]* section of the JSON /// API specification. /// /// [links]: https://goo.gl/E4E6Vt #[serde(default, skip_serializing_if = "Map::is_empty")] pub links: Map<Key, Link>, /// Non-standard meta information. If this value of this field is empty, it will not /// be 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, /// Describes relationships between this object and other resource objects. If this /// value of this field is empty, it will not be serialized. For more information, /// check out the *[relationships]* section of the JSON API specification. /// /// [relationships]: https://goo.gl/Gxghwc #[serde(default, skip_serializing_if = "Map::is_empty")] pub relationships: Map<Key, Relationship>, /// Private field for backwards compatibility. #[serde(skip)] _ext: (), } impl NewObject { /// Returns a new `NewObject`. /// /// # Example /// /// ``` /// # extern crate json_api; /// # /// # use json_api::Error; /// # /// # fn example() -> Result<(), Error> { /// use json_api::doc::NewObject; /// let mut obj = NewObject::new("users".parse()?); /// # Ok(()) /// # } /// # /// # fn main() { /// # example().unwrap(); /// # } /// ``` pub fn new(kind: Key) -> Self { NewObject { kind, id: Default::default(), attributes: Default::default(), links: Default::default(), meta: Default::default(), relationships: Default::default(), _ext: (), } } } impl PrimaryData for NewObject { fn flatten(self, _: &Set<Object>) -> Value { #[cfg_attr(rustfmt, rustfmt_skip)] let NewObject { id, attributes, relationships, .. } = self; let mut map = { let size = attributes.len() + relationships.len() + 1; Map::with_capacity(size) }; if let Some(value) = id { map.insert(Key::from_raw("id".to_owned()), Value::String(value)); } map.extend(attributes); for (key, value) in relationships { let value = match value.data { Data::Member(data) => match *data { Some(Identifier { id, .. }) => Value::String(id), None => Value::Null, }, Data::Collection(data) => { data.into_iter().map(|ident| ident.id).collect() } }; map.insert(key, value); } Value::Object(map) } } impl Render<NewObject> for NewObject { fn render(self, _: Option<&Query>) -> Result<Document<NewObject>, Error> { Ok(Document::Ok { data: Data::Member(Box::new(Some(self))), included: Default::default(), jsonapi: Default::default(), links: Default::default(), meta: Default::default(), }) } } impl Sealed for NewObject {}