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
167
168
169
170
#![feature(fs_try_exists)]
use std::io::{Read, Write};
use proc_macro::TokenStream;
use syn::parse_macro_input;
use syn::DeriveInput;
mod to_snake_case;
use to_snake_case::ToSnakeCase;
#[proc_macro_derive(GameStates)]
pub fn game_states(input: proc_macro::TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let enum_name: &str = &input.ident.to_string();
let path_name: &str=&ToSnakeCase::new(enum_name).collect::<String>();
if !std::fs::try_exists(String::from("src/") + path_name).unwrap() {
std::fs::create_dir_all(String::from("src/") + path_name + "/config").unwrap();
let mut mod_rs_file = std::fs::File::options()
.write(true)
.create(true)
.open(String::from("src/") + path_name + "/mod.rs")
.unwrap();
let mut file = std::fs::File::options()
.write(true)
.create(true)
.open(String::from("src/") + path_name + "/config/info.txt")
.unwrap();
let mut init_file = std::fs::File::options()
.write(true)
.create(true)
.open(String::from("src/") + path_name + "/config/init.txt")
.unwrap();
writeln!(init_file, "app.add_loopless_state(*self)").unwrap();
match input.data {
syn::Data::Enum(ref data_struct) => {
for variant in &data_struct.variants {
let variant_name: &str = &variant.ident.to_string();
let struct_file_name: &str=&ToSnakeCase::new(variant_name).collect::<String>();
writeln!(file, "{}", variant_name).unwrap();
writeln!(init_file, ".add_plugin({})", variant_name).unwrap();
writeln!(mod_rs_file, "mod {};", struct_file_name).unwrap();
writeln!(mod_rs_file, "use {}::{};", struct_file_name, variant_name).unwrap();
create_file(path_name,struct_file_name,enum_name, variant_name).unwrap();
}
}
_ => (),
}
let over_string = include_str!("../template_mod.txt").replace("$", enum_name);
write!(mod_rs_file, "{}", over_string).unwrap();
file.flush().unwrap();
init_file.flush().unwrap();
mod_rs_file.flush().unwrap();
} else {
let mut file = std::fs::File::options()
.read(true)
.write(true)
.open(String::from("src/") + path_name + "/config/info.txt")
.unwrap();
let mut mod_rs_file = std::fs::File::options()
.append(true)
.open(String::from("src/") + path_name + "/mod.rs")
.unwrap();
let mut init_file = std::fs::File::options()
.append(true)
.open(String::from("src/") + path_name + "/config/init.txt")
.unwrap();
let mut file_str = String::new();
file.read_to_string(&mut file_str).unwrap();
let arr = file_str.split('\n').collect::<Vec<_>>();
match input.data {
syn::Data::Enum(ref data_struct) => {
for variant in &data_struct.variants {
let variant_name: &str = &variant.ident.to_string();
let struct_file_name: &str = &variant_name.to_ascii_lowercase();
if !arr.contains(&variant_name) {
writeln!(file, "{}", variant_name).unwrap();
writeln!(init_file, ".add_plugin({})", variant_name).unwrap();
writeln!(mod_rs_file, "mod {};", struct_file_name).unwrap();
writeln!(mod_rs_file, "use {}::{};", struct_file_name, variant_name).unwrap();
create_file(path_name,struct_file_name,enum_name, variant_name).unwrap();
}
}
}
_ => (),
}
file.flush().unwrap();
init_file.flush().unwrap();
mod_rs_file.flush().unwrap();
}
TokenStream::new()
}
#[inline]
fn create_file(path: &str, file_name: &str,enum_name:&str,struct_name:&str) -> std::io::Result<()> {
let mut struct_file = std::fs::File::create(
String::from("src/")
+ &path
+ "/"
+ &file_name
+ ".rs",
)?;
let over_str = include_str!("../template.txt")
.replace("$", enum_name)
.replace("~", struct_name);
write!(struct_file, "{}", over_str)
}