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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
use std::mem;

use doc::{Data, Document, Identifier, Object};
use error::Error;
use query::Query;
use value::Set;
use value::fields::Key;
use view::{Context, Render};

/// A trait indicating that the given type can be represented as a resource.
///
/// Implementing this trait manually is not recommended. The [`resource!`] macro provides
/// a friendly DSL that implements trait with some additional functionality.
///
/// # Example
///
/// ```
/// #[macro_use]
/// extern crate json_api;
///
/// struct Post(u64);
///
/// resource!(Post, |&self| {
///     kind "posts";
///     id self.0;
/// });
/// #
/// # fn main() {}
/// ```
///
/// [`resource!`]: ./macro.resource.html
pub trait Resource {
    /// Returns a key containing the type of resource.
    ///
    /// # Example
    ///
    /// ```
    /// # #[macro_use]
    /// # extern crate json_api;
    /// #
    /// # struct Post(u64);
    /// #
    /// # resource!(Post, |&self| {
    /// #     kind "posts";
    /// #     id self.0;
    /// # });
    /// #
    /// # fn main() {
    /// use json_api::Resource;
    ///
    /// let kind = Post::kind();
    /// assert_eq!(kind, "posts");
    /// # }
    /// ```
    fn kind() -> Key;

    /// Returns a given resource's id as a string.
    ///
    /// # Example
    ///
    /// ```
    /// # #[macro_use]
    /// # extern crate json_api;
    /// #
    /// # struct Post(u64);
    /// #
    /// # resource!(Post, |&self| {
    /// #     kind "posts";
    /// #     id self.0;
    /// # });
    /// #
    /// # fn main() {
    /// use json_api::Resource;
    ///
    /// let post = Post(25);
    /// assert_eq!(post.id(), "25");
    /// # }
    /// ```
    fn id(&self) -> String;

    /// Renders a given resource as an identifier object.
    ///
    ///
    /// Calling this function directly is not recommended. It is much more ergonomic to
    /// use the [`json_api::to_doc`] function.
    ///
    /// [`json_api::to_doc`]: ./fn.to_doc.html
    fn to_ident(&self, ctx: &mut Context) -> Result<Identifier, Error>;

    /// Renders a given resource as a resource object.
    ///
    /// Calling this function directly is not recommended. It is much more ergonomic to
    /// use the [`json_api::to_doc`] function.
    ///
    /// [`json_api::to_doc`]: ./fn.to_doc.html
    fn to_object(&self, ctx: &mut Context) -> Result<Object, Error>;
}

impl<'a, T: Resource> Render<Identifier> for &'a T {
    fn render(self, query: Option<&Query>) -> Result<Document<Identifier>, Error> {
        let mut incl = Set::new();
        let mut ctx = Context::new(T::kind(), query, &mut incl);

        self.to_ident(&mut ctx)?.render(query)
    }
}

impl<'a, T: Resource> Render<Identifier> for &'a [T] {
    fn render(self, query: Option<&Query>) -> Result<Document<Identifier>, Error> {
        let mut incl = Set::new();
        let mut ctx = Context::new(T::kind(), query, &mut incl);

        self.into_iter()
            .map(|item| item.to_ident(&mut ctx))
            .collect::<Result<Vec<_>, _>>()?
            .render(query)
    }
}

impl<'a, T: Resource> Render<Object> for &'a T {
    fn render(self, query: Option<&Query>) -> Result<Document<Object>, Error> {
        let mut incl = Set::new();
        let (data, links, meta) = {
            let mut ctx = Context::new(T::kind(), query, &mut incl);
            let mut obj = self.to_object(&mut ctx)?;
            let links = mem::replace(&mut obj.links, Default::default());
            let meta = mem::replace(&mut obj.meta, Default::default());

            (obj.into(), links, meta)
        };

        Ok(Document::Ok {
            data,
            links,
            meta,
            included: incl,
            jsonapi: Default::default(),
        })
    }
}

impl<'a, T: Resource> Render<Object> for &'a [T] {
    fn render(self, query: Option<&Query>) -> Result<Document<Object>, Error> {
        let mut incl = Set::new();
        let mut data = Vec::with_capacity(self.len());

        {
            let mut ctx = Context::new(T::kind(), query, &mut incl);

            for item in self {
                data.push(item.to_object(&mut ctx)?);
            }
        }

        Ok(Document::Ok {
            data: Data::Collection(data),
            links: Default::default(),
            meta: Default::default(),
            included: incl,
            jsonapi: Default::default(),
        })
    }
}

/// A DSL for implementing the `Resource` trait.
///
/// # Examples
///
/// The `resource!` macro is both concise and flexible. Many of the keywords are
/// overloaded to provide a higher level of customization when necessary.
///
/// Here is a simple example that simply defines the resources id, kind, attributes, and
/// relationships.
///
/// ```
/// #[macro_use]
/// extern crate json_api;
///
/// struct Post {
///     id: u64,
///     body: String,
///     title: String,
///     author: Option<User>,
///     comments: Vec<Comment>,
/// }
///
/// resource!(Post, |&self| {
///     // Define the id.
///     id self.id;
///
///     // Define the resource "type"
///     kind "posts";
///
///     // Define attributes with a comma seperated list of field names.
///     attrs body, title;
///
///     // Define relationships with a comma seperated list of field names.
///     has_one author;
///     has_many comments;
/// });
/// #
/// # struct User;
/// #
/// # resource!(User, |&self| {
/// #     kind "users";
/// #     id String::new();
/// # });
/// #
/// # struct Comment;
/// #
/// # resource!(Comment, |&self| {
/// #     kind "comments";
/// #     id String::new();
/// # });
/// #
/// # fn main() {}
/// ```
///
/// Now let's take a look at how we can use the same DSL to get a higher level
/// customization.
///
/// ```
/// #[macro_use]
/// extern crate json_api;
///
/// struct Post {
///     id: u64,
///     body: String,
///     title: String,
///     author: Option<User>,
///     comments: Vec<Comment>,
/// }
///
/// resource!(Post, |&self| {
///     kind "articles";
///     id self.id;
///
///     attrs body, title;
///
///     // Define a virtual attribute with an expression
///     attr "preview", {
///         self.body
///             .chars()
///             .take(140)
///             .collect::<String>()
///     }
///
///     // Define a relationship with granular detail
///     has_one "author", {
///         // Data for has one should be Option<&T> where T: Resource
///         data self.author.as_ref();
///
///         // Define relationship links
///         link "self", format!("/articles/{}/relationships/author", self.id);
///         link "related", format!("/articles/{}/author", self.id);
///
///         // Define arbitrary meta members with a block expression
///         meta "read-only", true
///     }
///
///     // Define a relationship with granular detail
///     has_many "comments", {
///         // Data for has one should be an Iterator<Item = &T> where T: Resource
///         data self.comments.iter();
///
///         // Define relationship links
///         link "self", format!("/articles/{}/relationships/comments", self.id);
///         link "related", format!("/articles/{}/comments", self.id);
///
///         // Define arbitrary meta members with a block expression
///         meta "total", {
///             self.comments.len()
///         }
///     }
///
///     // You can also define links with granular details as well
///     link "self", {
///         href format!("/articles/{}", self.id);
///     }
///
///     // Define arbitrary meta members an expression
///     meta "copyright", self.author.as_ref().map(|user| {
///         format!("© 2017 {}", user.full_name())
///     });
/// });
/// #
/// # struct User;
/// #
/// # impl User {
/// #     fn full_name(&self) -> String {
/// #         String::new()
/// #     }
/// # }
/// #
/// # resource!(User, |&self| {
/// #     kind "users";
/// #     id String::new();
/// # });
/// #
/// # struct Comment;
/// #
/// # resource!(Comment, |&self| {
/// #     kind "comments";
/// #     id String::new();
/// # });
/// #
/// # fn main() {}
/// ```
#[macro_export]
macro_rules! resource {
    ($target:ident, |&$this:ident| { $($rest:tt)* }) => {
        impl $crate::Resource for $target {
            fn kind() -> $crate::value::Key {
                let raw = extract_resource_kind!({ $($rest)* }).to_owned();
                $crate::value::Key::from_raw(raw)
            }

            fn id(&$this) -> String {
                use $crate::value::Stringify as JsonApiStringifyTrait;
                extract_resource_id!({ $($rest)* }).stringify()
            }

            fn to_ident(
                &$this,
                _: &mut $crate::view::Context,
            ) -> Result<$crate::doc::Identifier, $crate::Error> {
                let mut ident = {
                    let kind = <$target as $crate::Resource>::kind();
                    let id = $crate::Resource::id($this);

                    $crate::doc::Identifier::new(kind, id)
                };

                {
                    let _meta = &mut ident.meta;
                    expand_resource_impl!(@meta $this, _meta, {
                        $($rest)*
                    });
                }

                Ok(ident)
            }

            fn to_object(
                &$this,
                ctx: &mut $crate::view::Context,
            ) -> Result<$crate::doc::Object, $crate::error::Error> {
                #[allow(dead_code)]
                fn item_kind<T: $crate::Resource>(_: &T) -> $crate::value::Key {
                    T::kind()
                }

                #[allow(dead_code)]
                fn iter_kind<'a, I, T>(_: &I) -> $crate::value::Key
                where
                    I: Iterator<Item = &'a T>,
                    T: $crate::Resource + 'a,
                {
                    T::kind()
                }

                let mut obj = {
                    let kind = <$target as $crate::Resource>::kind();
                    let id = $crate::Resource::id($this);

                    $crate::doc::Object::new(kind, id)
                };

                {
                    let _attrs = &mut obj.attributes;
                    expand_resource_impl!(@attrs $this, _attrs, ctx, {
                        $($rest)*
                    });
                }

                {
                    let _links = &mut obj.links;
                    expand_resource_impl!(@links $this, _links, {
                        $($rest)*
                    });
                }

                {
                    let _meta = &mut obj.meta;
                    expand_resource_impl!(@meta $this, _meta, {
                        $($rest)*
                    });
                }

                {
                    let _related = &mut obj.relationships;
                    expand_resource_impl!(@rel $this, _related, ctx, {
                        $($rest)*
                    });
                }

                Ok(obj)
            }
        }
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! expand_resource_impl {
    (@attrs $this:ident, $attrs:ident, $ctx:ident, {
        attr $key:expr, $value:block
        $($rest:tt)*
    }) => {
        if $ctx.field($key) {
            let key = $key.parse::<$crate::value::Key>()?;
            let value = $crate::to_value($value)?;

            $attrs.insert(key, value);
        }

        expand_resource_impl!(@attrs $this, $attrs, $ctx, {
            $($rest)*
        });
    };

    (@attrs $this:ident, $($arg:ident),*, { attr $field:ident; $($rest:tt)* }) => {
        expand_resource_impl!(@attrs $this, $($arg),*, {
            attr stringify!($field), &$this.$field;
            $($rest)*
        });
    };

    (@attrs $($arg:ident),*, { attrs $($field:ident),+; $($rest:tt)* }) => {
        expand_resource_impl!(@attrs $($arg),*, {
            $(attr $field;)+
            $($rest)*
        });
    };

    (@rel $this:ident, $related:ident, $ctx:ident, {
        has_many $key:expr, { $($body:tt)* }
        $($rest:tt)*
    }) => {
        if $ctx.field($key) {
            let key = $key.parse::<$crate::value::Key>()?;
            expand_resource_impl!(@has_many $this, $related, key, $ctx, {
                $($body)*
            });
        }

        expand_resource_impl!(@rel $this, $related, $ctx, {
            $($rest)*
        });
    };

    (@rel $this:ident, $related:ident, $ctx:ident, {
        has_one $key:expr, { $($body:tt)* }
        $($rest:tt)*
    }) => {
        if $ctx.field($key) {
            let key = $key.parse::<$crate::value::Key>()?;
            expand_resource_impl!(@has_one $this, $related, key, $ctx, {
                $($body)*
            });
        }

        expand_resource_impl!(@rel $this, $related, $ctx, {
            $($rest)*
        });
    };

    (@rel $this:ident, $($arg:ident),*, {
        has_many $($field:ident),*;
        $($rest:tt)*
    }) => {
        expand_resource_impl!(@rel $this, $($arg),*, {
            $(has_many stringify!($field), { data $this.$field.iter(); })*
            $($rest)*
        });
    };

    (@rel $this:ident, $($arg:ident),*, {
        has_one $($field:ident),*;
        $($rest:tt)*
    }) => {
        expand_resource_impl!(@rel $this, $($arg),*, {
            $(has_one stringify!($field), { data $this.$field.as_ref(); })*
            $($rest)*
        });
    };

    (@has_many $this:ident, $related:ident, $key:ident, $ctx:ident, {
        data $value:block
        $($rest:tt)*
    }) => {
        let mut rel = $crate::doc::Relationship::new({
            let mut ctx = $ctx.fork(iter_kind(&$value), &$key);
            let mut data = match $value.size_hint() {
                (_, Some(size)) => Vec::with_capacity(size),
                _ => Vec::new(),
            };

            if ctx.included() {
                for item in $value {
                    let object = $crate::Resource::to_object(item, &mut ctx)?;
                    let ident = $crate::doc::Identifier::from(&object);

                    ctx.include(object);
                    data.push(ident);
                }
            } else {
                for item in $value {
                    data.push($crate::Resource::to_ident(item, &mut ctx)?);
                }
            }

            data.into()
        });

        {
            let links = &mut rel.links;
            expand_resource_impl!(@links $this, links, {
                $($rest)*
            });
        }

        {
            let _meta = &mut rel.meta;
            expand_resource_impl!(@meta $this, _meta, {
                $($rest)*
            });
        }

        $related.insert($key, rel);
    };

    (@has_one $this:ident, $related:ident, $key:ident, $ctx:ident, {
        data $value:block
        $($rest:tt)*
    }) => {
        let mut rel = $crate::doc::Relationship::new({
            let mut data = None;

            if let Some(item) = $value {
                let mut ctx = $ctx.fork(item_kind(item), &$key);

                data = Some($crate::Resource::to_ident(item, &mut ctx)?);

                if ctx.included() {
                    let object = $crate::Resource::to_object(item, &mut ctx)?;
                    ctx.include(object);
                }
            }

            data.into()
        });

        {
            let _links = &mut rel.links;
            expand_resource_impl!(@links $this, _links, {
                $($rest)*
            });
        }

        {
            let _meta = &mut rel.meta;
            expand_resource_impl!(@meta $this, _meta, {
                $($rest)*
            });
        }

        $related.insert($key, rel);
    };

    (@links $this:ident, $links:ident, {
        link $key:expr, { $($body:tt)* }
        $($rest:tt)*
    }) => {
        {
            let key = $key.parse::<$crate::value::Key>()?;
            let link = expand_resource_impl!(@link $this, {
                $($body)*
            });

            $links.insert(key, link);
        }

        expand_resource_impl!(@links $this, $links, {
            $($rest)*
        });
    };

    (@links $($args:ident),+, {
        link $key:expr, $value:expr;
        $($rest:tt)*
    }) => {
        expand_resource_impl!(@links $($args),+, {
            link $key, { href { $value } }
            $($rest)*
        });
    };

    (@link $this:ident, { href $value:block $($rest:tt)* }) => {{
        let mut link = $value.parse::<$crate::doc::Link>()?;

        {
            let _meta = &link.meta;
            expand_resource_impl!(@meta $this, _meta, {
                $($rest)*
            });
        }

        link
    }};

    (@meta $this:ident, $meta:ident, {
        meta $key:expr, $value:block
        $($rest:tt)*
    }) => {
        {
            let key = $key.parse::<$crate::value::Key>()?;
            let value = $crate::to_value($value)?;

            $meta.insert(key, value);
        }

        expand_resource_impl!(@meta $this, $meta, {
            $($rest)*
        });
    };

    // Ignore has_many specific syntax in other scopes.
    (@$scope:tt $($args:ident),+, {
        has_many $key:expr, { $($body:tt)* }
        $($rest:tt)*
    }) => {
        expand_resource_impl!(@$scope $($args),+, {
            $($rest)*
        });
    };

    // Ignore has_one specific syntax in other scopes.
    (@$scope:tt $($args:ident),+, {
        has_one $key:expr, { $($body:tt)* }
        $($rest:tt)*
    }) => {
        expand_resource_impl!(@$scope $($args),+, {
            $($rest)*
        });
    };

    // Ignore link specific syntax in other scopes.
    (@$scope:tt $($args:ident),+, {
        link $key:expr, { $($body:tt)* }
        $($rest:tt)*
    }) => {
        expand_resource_impl!(@$scope $($args),+, {
            $($rest)*
        });
    };

    (@$scope:tt $($args:ident),+, {
        $kwd:ident $value:expr;
        $($rest:tt)*
    }) => {
        expand_resource_impl!(@$scope $($args),+, {
            $kwd { $value }
            $($rest)*
        });
    };

    (@$scope:tt $($args:ident),+, {
        has_many $key:expr, $value:block
        $($rest:tt)*
    }) => {
        expand_resource_impl!(@$scope $($args),+, {
            $($rest)*
        });
    };

    (@$scope:tt $($args:ident),+, {
        has_one $key:expr, $value:block
        $($rest:tt)*
    }) => {
        expand_resource_impl!(@$scope $($args),+, {
            $($rest)*
        });
    };

    (@$scope:tt $($args:ident),+, {
        link $key:expr, $value:block
        $($rest:tt)*
    }) => {
        expand_resource_impl!(@$scope $($args),+, {
            $($rest)*
        });
    };

    (@$scope:tt $($args:ident),+, {
        $kwd:ident $key:expr, $value:expr;
        $($rest:tt)*
    }) => {
        expand_resource_impl!(@$scope $($args),+, {
            $kwd $key, { $value }
            $($rest)*
        });
    };

    (@$scope:tt $($args:ident),+, {
        $skip:tt
        $($rest:tt)*
    }) => {
        expand_resource_impl!(@$scope $($args),+, {
            $($rest)*
        });
    };

    ($($rest:tt)*) => ();
}

#[doc(hidden)]
#[macro_export]
macro_rules! extract_resource_id {
    ({ id $value:block $($rest:tt)* }) => { $value };
    ({ id $value:expr; $($rest:tt)* }) => { $value };
    ({ $skip:tt $($rest:tt)* }) => { extract_resource_id!({ $($rest)* }) };
    ({ $($rest:tt)* }) => ();
}

#[doc(hidden)]
#[macro_export]
macro_rules! extract_resource_kind {
    ({ kind $value:block $($rest:tt)* }) => { $value };
    ({ kind $value:expr; $($rest:tt)* }) => { $value };
    ({ $skip:tt $($rest:tt)* }) => { extract_resource_kind!({ $($rest)* }) };
    ({ $($rest:tt)* }) => ();
}