Skip to content

Commit 9cb1d3b

Browse files
authored
Adds #[WpContextualDontDerivePartialEq] (#964)
1 parent ad9460b commit 9cb1d3b

File tree

8 files changed

+51
-19
lines changed

8 files changed

+51
-19
lines changed

wp_api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub struct AnyJson {
142142
}
143143

144144
uniffi::custom_newtype!(WpResponseString, Option<String>);
145-
#[derive(Debug, Serialize, Deserialize)]
145+
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
146146
#[serde(try_from = "BoolOrString")]
147147
pub struct WpResponseString(pub Option<String>);
148148

wp_api/src/media.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ impl From<MediaCreateParams> for HashMap<String, String> {
341341
}
342342

343343
#[derive(Debug, Serialize, Deserialize, uniffi::Record, WpContextual)]
344+
#[WpContextualDontDerivePartialEq]
344345
pub struct SparseMedia {
345346
#[WpContext(edit, embed, view)]
346347
pub id: Option<MediaId>,

wp_api/src/plugins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub enum PluginStatus {
150150

151151
impl_as_query_value_from_to_string!(PluginStatus);
152152

153-
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
153+
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)]
154154
pub struct PluginDescription {
155155
pub raw: String,
156156
pub rendered: String,

wp_api/src/posts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,14 +467,14 @@ pub struct SparsePostContent {
467467
pub block_version: Option<u32>,
468468
}
469469

470-
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
470+
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)]
471471
pub struct SparsePostExcerpt {
472472
pub raw: Option<String>,
473473
pub rendered: Option<String>,
474474
pub protected: Option<bool>,
475475
}
476476

477-
#[derive(Debug, Serialize, WpDeserialize, uniffi::Record)]
477+
#[derive(Debug, PartialEq, Eq, Serialize, WpDeserialize, uniffi::Record)]
478478
pub struct PostMeta {
479479
#[serde(deserialize_with = "deserialize_from_string_of_json_array")]
480480
#[serde(serialize_with = "serialize_as_json_string")]

wp_api/src/themes.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,49 +110,49 @@ impl Display for ThemeStylesheet {
110110
}
111111
}
112112

113-
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
113+
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)]
114114
pub struct ThemeAuthor {
115115
pub raw: String,
116116
pub rendered: String,
117117
}
118118

119-
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
119+
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)]
120120
pub struct ThemeAuthorName {
121121
pub raw: String,
122122
pub rendered: String,
123123
}
124124

125-
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
125+
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)]
126126
pub struct ThemeAuthorUri {
127127
pub raw: String,
128128
pub rendered: String,
129129
}
130130

131-
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
131+
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)]
132132
pub struct ThemeAuthorDescription {
133133
pub raw: String,
134134
pub rendered: String,
135135
}
136136

137-
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
137+
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)]
138138
pub struct ThemeDescription {
139139
pub raw: String,
140140
pub rendered: String,
141141
}
142142

143-
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
143+
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)]
144144
pub struct ThemeName {
145145
pub raw: String,
146146
pub rendered: String,
147147
}
148148

149-
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
149+
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)]
150150
pub struct ThemeTags {
151151
pub raw: Vec<String>,
152152
pub rendered: String,
153153
}
154154

155-
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
155+
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)]
156156
pub struct ThemeUri {
157157
pub raw: String,
158158
pub rendered: String,

wp_api/src/widgets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub struct SparseWidget {
3232
pub form_data: Option<String>,
3333
}
3434

35-
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
35+
#[derive(Debug, PartialEq, Serialize, Deserialize, uniffi::Record)]
3636
pub struct WidgetInstance {
3737
#[serde(skip_serializing_if = "Option::is_none")]
3838
pub encoded: Option<String>,

wp_contextual/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,8 @@ mod wp_contextual;
512512
WpContext,
513513
WpContextualField,
514514
WpContextualOption,
515-
WpContextualExcludeFromFields
515+
WpContextualExcludeFromFields,
516+
WpContextualDontDerivePartialEq
516517
)
517518
)]
518519
pub fn derive(input: TokenStream) -> TokenStream {

wp_contextual/src/wp_contextual.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ use syn::{DeriveInput, Ident, spanned::Spanned};
77

88
const IDENT_PREFIX: &str = "Sparse";
99

10+
/// Checks if the type has #[WpContextualDontDerivePartialEq] attribute.
11+
///
12+
/// When present, PartialEq won't be added to the generated types.
13+
/// This is useful for types that contain fields that don't support PartialEq.
14+
fn has_dont_derive_partial_eq_attr(attrs: &[syn::Attribute]) -> bool {
15+
attrs
16+
.iter()
17+
.any(|attr| attr.path().is_ident("WpContextualDontDerivePartialEq"))
18+
}
19+
1020
pub fn wp_contextual(ast: DeriveInput) -> Result<TokenStream, syn::Error> {
1121
let original_ident = &ast.ident;
1222
let original_ident_name = original_ident.to_string();
@@ -19,17 +29,37 @@ pub fn wp_contextual(ast: DeriveInput) -> Result<TokenStream, syn::Error> {
1929
struct_fields(&ast.data).map_err(|err| err.into_syn_error(original_ident.span()))?;
2030
let parsed_fields = parse_fields(fields)?;
2131

32+
// Check if PartialEq should be derived
33+
let should_derive_partial_eq = !has_dont_derive_partial_eq_attr(&ast.attrs);
34+
2235
let contextual_token_streams = WpContextAttr::iter().map(|current_context| {
2336
let generate_type = |is_sparse, ident, generated_fields: &Vec<GeneratedContextualField>| {
2437
if !generated_fields.is_empty() {
25-
let deserialize_derive = if is_sparse {
26-
quote! { wp_derive::WpDeserialize }
38+
// Build the hardcoded derives
39+
let mut derives: Vec<syn::Path> = vec![syn::parse_quote!(Debug)];
40+
41+
// Add PartialEq unless explicitly disabled
42+
// Note: We only derive PartialEq, not Eq, because we can't guarantee
43+
// that all field types will satisfy Eq's stricter requirements
44+
// (e.g., floating point types where NaN != NaN)
45+
if should_derive_partial_eq {
46+
derives.push(syn::parse_quote!(PartialEq));
47+
}
48+
49+
derives.push(syn::parse_quote!(serde::Serialize));
50+
51+
// For sparse types use WpDeserialize, for non-sparse use Deserialize
52+
if is_sparse {
53+
derives.push(syn::parse_quote!(wp_derive::WpDeserialize));
2754
} else {
28-
quote! { serde::Deserialize }
29-
};
55+
derives.push(syn::parse_quote!(serde::Deserialize));
56+
}
57+
58+
derives.push(syn::parse_quote!(uniffi::Record));
59+
3060
let fields_to_add = generated_fields.iter().map(|f| &f.field);
3161
quote! {
32-
#[derive(Debug, serde::Serialize, #deserialize_derive, uniffi::Record)]
62+
#[derive(#(#derives),*)]
3363
pub struct #ident {
3464
#(#fields_to_add,)*
3565
}

0 commit comments

Comments
 (0)