1
1
use serde:: { Deserialize , Serialize } ;
2
2
use wp_contextual:: WpContextual ;
3
3
4
- use crate :: { UserId , WpApiParamOrder } ;
4
+ use crate :: {
5
+ impl_as_query_value_for_new_type, impl_as_query_value_from_as_str,
6
+ url_query:: { AppendUrlQueryPairs , AsQueryValue , QueryPairs , QueryPairsExtension } ,
7
+ UserId , WpApiParamOrder ,
8
+ } ;
5
9
6
10
#[ derive( Debug , Default , Clone , Copy , PartialEq , Eq , uniffi:: Enum ) ]
7
11
pub enum WpApiParamPostsOrderBy {
@@ -18,6 +22,8 @@ pub enum WpApiParamPostsOrderBy {
18
22
Title ,
19
23
}
20
24
25
+ impl_as_query_value_from_as_str ! ( WpApiParamPostsOrderBy ) ;
26
+
21
27
impl WpApiParamPostsOrderBy {
22
28
fn as_str ( & self ) -> & str {
23
29
match self {
@@ -41,6 +47,8 @@ pub enum WpApiParamPostsTaxRelation {
41
47
Or ,
42
48
}
43
49
50
+ impl_as_query_value_from_as_str ! ( WpApiParamPostsTaxRelation ) ;
51
+
44
52
impl WpApiParamPostsTaxRelation {
45
53
fn as_str ( & self ) -> & str {
46
54
match self {
@@ -57,6 +65,8 @@ pub enum WpApiParamPostsSearchColumn {
57
65
PostTitle ,
58
66
}
59
67
68
+ impl_as_query_value_from_as_str ! ( WpApiParamPostsSearchColumn ) ;
69
+
60
70
impl WpApiParamPostsSearchColumn {
61
71
fn as_str ( & self ) -> & str {
62
72
match self {
@@ -148,152 +158,46 @@ pub struct PostListParams {
148
158
pub sticky : Option < bool > ,
149
159
}
150
160
151
- impl PostListParams {
152
- pub fn query_pairs ( & self ) -> impl IntoIterator < Item = ( & str , String ) > {
153
- [
154
- ( "page" , self . page . map ( |x| x. to_string ( ) ) ) ,
155
- ( "per_page" , self . per_page . map ( |x| x. to_string ( ) ) ) ,
156
- ( "search" , self . search . clone ( ) ) ,
157
- ( "after" , self . after . clone ( ) ) ,
158
- ( "modified_after" , self . modified_after . clone ( ) ) ,
159
- (
160
- "author" ,
161
- ( !self . author . is_empty ( ) ) . then_some (
162
- self . author
163
- . iter ( )
164
- . map ( |x| x. to_string ( ) )
165
- . collect :: < Vec < String > > ( )
166
- . join ( "," ) ,
167
- ) ,
168
- ) ,
169
- (
170
- "author_exclude" ,
171
- ( !self . author_exclude . is_empty ( ) ) . then_some (
172
- self . author_exclude
173
- . iter ( )
174
- . map ( |x| x. to_string ( ) )
175
- . collect :: < Vec < String > > ( )
176
- . join ( "," ) ,
177
- ) ,
178
- ) ,
179
- ( "before" , self . before . clone ( ) ) ,
180
- ( "modified_before" , self . modified_before . clone ( ) ) ,
181
- (
182
- "exclude" ,
183
- ( !self . exclude . is_empty ( ) ) . then_some (
184
- self . exclude
185
- . iter ( )
186
- . map ( |x| x. to_string ( ) )
187
- . collect :: < Vec < String > > ( )
188
- . join ( "," ) ,
189
- ) ,
190
- ) ,
191
- (
192
- "include" ,
193
- ( !self . include . is_empty ( ) ) . then_some (
194
- self . include
195
- . iter ( )
196
- . map ( |x| x. to_string ( ) )
197
- . collect :: < Vec < String > > ( )
198
- . join ( "," ) ,
199
- ) ,
200
- ) ,
201
- ( "offset" , self . offset . map ( |x| x. to_string ( ) ) ) ,
202
- ( "order" , self . order . as_ref ( ) . map ( |x| x. as_str ( ) . to_string ( ) ) ) ,
203
- (
204
- "orderby" ,
205
- self . orderby . as_ref ( ) . map ( |x| x. as_str ( ) . to_string ( ) ) ,
206
- ) ,
207
- (
208
- "search_columns" ,
209
- ( !self . search_columns . is_empty ( ) ) . then_some (
210
- self . search_columns
211
- . iter ( )
212
- . map ( |x| x. as_str ( ) . to_string ( ) )
213
- . collect :: < Vec < String > > ( )
214
- . join ( "," ) ,
215
- ) ,
216
- ) ,
217
- (
218
- "slug" ,
219
- ( !self . slug . is_empty ( ) ) . then_some (
220
- self . slug
221
- . iter ( )
222
- . map ( |x| x. to_string ( ) )
223
- . collect :: < Vec < String > > ( )
224
- . join ( "," ) ,
225
- ) ,
226
- ) ,
227
- (
228
- "status" ,
229
- ( !self . status . is_empty ( ) ) . then_some (
230
- self . status
231
- . iter ( )
232
- . map ( |x| x. as_str ( ) . to_string ( ) )
233
- . collect :: < Vec < String > > ( )
234
- . join ( "," ) ,
235
- ) ,
236
- ) ,
237
- (
238
- "tax_relation" ,
239
- self . tax_relation . as_ref ( ) . map ( |x| x. as_str ( ) . to_string ( ) ) ,
240
- ) ,
241
- (
242
- "categories" ,
243
- ( !self . categories . is_empty ( ) ) . then_some (
244
- self . categories
245
- . iter ( )
246
- . map ( |x| x. 0 . to_string ( ) )
247
- . collect :: < Vec < String > > ( )
248
- . join ( "," ) ,
249
- ) ,
250
- ) ,
251
- (
252
- "categories_exclude" ,
253
- ( !self . categories_exclude . is_empty ( ) ) . then_some (
254
- self . categories_exclude
255
- . iter ( )
256
- . map ( |x| x. 0 . to_string ( ) )
257
- . collect :: < Vec < String > > ( )
258
- . join ( "," ) ,
259
- ) ,
260
- ) ,
261
- (
262
- "tags" ,
263
- ( !self . tags . is_empty ( ) ) . then_some (
264
- self . tags
265
- . iter ( )
266
- . map ( |x| x. 0 . to_string ( ) )
267
- . collect :: < Vec < String > > ( )
268
- . join ( "," ) ,
269
- ) ,
270
- ) ,
271
- (
272
- "tags_exclude" ,
273
- ( !self . tags_exclude . is_empty ( ) ) . then_some (
274
- self . tags_exclude
275
- . iter ( )
276
- . map ( |x| x. 0 . to_string ( ) )
277
- . collect :: < Vec < String > > ( )
278
- . join ( "," ) ,
279
- ) ,
280
- ) ,
281
- ( "sticky" , self . sticky . map ( |x| x. to_string ( ) ) ) ,
282
- ]
283
- . into_iter ( )
284
- // Remove `None` values
285
- . filter_map ( |( k, opt_v) | opt_v. map ( |v| ( k, v) ) )
161
+ impl AppendUrlQueryPairs for PostListParams {
162
+ fn append_query_pairs ( & self , query_pairs_mut : & mut QueryPairs ) {
163
+ query_pairs_mut
164
+ . append_option_query_value_pair ( "page" , self . page . as_ref ( ) )
165
+ . append_option_query_value_pair ( "per_page" , self . per_page . as_ref ( ) )
166
+ . append_option_query_value_pair ( "search" , self . search . as_ref ( ) )
167
+ . append_option_query_value_pair ( "after" , self . after . as_ref ( ) )
168
+ . append_option_query_value_pair ( "modified_after" , self . modified_after . as_ref ( ) )
169
+ . append_vec_query_value_pair ( "author" , & self . author )
170
+ . append_vec_query_value_pair ( "author_exclude" , & self . author_exclude )
171
+ . append_option_query_value_pair ( "before" , self . before . as_ref ( ) )
172
+ . append_option_query_value_pair ( "modified_before" , self . modified_before . as_ref ( ) )
173
+ . append_vec_query_value_pair ( "exclude" , & self . exclude )
174
+ . append_vec_query_value_pair ( "include" , & self . include )
175
+ . append_option_query_value_pair ( "offset" , self . offset . as_ref ( ) )
176
+ . append_option_query_value_pair ( "order" , self . order . as_ref ( ) )
177
+ . append_option_query_value_pair ( "orderby" , self . orderby . as_ref ( ) )
178
+ . append_vec_query_value_pair ( "search_columns" , & self . search_columns )
179
+ . append_vec_query_value_pair ( "slug" , & self . slug )
180
+ . append_vec_query_value_pair ( "status" , & self . status )
181
+ . append_option_query_value_pair ( "tax_relation" , self . tax_relation . as_ref ( ) )
182
+ . append_vec_query_value_pair ( "categories" , & self . categories )
183
+ . append_vec_query_value_pair ( "categories_exclude" , & self . categories_exclude )
184
+ . append_vec_query_value_pair ( "tags" , & self . tags )
185
+ . append_vec_query_value_pair ( "tags_exclude" , & self . tags_exclude )
186
+ . append_option_query_value_pair ( "sticky" , self . sticky . as_ref ( ) ) ;
286
187
}
287
188
}
288
189
190
+ impl_as_query_value_for_new_type ! ( PostId ) ;
289
191
uniffi:: custom_newtype!( PostId , i32 ) ;
290
192
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Serialize , Deserialize ) ]
291
193
pub struct PostId ( pub i32 ) ;
292
194
195
+ impl_as_query_value_for_new_type ! ( TagId ) ;
293
196
uniffi:: custom_newtype!( TagId , i32 ) ;
294
197
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Serialize , Deserialize ) ]
295
198
pub struct TagId ( pub i32 ) ;
296
199
200
+ impl_as_query_value_for_new_type ! ( CategoryId ) ;
297
201
uniffi:: custom_newtype!( CategoryId , i32 ) ;
298
202
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Serialize , Deserialize ) ]
299
203
pub struct CategoryId ( pub i32 ) ;
@@ -413,6 +317,8 @@ pub enum PostStatus {
413
317
Custom ( String ) ,
414
318
}
415
319
320
+ impl_as_query_value_from_as_str ! ( PostStatus ) ;
321
+
416
322
impl PostStatus {
417
323
fn as_str ( & self ) -> & str {
418
324
match self {
0 commit comments