filterstruct 0.1.0

A simple macro for creating struct instances with ergonomic syntax, useful for filters.
Documentation
  • Coverage
  • 0%
    0 out of 2 items documented0 out of 0 items with examples
  • Size
  • Source code size: 9.36 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.07 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • MrRevillod/filterstruct
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • MrRevillod

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,
}