livre_derive/
lib.rs

1//! This is not documented nor tested... I'm sorry.
2//!
3//! See the [`livre`](https://docs.rs/livre) documentation for more information.
4
5use std::collections::HashSet;
6
7use syn::{parse_quote, GenericParam, Generics};
8
9mod extract;
10mod from_raw_dict;
11mod utilities;
12
13#[proc_macro_derive(FromRawDict, attributes(livre))]
14pub fn derive_from_raw_dict(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
15    from_raw_dict::derive(input)
16}
17
18#[proc_macro_derive(Extract, attributes(livre))]
19pub fn derive_extract(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
20    extract::derive(input)
21}
22
23// Add a bound `T: Extract` to every type parameter T.
24fn add_extraction_trait_bounds(mut generics: Generics, flattened: HashSet<String>) -> Generics {
25    for param in &mut generics.params {
26        if let GenericParam::Type(ref mut type_param) = *param {
27            if flattened.contains(&type_param.ident.to_string()) {
28                type_param
29                    .bounds
30                    .push(parse_quote!(crate::extraction::FromRawDict<'de>));
31            } else {
32                type_param
33                    .bounds
34                    .push(parse_quote!(crate::extraction::Extract<'de>));
35            }
36        }
37    }
38    generics
39}