filterstruct

Crates.iofilterstruct
lib.rsfilterstruct
version0.1.0
created_at2025-11-04 05:02:08.040917+00
updated_at2025-11-04 05:02:08.040917+00
descriptionA simple macro for creating struct instances with ergonomic syntax, useful for filters.
homepage
repositoryhttps://github.com/MrRevillod/filterstruct
max_upload_size
id1915757
size9,860
Luciano Revillod (MrRevillod)

documentation

README

FilterStruct

Simple macro for create instances of structs with more ergonomic syntax.

Installation

cargo add filterstruct

How to use

Define your struct as usual:

#[derive(Default, Clone)]
pub struct UserFilter {
    pub id: Option<Uuid>,
    pub email: Option<String>,
    pub search: Option<String>,
    pub page: u64,
}

Then, define your filter using the filter! macro:

macro_rules! user_filter {
    ($($field:ident $(: $value:expr)?),* $(,)?) => {
        ::filterstruct::filter!(UserFilter, { $($field $(: $value)?),* })
    };
}

Now you can create instances of UserFilter using the user_filter! macro:

let filter = user_filter! {
    id: Uuid::new_v4(),
}

This will create a UserFilter instance with the id field set to a new UUID, and all other fields set to their default values.

Also, you can use shorthand for fields that you want to set to Some(value):

let email = String::from("some@domain.com");

let filter = user_filter! {
    email,
    page: 2,
}
Commit count: 0

cargo fmt