Skip to content

Commit 5f52728

Browse files
authored
Refactor WordPress content ID types to use macros (#869)
* Refactor WordPress content ID types to use macros Introduces wp_content_i64_id and wp_content_string_id macros to eliminate duplication across ID type implementations. This refactor reduces boilerplate code while maintaining identical functionality and type safety. Changes: - Add wp_content_macros.rs with fully-qualified type macros - Refactor PostId, MediaId, CommentId, CategoryId, TagId, PostRevisionId, UserId to use wp_content_i64_id! - Export macros in prelude for easy access - Remove duplicate implementations (17 lines per ID type → 1 line) - Add From<T> trait implementations as enhancement - Maintain exact same generated code and behavior * Ignore wp_content_macro doc tests
1 parent 8e79a50 commit 5f52728

File tree

10 files changed

+118
-148
lines changed

10 files changed

+118
-148
lines changed

wp_api/src/categories.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,17 @@
11
use crate::{
2-
WpApiParamOrder, impl_as_query_value_for_new_type, impl_as_query_value_from_to_string,
2+
WpApiParamOrder, impl_as_query_value_from_to_string,
33
posts::PostId,
44
taxonomies::TaxonomyType,
55
url_query::{
66
AppendUrlQueryPairs, FromUrlQueryPairs, QueryPairs, QueryPairsExtension, UrlQueryPairsMap,
77
},
8+
wp_content_i64_id,
89
};
910
use serde::{Deserialize, Serialize};
10-
use std::{num::ParseIntError, str::FromStr};
1111
use strum_macros::IntoStaticStr;
1212
use wp_contextual::WpContextual;
1313

14-
impl_as_query_value_for_new_type!(CategoryId);
15-
uniffi::custom_newtype!(CategoryId, i64);
16-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
17-
pub struct CategoryId(pub i64);
18-
19-
impl FromStr for CategoryId {
20-
type Err = ParseIntError;
21-
22-
fn from_str(s: &str) -> Result<Self, Self::Err> {
23-
s.parse().map(Self)
24-
}
25-
}
26-
27-
impl std::fmt::Display for CategoryId {
28-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29-
write!(f, "{}", self.0)
30-
}
31-
}
14+
wp_content_i64_id!(CategoryId);
3215

3316
#[derive(
3417
Debug,

wp_api/src/comments.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use crate::{
22
AnyJson, UserAvatarSize, UserId, WpApiParamOrder, WpResponseString,
33
date::WpGmtDateTime,
4-
impl_as_query_value_for_new_type, impl_as_query_value_from_to_string,
4+
impl_as_query_value_from_to_string,
55
posts::PostId,
66
url_query::{
77
AppendUrlQueryPairs, FromUrlQueryPairs, QueryPairs, QueryPairsExtension, UrlQueryPairsMap,
88
},
9+
wp_content_i64_id,
910
};
1011
use serde::{Deserialize, Serialize};
11-
use std::{collections::HashMap, num::ParseIntError, str::FromStr, sync::Arc};
12+
use std::{collections::HashMap, str::FromStr, sync::Arc};
1213
use strum_macros::IntoStaticStr;
1314
use wp_contextual::WpContextual;
1415

@@ -37,24 +38,7 @@ pub enum WpApiParamCommentsOrderBy {
3738

3839
impl_as_query_value_from_to_string!(WpApiParamCommentsOrderBy);
3940

40-
impl_as_query_value_for_new_type!(CommentId);
41-
uniffi::custom_newtype!(CommentId, i64);
42-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
43-
pub struct CommentId(pub i64);
44-
45-
impl FromStr for CommentId {
46-
type Err = ParseIntError;
47-
48-
fn from_str(s: &str) -> Result<Self, Self::Err> {
49-
s.parse().map(Self)
50-
}
51-
}
52-
53-
impl std::fmt::Display for CommentId {
54-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55-
write!(f, "{}", self.0)
56-
}
57-
}
41+
wp_content_i64_id!(CommentId);
5842

5943
#[derive(
6044
Debug,

wp_api/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub mod uuid;
3939
pub mod widget_types;
4040
pub mod widgets;
4141
pub mod wordpress_org;
42+
pub mod wp_content_macros;
4243
pub mod wp_site_health_tests;
4344

4445
mod uniffi_serde;

wp_api/src/media.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,24 @@
11
use crate::{
22
UserId, WpApiParamOrder,
33
date::WpGmtDateTime,
4-
impl_as_query_value_for_new_type, impl_as_query_value_from_to_string,
4+
impl_as_query_value_from_to_string,
55
posts::{
66
PostCommentStatus, PostId, PostPingStatus, PostStatus, WpApiParamPostsOrderBy,
77
WpApiParamPostsSearchColumn,
88
},
99
url_query::{
1010
AppendUrlQueryPairs, FromUrlQueryPairs, QueryPairs, QueryPairsExtension, UrlQueryPairsMap,
1111
},
12+
wp_content_i64_id,
1213
};
1314
use serde::{Deserialize, Serialize};
1415
use serde_json::value::RawValue;
16+
use std::collections::HashMap;
1517
use std::sync::Arc;
16-
use std::{collections::HashMap, num::ParseIntError, str::FromStr};
1718
use strum_macros::IntoStaticStr;
1819
use wp_contextual::WpContextual;
1920

20-
impl_as_query_value_for_new_type!(MediaId);
21-
uniffi::custom_newtype!(MediaId, i64);
22-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
23-
pub struct MediaId(pub i64);
24-
25-
impl FromStr for MediaId {
26-
type Err = ParseIntError;
27-
28-
fn from_str(s: &str) -> Result<Self, Self::Err> {
29-
s.parse().map(Self)
30-
}
31-
}
32-
33-
impl std::fmt::Display for MediaId {
34-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35-
write!(f, "{}", self.0)
36-
}
37-
}
21+
wp_content_i64_id!(MediaId);
3822

3923
#[derive(
4024
Debug,

wp_api/src/post_revisions.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,18 @@
11
use crate::{
22
UserId, WpApiParamOrder,
33
date::WpGmtDateTime,
4-
impl_as_query_value_for_new_type, impl_as_query_value_from_to_string,
4+
impl_as_query_value_from_to_string,
55
posts::PostId,
66
url_query::{
77
AppendUrlQueryPairs, FromUrlQueryPairs, QueryPairs, QueryPairsExtension, UrlQueryPairsMap,
88
},
9+
wp_content_i64_id,
910
};
1011
use serde::{Deserialize, Serialize};
11-
use std::{num::ParseIntError, str::FromStr};
1212
use strum_macros::IntoStaticStr;
1313
use wp_contextual::WpContextual;
1414

15-
impl_as_query_value_for_new_type!(PostRevisionId);
16-
uniffi::custom_newtype!(PostRevisionId, i64);
17-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
18-
pub struct PostRevisionId(pub i64);
19-
20-
impl FromStr for PostRevisionId {
21-
type Err = ParseIntError;
22-
23-
fn from_str(s: &str) -> Result<Self, Self::Err> {
24-
s.parse().map(Self)
25-
}
26-
}
27-
28-
impl std::fmt::Display for PostRevisionId {
29-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30-
write!(f, "{}", self.0)
31-
}
32-
}
15+
wp_content_i64_id!(PostRevisionId);
3316

3417
#[derive(
3518
Debug,

wp_api/src/posts.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ use crate::{
22
UserId, WpApiParamOrder,
33
categories::CategoryId,
44
date::WpGmtDateTime,
5-
impl_as_query_value_for_new_type, impl_as_query_value_from_to_string,
5+
impl_as_query_value_from_to_string,
66
media::MediaId,
77
tags::TagId,
88
url_query::{
99
AppendUrlQueryPairs, FromUrlQueryPairs, QueryPairs, QueryPairsExtension, UrlQueryPairsMap,
1010
},
11+
wp_content_i64_id,
1112
};
1213
use serde::{Deserialize, Serialize};
13-
use std::{num::ParseIntError, str::FromStr};
1414
use strum_macros::IntoStaticStr;
1515
use wp_contextual::WpContextual;
1616
use wp_serde_helper::{deserialize_from_string_of_json_array, serialize_as_json_string};
@@ -448,24 +448,7 @@ pub struct PostUpdateParams {
448448
pub tags: Vec<TagId>,
449449
}
450450

451-
impl_as_query_value_for_new_type!(PostId);
452-
uniffi::custom_newtype!(PostId, i64);
453-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
454-
pub struct PostId(pub i64);
455-
456-
impl FromStr for PostId {
457-
type Err = ParseIntError;
458-
459-
fn from_str(s: &str) -> Result<Self, Self::Err> {
460-
s.parse().map(Self)
461-
}
462-
}
463-
464-
impl std::fmt::Display for PostId {
465-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
466-
write!(f, "{}", self.0)
467-
}
468-
}
451+
wp_content_i64_id!(PostId);
469452

470453
#[derive(Debug, Serialize, Deserialize, uniffi::Record, WpContextual)]
471454
pub struct SparsePost {

wp_api/src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub use crate::{
1717
endpoint::{ApiUrlResolver, WpOrgSiteApiUrlResolver, media_endpoint::MediaUploadRequest},
1818
},
1919
uuid::{WpUuid, WpUuidParseError},
20+
wp_content_i64_id, wp_content_string_id,
2021
};
2122

2223
#[cfg(feature = "reqwest-request-executor")]

wp_api/src/tags.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,17 @@
11
use crate::{
2-
WpApiParamOrder, impl_as_query_value_for_new_type, impl_as_query_value_from_to_string,
2+
WpApiParamOrder, impl_as_query_value_from_to_string,
33
posts::PostId,
44
taxonomies::TaxonomyType,
55
url_query::{
66
AppendUrlQueryPairs, FromUrlQueryPairs, QueryPairs, QueryPairsExtension, UrlQueryPairsMap,
77
},
8+
wp_content_i64_id,
89
};
910
use serde::{Deserialize, Serialize};
10-
use std::{num::ParseIntError, str::FromStr};
1111
use strum_macros::IntoStaticStr;
1212
use wp_contextual::WpContextual;
1313

14-
impl_as_query_value_for_new_type!(TagId);
15-
uniffi::custom_newtype!(TagId, i64);
16-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
17-
pub struct TagId(pub i64);
18-
19-
impl FromStr for TagId {
20-
type Err = ParseIntError;
21-
22-
fn from_str(s: &str) -> Result<Self, Self::Err> {
23-
s.parse().map(Self)
24-
}
25-
}
26-
27-
impl std::fmt::Display for TagId {
28-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29-
write!(f, "{}", self.0)
30-
}
31-
}
14+
wp_content_i64_id!(TagId);
3215

3316
#[derive(
3417
Debug,

wp_api/src/users.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use crate::{
22
EnumFromStrParsingError, OptionFromStr, WpApiParamOrder, WpResponseString,
3-
impl_as_query_value_for_new_type, impl_as_query_value_from_to_string,
3+
impl_as_query_value_from_to_string,
44
url_query::{
55
AppendUrlQueryPairs, FromUrlQueryPairs, QueryPairs, QueryPairsExtension, UrlQueryPairsMap,
66
},
7+
wp_content_i64_id,
78
};
89
use serde::{Deserialize, Serialize};
9-
use std::{
10-
collections::HashMap, convert::Infallible, fmt::Display, num::ParseIntError, str::FromStr,
11-
};
10+
use std::{collections::HashMap, convert::Infallible, fmt::Display, str::FromStr};
1211
use strum_macros::IntoStaticStr;
1312
use wp_contextual::WpContextual;
1413

@@ -403,30 +402,7 @@ pub struct UserDeleteResponse {
403402
pub previous: UserWithEditContext,
404403
}
405404

406-
impl_as_query_value_for_new_type!(UserId);
407-
uniffi::custom_newtype!(UserId, i64);
408-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
409-
pub struct UserId(pub i64);
410-
411-
impl FromStr for UserId {
412-
type Err = ParseIntError;
413-
414-
fn from_str(s: &str) -> Result<Self, Self::Err> {
415-
s.parse().map(Self)
416-
}
417-
}
418-
419-
impl std::fmt::Display for UserId {
420-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
421-
write!(f, "{}", self.0)
422-
}
423-
}
424-
425-
impl From<i64> for UserId {
426-
fn from(value: i64) -> Self {
427-
Self(value)
428-
}
429-
}
405+
wp_content_i64_id!(UserId);
430406

431407
#[derive(Debug, Serialize, Deserialize, uniffi::Record, WpContextual)]
432408
pub struct SparseUser {

0 commit comments

Comments
 (0)