fairingrey/fieldfilter


A Rust trait that lets you filter stuff!

Language: Rust


fieldfilter

fieldfilter is a pretty simple crate to use. Basically what it offers is a way to essentially “filter” a struct to a smaller set of itself.

Here’s an example:

use std::collections::HashSet;
use fieldfilter::FieldFilterable;

struct User {
    id: u32,
    name: String,
    email: String,
}
#[derive(Debug, FieldFilterable)]
#[field_filterable_on(User)]
struct FilteredUser {
    id: u32,
    name: Option<String>,
    email: Option<String>,
}

let user = User {
    id: 1,
    name: "Allen".to_string(),
    email: "[email protected]".to_string(),
};

let fields = HashSet::from(["name".to_owned()]);

let filtered_user: FilteredUser = FieldFilterable::field_filter(user, fields);
println!("{:?}", filtered_user);

This kind of behavior is quite useful for whenever you have some struct or entity you want to skim off the visibility of certain fields, whether that be some SQL row being returned as some REST or GraphQL response, or just as a simple sanitization routine.

Inspiration

Why this crate exists is because I had a certain use case for it. I wanted to essentially be able to map models from an ORM to a serialized HTTP response with only the fields I wanted to be visible, dictated by policy files in my chosen authorization engine.

Not to mention it was a reason to finally get into proc macros!

Project Statistics

Sourcerank 2
Repository Size 3.91 KB
Stars 0
Forks 0
Watchers 1
Open issues 0
Dependencies 3
Contributors 1
Tags 0
Created
Last updated
Last pushed

Top Contributors See all

Allen

Packages Referencing this Repo

fieldfilter
A Rust trait that lets you filter stuff!
Latest release 0.1.0 - Published
fieldfilter-derive
Proc macro to derive FieldFilterable
Latest release 0.1.0 - Published

Something wrong with this page? Make a suggestion

Last synced: 2022-02-18 18:17:59 UTC

Login to resync this repository