skippable-partialeq

Crates.ioskippable-partialeq
lib.rsskippable-partialeq
version1.0.0
sourcesrc
created_at2024-06-20 12:13:50.934427
updated_at2024-06-20 12:13:50.934427
descriptionPartialEq, but you can ignore fields
homepagehttps://github.com/caioluis/skippable-partialeq
repositoryhttps://github.com/caioluis/skippable-partialeq
max_upload_size
id1277917
size45,257
Caio Gomes (caioluis)

documentation

README

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.

Commit count: 18

cargo fmt