#partial-eq #struct-fields

macro skippable-partialeq

PartialEq, but you can ignore fields

1 stable release

1.0.0 Jun 20, 2024

#490 in Testing

AGPL-3.0-or-later

15KB
65 lines

skippable-partialeq

PartialEq, but you can ignore fields

This crate provides a custom derive macro SkippablePartialEq that allows you to implement PartialEq for structs while ignoring fields based on custom rules such as suffixes, as well as specific fields.

Usage

First, add SkippablePartialEq as a dependency.

cargo add skippable-partialeq

Then, derive SkippablePartialEq for your struct:

use skippable_partialeq::SkippablePartialEq;

#[derive(Debug, SkippablePartialEq)]
#[exclude_suffix(at)]
pub struct Post {
    pub id: i64,
    pub content: String,
    pub author: i32,
    pub created_at: DateTime<Utc>,
    pub updated_at: Option<DateTime<Utc>>,
}

This will generate an implementation of PartialEq that ignores the fields ending with _at, while still checking for Option's outer None & Some values. This attribute supports multiple values separated by comma.

You can also use the #[skip] attribute to filter by specific fields:

use skippable_partialeq::SkippablePartialEq;

#[derive(Debug, SkippablePartialEq)]
pub struct Post {
    pub id: i64,
    pub content: String,
    pub author: i32,
    #[skip]
    pub creation_date: DateTime<Utc>,
}

This would exclude creation_date from the comparison.

Dependencies

~245–700KB
~17K SLoC