Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a6d46ba

Browse files
committed
[add] modifier マクロ準備
1 parent 3db8c0f commit a6d46ba

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

‎crates/macros/src/impl/modifier.rs‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
1-
use proc_macro2::TokenStream;
1+
use proc_macro2::{TokenStream,Ident};
22
use quote::quote;
33
use syn::ItemTrait;
44

5+
/// 対象: トレイト
6+
/// 動作: 文脈導入用の宣言型マクロを生成
57
pub fn proc_macro_impl(_args: TokenStream, ast: ItemTrait) -> TokenStream {
8+
let trait_name = &ast.ident;
9+
let macro_ident = as_macro_ident(trait_name);
10+
611
quote! {
712
#ast
813

9-
macro_rules! hello {
10-
() => {
11-
println!("Hello!");
12-
};
14+
macro_rules! #macro_ident {
15+
($($body:tt)*) => {{
16+
fn __mymodifier_callee() {
17+
$($body)*
18+
}
19+
__mymodifier_callee();
20+
}};
1321
}
1422
}
1523
}
24+
25+
fn as_macro_ident(ident: &Ident) -> TokenStream {
26+
let mut result = String::new();
27+
for c in ident.to_string().chars() {
28+
if c.is_uppercase() {
29+
result.push('_');
30+
result.push(c.to_ascii_lowercase());
31+
} else {
32+
result.push(c);
33+
}
34+
}
35+
36+
let result = if result.starts_with('_') {
37+
&result[1..]
38+
} else {
39+
&result
40+
};
41+
42+
result.parse().unwrap()
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /