Skip to main content

filter_derive/
lib.rs

1extern crate proc_macro;
2
3use proc_macro::TokenStream;
4use quote::quote;
5
6/// The derive macro #[derive(Filter)] is used to implement the Filter trait by default for a struct.\
7/// The trait will not add any filters to the Query by default.
8#[proc_macro_derive(Filter)]
9pub fn filter_derive(input: TokenStream) -> TokenStream {
10    let ast = syn::parse(input).unwrap();
11    impl_filter_derive(&ast)
12}
13
14fn impl_filter_derive(ast: &syn::DeriveInput) -> TokenStream {
15    let name = &ast.ident;
16    let gen = quote! {
17        impl Filter for #name {
18            fn filter<T: IntoIterator>(mut self, property: impl ToString, value: T) -> Self
19            where
20                T::Item: ToString,
21            {
22                self
23            }
24
25            fn filter_with<T: IntoIterator>(mut self, property: impl ToString, filter: impl ToString, value: T) -> Self
26            where
27                T::Item: ToString,
28            {
29                self
30            }
31
32            fn pattern(mut self, pattern: impl ToString) -> Self {
33                self
34            }
35        }
36    };
37    gen.into()
38}