Skip to content

Commit 1371f91

Browse files
committed
feat: Macro generate from json file
1 parent 48c56bc commit 1371f91

File tree

11 files changed

+150
-2
lines changed

11 files changed

+150
-2
lines changed

Cargo.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ members = [
55
"yas-genshin",
66
"yas-starrail",
77
"yas-derive",
8-
"yas-application"
9-
]
8+
"yas-application",
9+
"yas-wutheringwaves"
10+
, "yas-derive-wuthering-waves"]
1011

1112
[profile.release]
1213
lto = true
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "yas-derive-wuthering-waves"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
proc-macro = true
8+
9+
[dependencies]
10+
proc-macro2 = "1.0"
11+
syn = { version = "2.0", features = ["parsing"] }
12+
quote = "1.0"
13+
serde = { version = "1", features = ["derive"] }
14+
serde_json = "1"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use serde::{Serializer, Deserialize};
2+
3+
#[derive(Deserialize)]
4+
pub struct EchoDataItem {
5+
pub name: String,
6+
pub cost: usize,
7+
pub name_chs: String,
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub use echo_item::EchoDataItem;
2+
3+
mod echo_item;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
extern crate proc_macro2;
2+
3+
use proc_macro::TokenStream;
4+
use crate::echoes::EchoDataItem;
5+
use quote::quote;
6+
7+
mod echoes;
8+
9+
fn get_echo_names(data: &[EchoDataItem]) -> Vec<proc_macro2::TokenStream> {
10+
let mut result = Vec::new();
11+
12+
for item in data.iter() {
13+
result.push(item.name.parse().unwrap());
14+
}
15+
16+
result
17+
}
18+
19+
fn echo_name_from_chs(data: &[EchoDataItem], echo_names: &[proc_macro2::TokenStream]) -> proc_macro2::TokenStream {
20+
let chs_names: Vec<_> = data.iter().map(|x| x.name_chs.clone()).collect();
21+
22+
quote! {
23+
impl WWEchoName {
24+
pub fn from_chs(chs: &str) -> Self {
25+
match chs {
26+
#(#chs_names => Self::#echo_names),*
27+
_ => panic!("Unknown chs name"),
28+
}
29+
}
30+
}
31+
}
32+
}
33+
34+
#[proc_macro]
35+
pub fn yas_wuthering_waves_echoes(input: TokenStream) -> TokenStream {
36+
let ast: syn::LitStr = syn::parse(input).unwrap();
37+
38+
let filename = ast.value();
39+
40+
let content = std::fs::read_to_string(filename).unwrap();
41+
let echo_data: Vec<EchoDataItem> = serde_json::from_str(&content).unwrap();
42+
43+
let echo_names = get_echo_names(&echo_data);
44+
println!("{:?}", echo_names);
45+
46+
let echo_name_enum = quote! {
47+
pub enum WWEchoName {
48+
#(#echo_names),*
49+
}
50+
};
51+
let echo_name_from_chs_impl = echo_name_from_chs(&echo_data, &echo_names);
52+
53+
let result = quote! {
54+
#echo_name_enum
55+
#echo_name_from_chs_impl
56+
};
57+
58+
result.into()
59+
}

yas-wutheringwaves/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "yas-wutheringwaves"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
yas-derive-wuthering-waves = { path = "../yas-derive-wuthering-waves" }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use crate::echo::WWEchoName;
2+
3+
pub struct WWEcho {
4+
pub name: WWEchoName,
5+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use yas_derive_wuthering_waves::yas_wuthering_waves_echoes;
2+
3+
yas_wuthering_waves_echoes!("yas-wutheringwaves/data/echoes.json");
4+
5+
// pub enum WWEchoName {
6+
// ThunderingMephis,
7+
// InfernoRider,
8+
// BellBorneGeochelone,
9+
// TempestMephis,
10+
// Crownless,
11+
// FeilianBeringal,
12+
// LampylumenMyriad,
13+
// MourningAix,
14+
// MechAbomination,
15+
// ImpermanenceHeron,
16+
// Dreamless,
17+
// Jue,
18+
// VioletFeatheredHeron,
19+
// CyanFeatheredHeron,
20+
// StonewallBracer,
21+
// Flautist,
22+
// Tambourinist,
23+
// ChasmGuardian,
24+
// RocksteadyGuardian,
25+
// ViridblazeSaurian,
26+
// Roseshroom,
27+
// Spearback,
28+
// }

yas-wutheringwaves/src/echo/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub use echo_name::WWEchoName;
2+
3+
mod echo;
4+
mod echo_name;

0 commit comments

Comments
 (0)