edict_proc_lib/
lib.rs

1use proc_macro2::TokenStream;
2
3mod component;
4// mod query;
5mod relation;
6mod system;
7
8mod kw {
9    proc_easy::easy_token!(name);
10    proc_easy::easy_token!(borrow);
11    proc_easy::easy_token!(on_drop);
12    proc_easy::easy_token!(on_target_drop);
13    proc_easy::easy_token!(on_replace);
14    proc_easy::easy_token!(exclusive);
15    proc_easy::easy_token!(symmetric);
16    proc_easy::easy_token!(owned);
17}
18
19proc_easy::easy_argument_value! {
20    struct Name {
21        kw: kw::name,
22        literal: syn::LitStr,
23    }
24}
25
26proc_easy::easy_argument! {
27    struct Borrow {
28        kw: kw::borrow,
29        targets: proc_easy::EasyParenthesized<proc_easy::EasyTerminated<syn::Type>>,
30    }
31}
32
33proc_easy::easy_argument! {
34    struct OnDrop {
35        kw: kw::on_drop,
36        eq: syn::Token![=],
37        function: syn::Expr,
38    }
39}
40
41proc_easy::easy_argument! {
42    struct OnReplace {
43        kw: kw::on_replace,
44        eq: syn::Token![=],
45        function: syn::Expr,
46    }
47}
48
49proc_easy::easy_argument! {
50    struct OnTargetDrop {
51        kw: kw::on_target_drop,
52        eq: syn::Token![=],
53        function: syn::Expr,
54    }
55}
56
57proc_easy::easy_argument! {
58    struct Exclusive {
59        kw: kw::exclusive,
60    }
61}
62
63proc_easy::easy_argument! {
64    struct Owned {
65        kw: kw::owned,
66    }
67}
68
69proc_easy::easy_argument! {
70    struct WhereClause {
71        kw: syn::Token![where],
72        predicates: proc_easy::EasyTerminated<syn::WherePredicate, syn::Token![,]>,
73    }
74}
75
76fn merge_where_clauses(
77    where_clause: Option<&syn::WhereClause>,
78    additional: &[WhereClause],
79) -> Option<syn::WhereClause> {
80    match where_clause {
81        None if additional.is_empty() => None,
82        None => {
83            let mut predicates = syn::punctuated::Punctuated::new();
84
85            for where_clause in additional {
86                for predicate in where_clause.predicates.iter() {
87                    predicates.push(predicate.clone());
88                }
89            }
90
91            Some(syn::WhereClause {
92                where_token: additional[0].kw,
93                predicates,
94            })
95        }
96        Some(where_clause) => {
97            let mut predicates = where_clause.predicates.clone();
98
99            for where_clause in additional {
100                for predicate in where_clause.predicates.iter() {
101                    predicates.push(predicate.clone());
102                }
103            }
104
105            Some(syn::WhereClause {
106                where_token: where_clause.where_token,
107                predicates,
108            })
109        }
110    }
111}
112
113pub fn derive_component(
114    item: TokenStream,
115    edict_path: &syn::Path,
116    edict_namespace: &syn::Ident,
117) -> TokenStream {
118    match syn::parse2(item).and_then(|input| component::derive(input, edict_path, edict_namespace))
119    {
120        Ok(output) => output.into(),
121        Err(err) => err.to_compile_error().into(),
122    }
123}
124
125pub fn derive_relation(
126    item: TokenStream,
127    edict_path: &syn::Path,
128    edict_namespace: &syn::Ident,
129) -> TokenStream {
130    match syn::parse2(item).and_then(|input| relation::derive(input, edict_path, edict_namespace)) {
131        Ok(output) => output.into(),
132        Err(err) => err.to_compile_error().into(),
133    }
134}
135
136// pub fn derive_query(
137//     item: TokenStream,
138//     edict_path: &syn::Path,
139//     edict_namespace: &syn::Ident,
140// ) -> TokenStream {
141//     match syn::parse2(item).and_then(|input| query::derive(input, edict_path, edict_namespace)) {
142//         Ok(output) => output.into(),
143//         Err(err) => err.to_compile_error().into(),
144//     }
145// }
146
147pub fn system(item: syn::ItemFn, edict_path: &syn::Path) -> syn::Result<TokenStream> {
148    system::system(item, edict_path)
149}