Skip to main content

type_rules_derive/
lib.rs

1extern crate proc_macro;
2extern crate proc_macro2;
3
4use crate::parsing::from_ast;
5use proc_macro2::TokenStream;
6use quote::quote;
7use syn::{parse_macro_input, DeriveInput};
8
9mod parsing;
10
11#[proc_macro_derive(Validator, attributes(rule))]
12pub fn derive_validator(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
13    let input = parse_macro_input!(input as DeriveInput);
14
15    expand_derive_validator(input).into()
16}
17
18fn expand_derive_validator(input: DeriveInput) -> TokenStream {
19    let name = input.ident;
20    let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl();
21    let body = from_ast(&input.data).body(&name);
22
23    quote! {
24        impl #impl_generics type_rules::Validator for #name #type_generics #where_clause {
25            fn check_validity(&self) -> Result<(), String> {
26                #body
27            }
28        }
29    }
30}