-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add proc macro for expanding config structs and borders config
- Loading branch information
1 parent
0a4189d
commit bfdf644
Showing
16 changed files
with
398 additions
and
164 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 @@ | ||
[package] | ||
name = "get_derive" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
quote = "1.0.26" | ||
syn = { version = "2.0.13", features = [ "full" ] } | ||
proc-macro2 = "1.0.56" |
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,65 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Expr, Fields, Lit}; | ||
|
||
#[proc_macro_derive(GetOnceCell, attributes(config_path))] | ||
pub fn derive(input: TokenStream) -> TokenStream { | ||
let input = parse_macro_input!(input as DeriveInput); | ||
|
||
let fields = match &input.data { | ||
Data::Struct(DataStruct { | ||
fields: Fields::Named(fields), | ||
.. | ||
}) => &fields.named, | ||
_ => panic!("expected a struct with named fields"), | ||
}; | ||
|
||
let field_name = fields.iter().map(|f| &f.ident); | ||
let field_fn_name = fields.iter().map(|field| { | ||
if let Some(ident) = &field.ident { | ||
let name = format!("get_{}", ident.to_string()); | ||
Some(proc_macro2::Ident::new(&name, ident.span())) | ||
} else { | ||
None | ||
} | ||
}); | ||
|
||
let field_mut_fn_name = fields.iter().map(|field| { | ||
if let Some(ident) = &field.ident { | ||
let name = format!("get_mut_{}", ident.to_string()); | ||
Some(proc_macro2::Ident::new(&name, ident.span())) | ||
} else { | ||
None | ||
} | ||
}); | ||
|
||
let config_path = fields.iter().map(|f| { | ||
if let Some(attr) = f.attrs.first() { | ||
if attr.path().is_ident("config_path") { | ||
let path: Lit = attr.parse_args().unwrap(); | ||
Some(path) | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
}); | ||
|
||
let struct_name = &input.ident; | ||
|
||
TokenStream::from(quote! { | ||
impl #struct_name { | ||
#( | ||
pub fn #field_fn_name(&self) -> &::std::collections::HashMap<&'static str, &'static str> { | ||
self.#field_name.get_or_init(|| ron::from_str(include_str!(#config_path)).unwrap()) | ||
} | ||
|
||
pub fn #field_mut_fn_name(&mut self) -> &mut ::std::collections::HashMap<&'static str, &'static str> { | ||
let _ = self.#field_fn_name(); | ||
self.#field_name.get_mut().unwrap() | ||
} | ||
)* | ||
} | ||
}) | ||
} |
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
Oops, something went wrong.