Skip to content

Commit d92e4d8

Browse files
authored
Implement WpDeriveParamsField macro for automatic enum generation (#870)
Replace manual *ListParamsField enum implementations with a procedural macro that automatically generates field enums from struct definitions. The macro generates enums with: - Automatic PascalCase conversion for variant names - Support for #[field_name("custom_name")] attribute for custom serialization names - Proper strum attributes for field serialization Changes: - Add wp_derive::WpDeriveParamsField procedural macro in params_field.rs - Replace manual enum implementations across 12 API parameter structs - Remove redundant IntoStaticStr imports (now handled by macro) - Add comprehensive test suite with pass/fail scenarios - Update dependencies to include convert_case and strum_macros
1 parent 5f52728 commit d92e4d8

21 files changed

+275
-314
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wp_api/src/categories.rs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use crate::{
88
wp_content_i64_id,
99
};
1010
use serde::{Deserialize, Serialize};
11-
use strum_macros::IntoStaticStr;
1211
use wp_contextual::WpContextual;
12+
use wp_derive::WpDeriveParamsField;
1313

1414
wp_content_i64_id!(CategoryId);
1515

@@ -39,7 +39,7 @@ pub enum WpApiParamCategoriesOrderBy {
3939

4040
impl_as_query_value_from_to_string!(WpApiParamCategoriesOrderBy);
4141

42-
#[derive(Debug, Default, PartialEq, Eq, uniffi::Record)]
42+
#[derive(Debug, Default, PartialEq, Eq, uniffi::Record, WpDeriveParamsField)]
4343
pub struct CategoryListParams {
4444
/// Current page of the collection.
4545
/// Default: `1`
@@ -70,6 +70,7 @@ pub struct CategoryListParams {
7070
/// Default: `name`
7171
/// One of: `id`, `include`, `name`, `slug`, `include_slugs`, `term_group`, `description`, `count`
7272
#[uniffi(default = None)]
73+
#[field_name("orderby")]
7374
pub orderby: Option<WpApiParamCategoriesOrderBy>,
7475
/// Whether to hide terms not assigned to any posts.
7576
#[uniffi(default = None)]
@@ -85,34 +86,6 @@ pub struct CategoryListParams {
8586
pub slug: Vec<String>,
8687
}
8788

88-
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, IntoStaticStr)]
89-
enum CategoryListParamsField {
90-
#[strum(serialize = "page")]
91-
Page,
92-
#[strum(serialize = "per_page")]
93-
PerPage,
94-
#[strum(serialize = "search")]
95-
Search,
96-
#[strum(serialize = "exclude")]
97-
Exclude,
98-
#[strum(serialize = "include")]
99-
Include,
100-
#[strum(serialize = "offset")]
101-
Offset,
102-
#[strum(serialize = "order")]
103-
Order,
104-
#[strum(serialize = "orderby")]
105-
Orderby,
106-
#[strum(serialize = "hide_empty")]
107-
HideEmpty,
108-
#[strum(serialize = "parent")]
109-
Parent,
110-
#[strum(serialize = "post")]
111-
Post,
112-
#[strum(serialize = "slug")]
113-
Slug,
114-
}
115-
11689
impl AppendUrlQueryPairs for CategoryListParams {
11790
fn append_query_pairs(&self, query_pairs_mut: &mut QueryPairs) {
11891
query_pairs_mut

wp_api/src/comments.rs

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::{
1010
};
1111
use serde::{Deserialize, Serialize};
1212
use std::{collections::HashMap, str::FromStr, sync::Arc};
13-
use strum_macros::IntoStaticStr;
1413
use wp_contextual::WpContextual;
14+
use wp_derive::WpDeriveParamsField;
1515

1616
#[derive(
1717
Debug,
@@ -69,7 +69,7 @@ pub enum CommentType {
6969

7070
impl_as_query_value_from_to_string!(CommentType);
7171

72-
#[derive(Debug, Default, PartialEq, Eq, uniffi::Record)]
72+
#[derive(Debug, Default, PartialEq, Eq, uniffi::Record, WpDeriveParamsField)]
7373
pub struct CommentListParams {
7474
/// Current page of the collection.
7575
/// Default: `1`
@@ -115,6 +115,7 @@ pub struct CommentListParams {
115115
/// Default: date_gmt
116116
/// One of: date, date_gmt, id, include, post, parent, type
117117
#[uniffi(default = None)]
118+
#[field_name("orderby")]
118119
pub orderby: Option<WpApiParamCommentsOrderBy>,
119120
/// Limit result set to comments of specific parent IDs.
120121
#[uniffi(default = [])]
@@ -132,54 +133,13 @@ pub struct CommentListParams {
132133
/// Limit result set to comments assigned a specific type. Requires authorization.
133134
/// Default: comment
134135
#[uniffi(default = None)]
136+
#[field_name("type")]
135137
pub comment_type: Option<CommentType>,
136138
/// The password for the post if it is password protected.
137139
#[uniffi(default = None)]
138140
pub password: Option<String>,
139141
}
140142

141-
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, IntoStaticStr)]
142-
enum CommentListParamsField {
143-
#[strum(serialize = "page")]
144-
Page,
145-
#[strum(serialize = "per_page")]
146-
PerPage,
147-
#[strum(serialize = "search")]
148-
Search,
149-
#[strum(serialize = "after")]
150-
After,
151-
#[strum(serialize = "author")]
152-
Author,
153-
#[strum(serialize = "author_exclude")]
154-
AuthorExclude,
155-
#[strum(serialize = "author_email")]
156-
AuthorEmail,
157-
#[strum(serialize = "before")]
158-
Before,
159-
#[strum(serialize = "exclude")]
160-
Exclude,
161-
#[strum(serialize = "include")]
162-
Include,
163-
#[strum(serialize = "offset")]
164-
Offset,
165-
#[strum(serialize = "order")]
166-
Order,
167-
#[strum(serialize = "orderby")]
168-
Orderby,
169-
#[strum(serialize = "parent")]
170-
Parent,
171-
#[strum(serialize = "parent_exclude")]
172-
ParentExclude,
173-
#[strum(serialize = "post")]
174-
Post,
175-
#[strum(serialize = "status")]
176-
Status,
177-
#[strum(serialize = "type")]
178-
CommentType,
179-
#[strum(serialize = "password")]
180-
Password,
181-
}
182-
183143
impl AppendUrlQueryPairs for CommentListParams {
184144
fn append_query_pairs(&self, query_pairs_mut: &mut QueryPairs) {
185145
query_pairs_mut

wp_api/src/media.rs

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use serde::{Deserialize, Serialize};
1515
use serde_json::value::RawValue;
1616
use std::collections::HashMap;
1717
use std::sync::Arc;
18-
use strum_macros::IntoStaticStr;
1918
use wp_contextual::WpContextual;
19+
use wp_derive::WpDeriveParamsField;
2020

2121
wp_content_i64_id!(MediaId);
2222

@@ -107,7 +107,7 @@ pub enum MediaStatus {
107107

108108
impl_as_query_value_from_to_string!(MediaStatus);
109109

110-
#[derive(Debug, Default, PartialEq, Eq, uniffi::Record)]
110+
#[derive(Debug, Default, PartialEq, Eq, uniffi::Record, WpDeriveParamsField)]
111111
pub struct MediaListParams {
112112
/// Current page of the collection.
113113
/// Default: `1`
@@ -156,6 +156,7 @@ pub struct MediaListParams {
156156
/// Default: date
157157
/// One of: author, date, id, include, modified, parent, relevance, slug, include_slugs, title
158158
#[uniffi(default = None)]
159+
#[field_name("orderby")]
159160
pub orderby: Option<WpApiParamPostsOrderBy>,
160161
/// Limit result set to items with particular parent IDs.
161162
#[uniffi(default = [])]
@@ -182,52 +183,6 @@ pub struct MediaListParams {
182183
pub mime_type: Option<String>,
183184
}
184185

185-
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, IntoStaticStr)]
186-
enum MediaListParamsField {
187-
#[strum(serialize = "page")]
188-
Page,
189-
#[strum(serialize = "per_page")]
190-
PerPage,
191-
#[strum(serialize = "search")]
192-
Search,
193-
#[strum(serialize = "after")]
194-
After,
195-
#[strum(serialize = "modified_after")]
196-
ModifiedAfter,
197-
#[strum(serialize = "author")]
198-
Author,
199-
#[strum(serialize = "author_exclude")]
200-
AuthorExclude,
201-
#[strum(serialize = "before")]
202-
Before,
203-
#[strum(serialize = "modified_before")]
204-
ModifiedBefore,
205-
#[strum(serialize = "exclude")]
206-
Exclude,
207-
#[strum(serialize = "include")]
208-
Include,
209-
#[strum(serialize = "offset")]
210-
Offset,
211-
#[strum(serialize = "order")]
212-
Order,
213-
#[strum(serialize = "orderby")]
214-
Orderby,
215-
#[strum(serialize = "parent")]
216-
Parent,
217-
#[strum(serialize = "parent_exclude")]
218-
ParentExclude,
219-
#[strum(serialize = "search_columns")]
220-
SearchColumns,
221-
#[strum(serialize = "slug")]
222-
Slug,
223-
#[strum(serialize = "status")]
224-
Status,
225-
#[strum(serialize = "media_type")]
226-
MediaType,
227-
#[strum(serialize = "mime_type")]
228-
MimeType,
229-
}
230-
231186
impl AppendUrlQueryPairs for MediaListParams {
232187
fn append_query_pairs(&self, query_pairs_mut: &mut QueryPairs) {
233188
query_pairs_mut

wp_api/src/post_revisions.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::{
99
wp_content_i64_id,
1010
};
1111
use serde::{Deserialize, Serialize};
12-
use strum_macros::IntoStaticStr;
1312
use wp_contextual::WpContextual;
13+
use wp_derive::WpDeriveParamsField;
1414

1515
wp_content_i64_id!(PostRevisionId);
1616

@@ -39,7 +39,7 @@ pub enum WpApiParamPostRevisionsOrderBy {
3939

4040
impl_as_query_value_from_to_string!(WpApiParamPostRevisionsOrderBy);
4141

42-
#[derive(Debug, Default, PartialEq, Eq, uniffi::Record)]
42+
#[derive(Debug, Default, PartialEq, Eq, uniffi::Record, WpDeriveParamsField)]
4343
pub struct PostRevisionListParams {
4444
/// Current page of the collection.
4545
/// Default: `1`
@@ -69,29 +69,10 @@ pub struct PostRevisionListParams {
6969
/// Default: date
7070
/// One of: date, id, include, relevance, slug, include_slugs, title
7171
#[uniffi(default = None)]
72+
#[field_name("orderby")]
7273
pub orderby: Option<WpApiParamPostRevisionsOrderBy>,
7374
}
7475

75-
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, IntoStaticStr)]
76-
enum PostRevisionListParamsField {
77-
#[strum(serialize = "page")]
78-
Page,
79-
#[strum(serialize = "per_page")]
80-
PerPage,
81-
#[strum(serialize = "search")]
82-
Search,
83-
#[strum(serialize = "exclude")]
84-
Exclude,
85-
#[strum(serialize = "include")]
86-
Include,
87-
#[strum(serialize = "offset")]
88-
Offset,
89-
#[strum(serialize = "order")]
90-
Order,
91-
#[strum(serialize = "orderby")]
92-
Orderby,
93-
}
94-
9576
impl AppendUrlQueryPairs for PostRevisionListParams {
9677
fn append_query_pairs(&self, query_pairs_mut: &mut QueryPairs) {
9778
query_pairs_mut

wp_api/src/posts.rs

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::{
1111
wp_content_i64_id,
1212
};
1313
use serde::{Deserialize, Serialize};
14-
use strum_macros::IntoStaticStr;
1514
use wp_contextual::WpContextual;
15+
use wp_derive::WpDeriveParamsField;
1616
use wp_serde_helper::{deserialize_from_string_of_json_array, serialize_as_json_string};
1717

1818
#[derive(
@@ -67,7 +67,7 @@ pub enum WpApiParamPostsSearchColumn {
6767

6868
impl_as_query_value_from_to_string!(WpApiParamPostsSearchColumn);
6969

70-
#[derive(Debug, Default, PartialEq, Eq, uniffi::Record)]
70+
#[derive(Debug, Default, PartialEq, Eq, uniffi::Record, WpDeriveParamsField)]
7171
pub struct PostListParams {
7272
/// Current page of the collection.
7373
/// Default: `1`
@@ -116,6 +116,7 @@ pub struct PostListParams {
116116
/// Default: date
117117
/// One of: author, date, id, include, modified, parent, relevance, slug, include_slugs, title
118118
#[uniffi(default = None)]
119+
#[field_name("orderby")]
119120
pub orderby: Option<WpApiParamPostsOrderBy>,
120121
/// Array of column names to be searched.
121122
#[uniffi(default = [])]
@@ -148,56 +149,6 @@ pub struct PostListParams {
148149
pub sticky: Option<bool>,
149150
}
150151

151-
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, IntoStaticStr)]
152-
enum PostListParamsField {
153-
#[strum(serialize = "page")]
154-
Page,
155-
#[strum(serialize = "per_page")]
156-
PerPage,
157-
#[strum(serialize = "search")]
158-
Search,
159-
#[strum(serialize = "after")]
160-
After,
161-
#[strum(serialize = "modified_after")]
162-
ModifiedAfter,
163-
#[strum(serialize = "author")]
164-
Author,
165-
#[strum(serialize = "author_exclude")]
166-
AuthorExclude,
167-
#[strum(serialize = "before")]
168-
Before,
169-
#[strum(serialize = "modified_before")]
170-
ModifiedBefore,
171-
#[strum(serialize = "exclude")]
172-
Exclude,
173-
#[strum(serialize = "include")]
174-
Include,
175-
#[strum(serialize = "offset")]
176-
Offset,
177-
#[strum(serialize = "order")]
178-
Order,
179-
#[strum(serialize = "orderby")]
180-
Orderby,
181-
#[strum(serialize = "search_columns")]
182-
SearchColumns,
183-
#[strum(serialize = "slug")]
184-
Slug,
185-
#[strum(serialize = "status")]
186-
Status,
187-
#[strum(serialize = "tax_relation")]
188-
TaxRelation,
189-
#[strum(serialize = "categories")]
190-
Categories,
191-
#[strum(serialize = "categories_exclude")]
192-
CategoriesExclude,
193-
#[strum(serialize = "tags")]
194-
Tags,
195-
#[strum(serialize = "tags_exclude")]
196-
TagsExclude,
197-
#[strum(serialize = "sticky")]
198-
Sticky,
199-
}
200-
201152
impl AppendUrlQueryPairs for PostListParams {
202153
fn append_query_pairs(&self, query_pairs_mut: &mut QueryPairs) {
203154
query_pairs_mut

0 commit comments

Comments
 (0)