dir_structure_macros/
lib.rs1use 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 .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 .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 = "resolve-path")]
62#[proc_macro]
63pub fn __resolve_max_len(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
64 let max_len = resolve_path::MAX_LEN;
67 let output = quote::quote! { #max_len };
68 output.into()
69}
70
71#[cfg(feature = "include_dir_vfs")]
72#[proc_macro]
73pub fn include_dir_patched(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
74 let input = syn::parse_macro_input!(input as syn::LitStr);
75 quote::quote! {::dir_structure::include_dir::include_dir!(#input)}.into()
76}
77
78fn merge_where_clause(
79 where_clause: Option<syn::WhereClause>,
80 additional_bounds: Vec<syn::WherePredicate>,
81) -> Option<syn::WhereClause> {
82 if let Some(mut where_clause) = where_clause {
83 where_clause.predicates.extend(additional_bounds);
84 Some(where_clause)
85 } else {
86 let mut where_clause = syn::WhereClause {
87 where_token: <syn::Token![where]>::default(),
88 predicates: Punctuated::new(),
89 };
90 where_clause.predicates.extend(additional_bounds);
91 if where_clause.predicates.is_empty() {
92 None
93 } else {
94 Some(where_clause)
95 }
96 }
97}