Skip to content

caioluis/skippable-partialeq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Releases

No releases published

Packages

No packages published

Languages