@@ -8,14 +8,36 @@ use pyo3::{
8
8
use super :: CalcResult ;
9
9
use crate :: { methods:: pp, objects:: Beatmap , set_calculator} ;
10
10
11
- macro_rules! generate_func {
12
- ( { $( $attr: ident: $type: ty) ,* } ) => {
11
+ macro_rules! create_py_methods {
12
+ ( $for : ident , fn get_set_del ( ) { $( $attr: ident: $type: ty) ,* } , impl { $ ( $others : tt ) * } ) => {
13
13
paste:: paste! {
14
- $(
15
- pub fn [ <get_ $attr>] ( & self ) -> $type { self . $attr }
16
- pub fn [ <set_ $attr>] ( & mut self , value: $type) { self . $attr = value; }
17
- pub fn [ <del_ $attr>] ( & mut self ) { self . $attr = None ; }
18
- ) *
14
+ #[ pymethods] impl $for {
15
+ #[ new]
16
+ #[ args( data = "None" , kwargs = "**" ) ]
17
+ pub fn new( data: Option <& PyDict >, kwargs: Option <& PyDict >) -> PyResult <Self > {
18
+ let mut slf = Self :: default ( ) ;
19
+ if let Some ( d) = data. or( kwargs) {
20
+ Self :: set_with_dict( & mut slf, d) ?;
21
+ }
22
+ Ok ( slf)
23
+ }
24
+
25
+ pub fn getattr<' a>( slf: & ' a PyCell <Self >, attr: & PyAny ) -> PyResult <& ' a PyAny > {
26
+ slf. as_ref( ) . getattr( attr)
27
+ }
28
+
29
+ pub fn setattr<' a>( slf: & PyCell <Self >, attr: & PyAny , value: & PyAny ) -> PyResult <( ) > {
30
+ slf. as_ref( ) . setattr( attr, value)
31
+ }
32
+
33
+ $(
34
+ pub fn [ <r#get_ $attr>] ( & self ) -> $type { self . $attr }
35
+ pub fn [ <r#set_ $attr>] ( & mut self , value: $type) { self . $attr = value; }
36
+ pub fn [ <r#del_ $attr>] ( & mut self ) { self . $attr = None ; }
37
+ ) *
38
+
39
+ $( $others) *
40
+ }
19
41
}
20
42
} ;
21
43
}
@@ -47,10 +69,9 @@ pub struct Calculator {
47
69
pub score : Option < u32 > ,
48
70
}
49
71
crate :: pyo3_py_protocol!( Calculator ) ;
50
-
51
- #[ pymethods]
52
- impl Calculator {
53
- generate_func ! ( {
72
+ create_py_methods ! (
73
+ Calculator ,
74
+ fn get_set_del( ) {
54
75
mode: Option <u8 >,
55
76
mods: Option <u32 >,
56
77
n50: Option <usize >,
@@ -62,125 +83,108 @@ impl Calculator {
62
83
combo: Option <usize >,
63
84
miss: Option <usize >,
64
85
score: Option <u32 >
65
- } ) ;
66
-
67
- #[ new]
68
- #[ args( data = "None" , kwargs = "**" ) ]
69
- pub fn new ( data : Option < & PyDict > , kwargs : Option < & PyDict > ) -> PyResult < Self > {
70
- let mut slf = Self :: default ( ) ;
71
- if let Some ( d) = data. or ( kwargs) {
72
- Self :: set_with_dict ( & mut slf, d) ?;
86
+ } ,
87
+ impl {
88
+ #[ staticmethod]
89
+ pub fn new_empty( ) -> Self {
90
+ Self :: default ( )
73
91
}
74
- Ok ( slf)
75
- }
76
-
77
- #[ staticmethod]
78
- pub fn new_empty ( ) -> Self {
79
- Self :: default ( )
80
- }
81
-
82
- #[ inline( always) ]
83
- pub fn reset ( & mut self ) {
84
- self . mode = None ;
85
- self . mods = None ;
86
- self . n50 = None ;
87
- self . n100 = None ;
88
- self . n300 = None ;
89
- self . katu = None ;
90
- self . acc = None ;
91
- self . passed_obj = None ;
92
- self . combo = None ;
93
- self . miss = None ;
94
- self . score = None ;
95
- }
96
92
97
- #[ inline( always) ]
98
- pub fn calculate_raw ( & self , beatmap : & Beatmap ) -> CalcResult {
99
- CalcResult ( self . calc ( beatmap) )
100
- }
101
-
102
- pub fn getattr < ' a > ( slf : & ' a PyCell < Self > , attr : & PyAny ) -> PyResult < & ' a PyAny > {
103
- slf. as_ref ( ) . getattr ( attr)
104
- }
93
+ #[ inline( always) ]
94
+ pub fn reset( & mut self ) {
95
+ self . mode = None ;
96
+ self . mods = None ;
97
+ self . n50 = None ;
98
+ self . n100 = None ;
99
+ self . n300 = None ;
100
+ self . katu = None ;
101
+ self . acc = None ;
102
+ self . passed_obj = None ;
103
+ self . combo = None ;
104
+ self . miss = None ;
105
+ self . score = None ;
106
+ }
105
107
106
- pub fn setattr < ' a > ( slf : & PyCell < Self > , attr : & PyAny , value : & PyAny ) -> PyResult < ( ) > {
107
- slf. as_ref ( ) . setattr ( attr, value)
108
- }
108
+ #[ inline( always) ]
109
+ pub fn calculate_raw( & self , beatmap: & Beatmap ) -> CalcResult {
110
+ CalcResult ( self . calc( beatmap) )
111
+ }
109
112
110
- #[ inline( always) ]
111
- pub fn set_with_str ( & mut self , attr : & str , value : & PyAny ) -> PyResult < ( ) > {
112
- crate :: set_with_py_str!( self , attr, value; {
113
- mode,
114
- mods,
115
- n50,
116
- n100,
117
- n300,
118
- katu,
119
- acc,
120
- passed_obj,
121
- combo,
122
- miss,
123
- score
124
- } ) ;
125
- Ok ( ( ) )
126
- }
113
+ #[ inline( always) ]
114
+ pub fn set_with_str( & mut self , attr: & str , value: & PyAny ) -> PyResult <( ) > {
115
+ crate :: set_with_py_str!( self , attr, value; {
116
+ mode,
117
+ mods,
118
+ n50,
119
+ n100,
120
+ n300,
121
+ katu,
122
+ acc,
123
+ passed_obj,
124
+ combo,
125
+ miss,
126
+ score
127
+ } ) ;
128
+ Ok ( ( ) )
129
+ }
127
130
128
- #[ inline( always) ]
129
- pub fn set_with_dict ( & mut self , data : & PyDict ) -> PyResult < ( ) > {
130
- for ( k, v) in data. iter ( ) {
131
- self . set_with_str ( & k. extract :: < String > ( ) ?, v) ?;
131
+ #[ inline( always) ]
132
+ pub fn set_with_dict( & mut self , data: & PyDict ) -> PyResult <( ) > {
133
+ for ( k, v) in data. iter( ) {
134
+ self . set_with_str( & k. extract:: <String >( ) ?, v) ?;
135
+ }
136
+ Ok ( ( ) )
132
137
}
133
- Ok ( ( ) )
134
- }
135
138
136
- #[ getter]
137
- pub fn attrs ( & self ) -> String {
138
- self . as_string ( )
139
- }
139
+ #[ getter]
140
+ pub fn attrs( & self ) -> String {
141
+ self . as_string( )
142
+ }
140
143
141
- #[ getter]
142
- pub fn attrs_dict < ' a > ( & self , py : Python < ' a > ) -> PyResult < & ' a PyDict > {
143
- self . as_dict ( py)
144
- }
144
+ #[ getter]
145
+ pub fn attrs_dict<' a>( & self , py: Python <' a>) -> PyResult <& ' a PyDict > {
146
+ self . as_dict( py)
147
+ }
145
148
146
- #[ getter]
147
- #[ inline( always) ]
148
- pub fn as_string ( & self ) -> String {
149
- format ! (
150
- "mode: {:?}, mods: {:?}, n50: {:?}, n100: {:?}, n300: {:?}, katu: {:?}, acc: {:?}, passed_obj: {:?}, combo: {:?}, miss: {:?}, score: {:?}" ,
151
- self . mode,
152
- self . mods,
153
- self . n50,
154
- self . n100,
155
- self . n300,
156
- self . katu,
157
- self . acc,
158
- self . passed_obj,
159
- self . combo,
160
- self . miss,
161
- self . score,
162
- )
163
- }
149
+ #[ getter]
150
+ #[ inline( always) ]
151
+ pub fn as_string( & self ) -> String {
152
+ format!(
153
+ "mode: {:?}, mods: {:?}, n50: {:?}, n100: {:?}, n300: {:?}, katu: {:?}, acc: {:?}, passed_obj: {:?}, combo: {:?}, miss: {:?}, score: {:?}" ,
154
+ self . mode,
155
+ self . mods,
156
+ self . n50,
157
+ self . n100,
158
+ self . n300,
159
+ self . katu,
160
+ self . acc,
161
+ self . passed_obj,
162
+ self . combo,
163
+ self . miss,
164
+ self . score,
165
+ )
166
+ }
164
167
165
- #[ getter]
166
- #[ inline( always) ]
167
- pub fn as_dict < ' a > ( & self , py : Python < ' a > ) -> PyResult < & ' a PyDict > {
168
- let d = crate :: pyo3_py_dict!( py, self ; {
169
- mode,
170
- mods,
171
- n50,
172
- n100,
173
- n300,
174
- katu,
175
- acc,
176
- passed_obj,
177
- combo,
178
- miss,
179
- score
180
- } ) ;
181
- Ok ( d)
168
+ #[ getter]
169
+ #[ inline( always) ]
170
+ pub fn as_dict<' a>( & self , py: Python <' a>) -> PyResult <& ' a PyDict > {
171
+ let d = crate :: pyo3_py_dict!( py, self ; {
172
+ mode,
173
+ mods,
174
+ n50,
175
+ n100,
176
+ n300,
177
+ katu,
178
+ acc,
179
+ passed_obj,
180
+ combo,
181
+ miss,
182
+ score
183
+ } ) ;
184
+ Ok ( d)
185
+ }
182
186
}
183
- }
187
+ ) ;
184
188
185
189
impl Calculator {
186
190
#[ inline( always) ]
0 commit comments