Crates.io | timeless-partialeq |
lib.rs | timeless-partialeq |
version | 0.2.0 |
source | src |
created_at | 2024-05-06 18:50:08.978568 |
updated_at | 2024-05-09 12:19:49.104863 |
description | PartialEq, but ignores timestamps, ideal for API testing |
homepage | https://github.com/caioluis/timeless-partialeq |
repository | https://github.com/caioluis/timeless-partialeq |
max_upload_size | |
id | 1231386 |
size | 44,875 |
This crate provides a custom derive macro TimelessPartialEq
that allows you to implement PartialEq
for structs while ignoring fields based on their suffixes.
First, add TimelessPartialEq as a dependency.
cargo add timeless-partialeq
Then, derive TimelessPartialEq
for your struct:
use timeless_partialeq::TimelessPartialEq;
#[derive(Debug, TimelessPartialEq)]
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.
You can also use the #[exclude_suffix]
attribute to filter by specific suffixes:
use timeless_partialeq::TimelessPartialEq;
#[derive(Debug, TimelessPartialEq)]
#[exclude_suffix(at, date)]
pub struct Post {
pub id: i64,
pub content: String,
pub author: i32,
pub creation_date: DateTime<Utc>,
pub updated_at: Option<DateTime<Utc>>,
}
This would exclude fields ending with _at
and/or date
.
This crate was made to solve a very specific problem: assert the equality of two objects despite the timestamp differences. It was also made so that I could study proc macros. However, just after a day after publishing it, I realized that it can be broader than just timestamps.
I will not make a commitment into iterating this quickly, but it is in my plans to expand the scope of the crate.