1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use mokuya::components::prelude::*;
use proc_macro2::{Ident, Span};
use quote::quote;
use syn::{DeriveInput, ItemFn};
pub fn generate_print_by_field(input: &DeriveInput) -> proc_macro2::TokenStream {
let struct_name = get_struct_name(input);
let struct_name_string = struct_name.to_string();
let attributes = get_attributes(input);
let impl_block = get_impl(input);
let transporter_stmts = match get_attr::<ItemFn>(&attributes, "transporter") {
Ok(transporter) => transporter.block.stmts.clone(),
Err(_) => Vec::new(),
};
let tokens: Vec<proc_macro2::TokenStream> = get_named_fields(input)
.unwrap()
.named
.iter()
.map(|field| {
let field_name = field.ident.as_ref().unwrap();
let field_name_string = field_name.to_string();
let field_type = field.ty.clone();
let field_name_capitalized = format!(
"{}{}",
field_name_string.chars().next().unwrap().to_uppercase(),
&field_name_string[1..]
);
let method_name = new_ident("print", field_name);
let prefix =
format!("Neko_Print_Printer_{struct_name_string}_{field_name_capitalized}");
let field_struct_name = Ident::new(&prefix, Span::call_site());
quote! {
#[derive(Debug, Default, kenzu::Builder)]
pub struct #field_struct_name {
pub target: #field_type,
pub message: String,
}
impl #field_struct_name {
pub async fn rust(&self) {
use colorful::Colorful;
let message = format!(
"({} {} {}:{}) @RUST => {}.{} = {:#?} {}",
chrono::Local::now(),
file!(),
line!(),
column!(),
#struct_name_string,
#field_name_string,
&self.target,
&self.message
).rgb(255,165,0);
#(#transporter_stmts)*
}
pub async fn info(&self) {
use colorful::Colorful;
let message = format!(
"({} {} {}:{}) @INFO => {}.{} = {:#?} {}",
chrono::Local::now(),
file!(),
line!(),
column!(),
#struct_name_string,
#field_name_string,
&self.target,
&self.message
).rgb(0,191,255);
#(#transporter_stmts)*
}
pub async fn success(&self) {
use colorful::Colorful;
let message = format!(
"({} {} {}:{}) @SUCCESS => {}.{} = {:#?} {}",
chrono::Local::now(),
file!(),
line!(),
column!(),
#struct_name_string,
#field_name_string,
&self.target,
&self.message
).green();
#(#transporter_stmts)*
}
pub async fn warning(&self) {
use colorful::Colorful;
let message = format!(
"({} {} {}:{}) @WARNING => {}.{} = {:#?} {}",
chrono::Local::now(),
file!(),
line!(),
column!(),
#struct_name_string,
#field_name_string,
&self.target,
&self.message
).yellow();
#(#transporter_stmts)*
}
pub async fn err(&self) {
use colorful::Colorful;
let message = format!(
"({} {} {}:{}) @ERROR => {}.{} = {:#?} {}",
chrono::Local::now(),
file!(),
line!(),
column!(),
#struct_name_string,
#field_name_string,
&self.target,
&self.message
).rgb(255, 49, 49);
#(#transporter_stmts)*
}
pub async fn critical(&self) {
use colorful::Colorful;
let message = format!(
"({} {} {}:{}) @CRITICAL => {}.{} = {:#?} {}",
chrono::Local::now(),
file!(),
line!(),
column!(),
#struct_name_string,
#field_name_string,
&self.target,
&self.message
).red();
#(#transporter_stmts)*
}
pub async fn panic(&self) {
use colorful::Colorful;
let message = format!(
"({} {} {}:{}) @PANIC => {}.{} = {:#?} {}",
chrono::Local::now(),
file!(),
line!(),
column!(),
#struct_name_string,
#field_name_string,
&self.target,
&self.message
).rgb(225,32,254);
#(#transporter_stmts)*
}
}
impl #impl_block {
pub fn #method_name(&self) -> #field_struct_name {
#field_struct_name::new()
.target(self.#field_name.clone())
}
}
}
})
.collect();
quote! { #(#tokens)* }
}