File tree Expand file tree Collapse file tree 11 files changed +150
-2
lines changed
yas-derive-wuthering-waves Expand file tree Collapse file tree 11 files changed +150
-2
lines changed Original file line number Diff line number Diff 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 ]
1213lto = true
Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ pub use echo_item:: EchoDataItem ;
2+
3+ mod echo_item;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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" }
Original file line number Diff line number Diff line change 1+ use crate :: echo:: WWEchoName ;
2+
3+ pub struct WWEcho {
4+ pub name : WWEchoName ,
5+ }
Original file line number Diff line number Diff line change 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+ // }
Original file line number Diff line number Diff line change 1+ pub use echo_name:: WWEchoName ;
2+
3+ mod echo;
4+ mod echo_name;
You can’t perform that action at this time.
0 commit comments