dir_structure_macros/
lib.rs

1use syn::ItemStruct;
2use syn::punctuated::Punctuated;
3
4mod dir_structure;
5#[cfg(feature = "async")]
6mod dir_structure_async;
7mod dir_structure_core;
8
9#[proc_macro_derive(DirStructure, attributes(dir_structure))]
10pub fn derive_dir_structure(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
11    let item = syn::parse_macro_input!(item as ItemStruct);
12
13    dir_structure::expand_dir_structure(item)
14        // .map(|ts| {
15        //     eprintln!("Expanded DirStructure for {}", ts);
16        //     ts
17        // })
18        .unwrap_or_else(|err| err.to_compile_error())
19        .into()
20}
21
22#[cfg(feature = "async")]
23#[proc_macro_derive(DirStructureAsync, attributes(dir_structure))]
24pub fn derive_dir_structure_async(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
25    let item = syn::parse_macro_input!(item as ItemStruct);
26
27    dir_structure_async::expand_dir_structure_async(item)
28        // .map(|ts| {
29        //     eprintln!("Expanded DirStructureAsync for {}", ts);
30        //     ts
31        // })
32        .unwrap_or_else(|err| err.to_compile_error())
33        .into()
34}
35
36#[cfg(feature = "resolve-path")]
37mod resolve_path;
38
39#[cfg(feature = "resolve-path")]
40#[proc_macro]
41pub fn resolve_path(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
42    resolve_path::resolve_path(input)
43}
44
45#[cfg(feature = "resolve-path")]
46#[proc_macro_derive(HasField, attributes(dir_structure))]
47pub fn derive_has_field(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
48    let item = syn::parse_macro_input!(item as ItemStruct);
49
50    resolve_path::expand_has_field_impls(item)
51        .unwrap_or_else(|err| err.to_compile_error())
52        .into()
53}
54
55#[cfg(feature = "resolve-path")]
56#[proc_macro]
57pub fn load_path(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
58    resolve_path::load_path(input)
59}
60
61#[cfg(feature = "include_dir_vfs")]
62#[proc_macro]
63pub fn include_dir_patched(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
64    let input = syn::parse_macro_input!(input as syn::LitStr);
65    quote::quote! {::dir_structure::include_dir::include_dir!(#input)}.into()
66}
67
68fn merge_where_clause(
69    where_clause: Option<syn::WhereClause>,
70    additional_bounds: Vec<syn::WherePredicate>,
71) -> Option<syn::WhereClause> {
72    if let Some(mut where_clause) = where_clause {
73        where_clause.predicates.extend(additional_bounds);
74        Some(where_clause)
75    } else {
76        let mut where_clause = syn::WhereClause {
77            where_token: <syn::Token![where]>::default(),
78            predicates: Punctuated::new(),
79        };
80        where_clause.predicates.extend(additional_bounds);
81        if where_clause.predicates.is_empty() {
82            None
83        } else {
84            Some(where_clause)
85        }
86    }
87}