@@ -4,25 +4,28 @@ use errors::*;
4
4
use serde:: { Deserialize , Serialize } ;
5
5
use serde_json:: { from_value, to_value, Value , Map } ;
6
6
7
- /* A trait for any struct that can be converted from/into a Resource.
8
- * The only requirement is that your struct has a field that implements
9
- * `ToString` and can be used as an ID. (not necesarilly called ID though).
10
- * You shouldn't be implementing JsonApiModel manually, look at the
11
- * `jsonapi_model` macro instead.
12
- */
7
+ /// A trait for any struct that can be converted from/into a Resource.
8
+ /// The only requirement is that your struct has an 'id: String' field.
9
+ /// You shouldn't be implementing JsonApiModel manually, look at the
10
+ /// `jsonapi_model!` macro instead.
13
11
pub trait JsonApiModel : Serialize
14
12
where for < ' de > Self : Deserialize < ' de >
15
13
{
14
+ #[ doc( hidden) ]
16
15
fn jsonapi_type ( & self ) -> String ;
16
+ #[ doc( hidden) ]
17
17
fn jsonapi_id ( & self ) -> String ;
18
+ #[ doc( hidden) ]
18
19
fn relationship_fields ( ) -> Option < & ' static [ & ' static str ] > ;
20
+ #[ doc( hidden) ]
19
21
fn build_relationships ( & self ) -> Option < Relationships > ;
22
+ #[ doc( hidden) ]
20
23
fn build_included ( & self ) -> Option < Resources > ;
21
24
22
25
fn from_jsonapi_resource ( resource : & Resource , included : & Option < Resources > )
23
26
-> Result < Self >
24
27
{
25
- Self :: from_serializable ( Self :: resource_to_attrs ( & resource, & included) )
28
+ Self :: from_serializable ( Self :: resource_to_attrs ( resource, included) )
26
29
}
27
30
28
31
fn from_jsonapi_document ( doc : & JsonApiDocument ) -> Result < Self > {
@@ -31,11 +34,11 @@ pub trait JsonApiModel: Serialize
31
34
match * primary_data {
32
35
PrimaryData :: None => bail ! ( "Document had no data" ) ,
33
36
PrimaryData :: Single ( ref resource) =>
34
- Self :: from_jsonapi_resource ( & resource, & doc. included ) ,
37
+ Self :: from_jsonapi_resource ( resource, & doc. included ) ,
35
38
PrimaryData :: Multiple ( ref resources) => {
36
39
let all: Vec < ResourceAttributes > = resources
37
40
. iter ( )
38
- . map ( |r| Self :: resource_to_attrs ( & r, & doc. included ) )
41
+ . map ( |r| Self :: resource_to_attrs ( r, & doc. included ) )
39
42
. collect ( ) ;
40
43
Self :: from_serializable ( all)
41
44
}
@@ -76,14 +79,16 @@ pub trait JsonApiModel: Serialize
76
79
}
77
80
78
81
82
+ #[ doc( hidden) ]
79
83
fn build_has_one < M : JsonApiModel > ( model : & M ) -> Relationship {
80
84
Relationship {
81
85
data : IdentifierData :: Single ( model. as_resource_identifier ( ) ) ,
82
86
links : None
83
87
}
84
88
}
85
89
86
- fn build_has_many < M : JsonApiModel > ( models : & Vec < M > ) -> Relationship {
90
+ #[ doc( hidden) ]
91
+ fn build_has_many < M : JsonApiModel > ( models : & [ M ] ) -> Relationship {
87
92
Relationship {
88
93
data : IdentifierData :: Multiple (
89
94
models. iter ( ) . map ( |m| m. as_resource_identifier ( ) ) . collect ( )
@@ -92,6 +97,7 @@ pub trait JsonApiModel: Serialize
92
97
}
93
98
}
94
99
100
+ #[ doc( hidden) ]
95
101
fn as_resource_identifier ( & self ) -> ResourceIdentifier {
96
102
ResourceIdentifier {
97
103
_type : self . jsonapi_type ( ) ,
@@ -103,17 +109,19 @@ pub trait JsonApiModel: Serialize
103
109
* before calling this, so there's no need to ignore it like we do
104
110
* with the attributes that correspond with relationships.
105
111
* */
112
+ #[ doc( hidden) ]
106
113
fn extract_attributes ( attrs : & Map < String , Value > ) -> ResourceAttributes {
107
114
attrs. iter ( ) . filter ( |& ( key, _) |{
108
- if let Some ( ref fields) = Self :: relationship_fields ( ) {
115
+ if let Some ( fields) = Self :: relationship_fields ( ) {
109
116
if fields. contains ( & key. as_str ( ) ) {
110
117
return false ;
111
118
}
112
119
}
113
- return true ;
120
+ true
114
121
} ) . map ( |( k, v) |{ ( k. clone ( ) , v. clone ( ) ) } ) . collect ( )
115
122
}
116
123
124
+ #[ doc( hidden) ]
117
125
fn to_resources ( & self ) -> Resources {
118
126
let ( me, maybe_others) = self . to_jsonapi_resource ( ) ;
119
127
let mut flattened = vec ! [ me] ;
@@ -123,17 +131,19 @@ pub trait JsonApiModel: Serialize
123
131
flattened
124
132
}
125
133
126
- fn lookup < ' a > ( needle : & ResourceIdentifier , haystack : & ' a Resources )
134
+ #[ doc( hidden) ]
135
+ fn lookup < ' a > ( needle : & ResourceIdentifier , haystack : & ' a [ Resource ] )
127
136
-> Option < & ' a Resource >
128
137
{
129
138
for resource in haystack {
130
139
if resource. _type == needle. _type && resource. id == needle. id {
131
- return Some ( & resource)
140
+ return Some ( resource)
132
141
}
133
142
}
134
143
None
135
144
}
136
145
146
+ #[ doc( hidden) ]
137
147
fn resource_to_attrs ( resource : & Resource , included : & Option < Resources > )
138
148
-> ResourceAttributes
139
149
{
@@ -148,15 +158,15 @@ pub trait JsonApiModel: Serialize
148
158
IdentifierData :: None => Value :: Null ,
149
159
IdentifierData :: Single ( ref identifier) => {
150
160
let found = Self :: lookup ( identifier, inc)
151
- . map ( |r| Self :: resource_to_attrs ( r, & included) ) ;
161
+ . map ( |r| Self :: resource_to_attrs ( r, included) ) ;
152
162
to_value ( found)
153
163
. expect ( "Casting Single relation to value" )
154
164
} ,
155
165
IdentifierData :: Multiple ( ref identifiers) => {
156
166
let found: Vec < Option < ResourceAttributes > > =
157
167
identifiers. iter ( ) . map ( |id|{
158
168
Self :: lookup ( id, inc) . map ( |r|{
159
- Self :: resource_to_attrs ( r, & included)
169
+ Self :: resource_to_attrs ( r, included)
160
170
} )
161
171
} ) . collect ( ) ;
162
172
to_value ( found)
@@ -171,6 +181,7 @@ pub trait JsonApiModel: Serialize
171
181
new_attrs
172
182
}
173
183
184
+ #[ doc( hidden) ]
174
185
fn from_serializable < S : Serialize > ( s : S ) -> Result < Self > {
175
186
from_value ( to_value ( s) . unwrap ( ) )
176
187
. chain_err ( || "Error casting via serde_json" )
0 commit comments