-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from MartinFillon/add-proc-macros
Add bean proc macros
- Loading branch information
Showing
7 changed files
with
89 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[workspace] | ||
|
||
members = ["server/tests/server-tester", "ai"] | ||
members = ["server/tests/server-tester", "ai", "rust-macros/"] | ||
resolver = "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "rust-macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
syn = {} | ||
quote = {} | ||
proc-macro2 = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
extern crate proc_macro; | ||
|
||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{Data, DeriveInput, Fields, FieldsNamed}; | ||
|
||
fn get_fields(data: &Data) -> FieldsNamed { | ||
if let Data::Struct(strct) = data { | ||
if let Fields::Named(fields) = &strct.fields { | ||
fields.to_owned() | ||
} else { | ||
panic!("Can only handle named fields") | ||
} | ||
} else { | ||
panic!("Can only handle structs"); | ||
} | ||
} | ||
|
||
fn impl_bean(ast: DeriveInput) -> TokenStream { | ||
let ident = ast.ident; | ||
let fields = get_fields(&ast.data); | ||
let setters = fields.named.iter().map(|field| { | ||
let name = field.ident.as_ref().unwrap(); | ||
let ty = &field.ty; | ||
let f_name: proc_macro2::TokenStream = format!("set_{}", name).parse().unwrap(); | ||
let function_signature = quote!(pub fn #f_name (&mut self, input: #ty)); | ||
quote!( | ||
#function_signature { | ||
self.#name = input; | ||
} | ||
) | ||
}); | ||
|
||
let getters = fields.named.iter().map(|field| { | ||
let name = field.ident.as_ref().unwrap(); | ||
let ty = &field.ty; | ||
let function_signature = quote!(pub fn #name (&self) -> &#ty); | ||
quote!( | ||
#function_signature { | ||
&self.#name | ||
} | ||
) | ||
}); | ||
|
||
quote! { | ||
impl #ident { | ||
#(#setters)* | ||
#(#getters)* | ||
} | ||
} | ||
.into() | ||
} | ||
|
||
#[proc_macro_derive(Bean)] | ||
pub fn my_macro_here_derive(input: TokenStream) -> TokenStream { | ||
let ast: DeriveInput = syn::parse(input).unwrap(); | ||
|
||
impl_bean(ast) | ||
} |