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
#[macro_export]
/// DSL for constructing a Relation type.
macro_rules! relation {
    ($rel:ident: $resource:ident as $to_one:expr, $to_many:expr) => {
        struct $rel;

        impl $crate::api::rel::Relation for $rel {
            type Resource = $resource;
            fn to_one() -> &'static str { $to_one }
            fn to_many() -> &'static str { $to_many }
        }
    };
}

#[cfg(test)]
mod tests {
    use api::Resource;
    use api::rel::Relation;
    use Serialize;
    use Serializer;

    struct User;

    impl Serialize for User {
        fn serialize<S: Serializer>(&self, _: &mut S) -> Result<(), S::Error> { unimplemented!() }
    }

    impl Resource for User {
        type Id = u32;
        fn id(&self) -> u32 { unimplemented!() }
        fn resource() -> &'static str { "user" }
        fn resource_plural() -> &'static str { "users" }
    }

    relation!(Author: User as "author", "authors");

    fn assert_rel_to_resource<T: Relation<Resource = U>, U: Resource>() { }

    #[test]
    fn author_is_rel_to_user() {
        assert_rel_to_resource::<Author, User>();
        assert_eq!(Author::to_one(), "author");
        assert_eq!(Author::to_many(), "authors");
    }
}