Crates.io | skippable-partialeq |
lib.rs | skippable-partialeq |
version | 1.0.0 |
source | src |
created_at | 2024-06-20 12:13:50.934427 |
updated_at | 2024-06-20 12:13:50.934427 |
description | PartialEq, but you can ignore fields |
homepage | https://github.com/caioluis/skippable-partialeq |
repository | https://github.com/caioluis/skippable-partialeq |
max_upload_size | |
id | 1277917 |
size | 45,257 |
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.
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.