rudeboy_derive/lib.rs
1//! This crate provides derive and attr macros for use by the [`rudeboy`] crate.
2//! Please refer to it for documentation and usage information.
3//!
4//! [`rudeboy`]: https://docs.rs/rudeboy
5use proc_macro::TokenStream;
6use syn;
7
8mod methods;
9use methods::impl_methods_attr_macro;
10
11/// Placed on an inherent impl block; generates an impl of [`RudeboyMethods`] to
12/// add the contained methods to the exported user data. Takes no parameters.
13///
14/// [`RudeboyMethods`]: trait.RudeboyMethods.html
15#[proc_macro_attribute]
16pub fn methods(_attr: TokenStream, item: TokenStream) -> TokenStream {
17 let input = syn::parse_macro_input!(item as syn::Item);
18 impl_methods_attr_macro(input).into()
19}
20
21mod metamethods;
22use metamethods::impl_metamethods_attr_macro;
23
24/// Placed on a struct or enum definition; generates an impl of
25/// [`RudeboyMetamethods`] to add the specified metamethods to the exported user
26/// data.
27///
28/// Takes any combination of the following parameters:
29/// * Add - allows the use of the `+` operator. Uses `std::ops::Add`
30/// * BAnd - allows the use of the `&` operator. Uses `std::ops::BitAnd`
31/// * BNot - allows the use of the unary `~` operator. Uses `std::ops::Not`
32/// * BOr - allows the use of the `|` operator. Uses `std::ops::BitOr`
33/// * BXor - allows the use of the binary `~` operator. Uses `std::ops::BitXor`
34/// * Div - allows the use of the `/` operator. Uses `std::ops::Div`
35/// * Eq - allows the use of the `==` operator. Uses `std::cmp::PartialEq`
36/// * Index - allows the use of `.` to retrieve fields. Only usable for structs
37/// with named fields
38/// * Le - allows the use of the `<=` operator. Uses `std::cmp::PartialOrd`
39/// * Lt - allows the use of the `<` operator. Uses `std::cmp::PartialOrd`
40/// * Mod - allows the use of the `%` operator. Uses `std::ops::Rem`
41/// * Mul - allows the use of the `*` operator. Uses `std::ops::Mul`
42/// * Shl - allows the use of the `<<` operator. Uses `std::ops::Shl`
43/// * Shr - allows the use of the `>>` operator. Uses `std::ops::Shr`
44/// * Sub - allows the use of the binary `-` operator. Uses `std::ops::Sub`
45/// * Unm - allows the use of the unary `-` operator. Uses `std::ops::Neg`
46///
47/// Note: all binary operators currently take a parameter of the same type as the
48/// type the metamethod is being added to. This is not obviously not ideal.
49///
50/// [`RudeboyMetaMethods`]: trait.RudeboyMetaMethods.html
51#[proc_macro_attribute]
52pub fn metamethods(attr: TokenStream, item: TokenStream) -> TokenStream {
53 let input = syn::parse_macro_input!(item as syn::Item);
54 use syn::parse::Parser;
55 let parser = syn::punctuated::Punctuated::<syn::NestedMeta, syn::Token!(,)>::parse_terminated;
56 let parsed_attrs = parser.parse(attr);
57 let attrs = match &parsed_attrs {
58 Ok(ok) => ok.iter().collect(),
59 Err(e) => return e.to_compile_error().into(),
60 };
61 impl_metamethods_attr_macro(input, attrs).into()
62}
63
64mod user_data;
65use user_data::impl_user_data_attr_macro;
66
67/// Generates an implementation of `rlua::UserData` for the tagged type
68/// definition or the type that matches a tagged impl block.
69///
70/// Takes zero or more of the following parameters. If given none, then the
71/// exported type will have no methods or metamethods available.
72/// * MetaMethods - will use the [`RudeboyMetaMethods`] trait to add generated
73/// meta methods
74/// * Methods - will use the [`RudeboyMethods`] trait to add generated methods
75///
76/// Note: if you wish to add additional (meta)methods beyond the ones generated
77/// by rudeboy, do not use this macro and instead manually call the appropriate
78/// trait methods in your implementation of `rlua::UserData`
79///
80/// [`RudeboyMetaMethods`]: trait.RudeboyMetaMethods.html
81/// [`RudeboyMethods`]: trait.RudeboyMethods.html
82#[proc_macro_attribute]
83pub fn user_data(attr: TokenStream, item: TokenStream) -> TokenStream {
84 use syn::parse::Parser;
85 let parser = syn::punctuated::Punctuated::<syn::NestedMeta, syn::Token!(,)>::parse_terminated;
86 let parsed_attrs = parser.parse(attr);
87 let attrs = match &parsed_attrs {
88 Ok(ok) => ok.iter().collect(),
89 Err(e) => return e.to_compile_error().into(),
90 };
91 let input = syn::parse_macro_input!(item as syn::Item);
92 impl_user_data_attr_macro(input, attrs).into()
93}