1
1
use std:: collections:: HashMap ;
2
2
use std:: sync:: { Arc , Mutex } ;
3
3
4
- use glam:: { DVec4 , Vec4Swizzles } ;
5
4
use glam:: DVec3 as Vec3 ;
5
+ use glam:: { DVec4 , Vec4Swizzles } ;
6
6
7
- use serde:: { Serialize , Deserialize } ;
7
+ use serde:: { Deserialize , Serialize } ;
8
8
9
9
#[ derive( Debug , Default , Clone ) ]
10
10
struct MacroToolheadPosition ( Arc < Mutex < Position > > ) ;
@@ -24,8 +24,7 @@ struct PrinterObj {
24
24
25
25
impl std:: fmt:: Debug for PrinterObj {
26
26
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
27
- f
28
- . debug_struct ( "PrinterObj" )
27
+ f. debug_struct ( "PrinterObj" )
29
28
// .field("config_map", &self.config_map)
30
29
. field ( "config_map" , & "<...>" )
31
30
. field ( "toolhead_position" , & self . toolhead_position )
@@ -43,20 +42,23 @@ impl std::fmt::Display for PrinterObj {
43
42
impl minijinja:: value:: Object for PrinterObj {
44
43
fn get_attr ( & self , name : & str ) -> Option < minijinja:: value:: Value > {
45
44
match name {
46
- "toolhead" => {
47
- Some ( minijinja:: value:: Value :: from_serializable ( & self . get_toolhead ( ) ) )
48
- }
49
- name => {
50
- self . config_map . get ( name) . map ( |v| v. to_owned ( ) )
51
- }
45
+ "toolhead" => Some ( minijinja:: value:: Value :: from_serializable (
46
+ & self . get_toolhead ( ) ,
47
+ ) ) ,
48
+ name => self . config_map . get ( name) . map ( |v| v. to_owned ( ) ) ,
52
49
}
53
50
}
54
51
55
52
fn attributes ( & self ) -> & [ & str ] {
56
53
unimplemented ! ( )
57
54
}
58
55
59
- fn call_method ( & self , state : & minijinja:: State , name : & str , args : Vec < minijinja:: value:: Value > ) -> Result < minijinja:: value:: Value , minijinja:: Error > {
56
+ fn call_method (
57
+ & self ,
58
+ state : & minijinja:: State ,
59
+ name : & str ,
60
+ args : Vec < minijinja:: value:: Value > ,
61
+ ) -> Result < minijinja:: value:: Value , minijinja:: Error > {
60
62
let _state = state;
61
63
let _args = args;
62
64
Err ( minijinja:: Error :: new (
@@ -65,27 +67,29 @@ impl minijinja::value::Object for PrinterObj {
65
67
) )
66
68
}
67
69
68
- fn call ( & self , state : & minijinja:: State , args : Vec < minijinja:: value:: Value > ) -> Result < minijinja:: value:: Value , minijinja:: Error > {
70
+ fn call (
71
+ & self ,
72
+ state : & minijinja:: State ,
73
+ args : Vec < minijinja:: value:: Value > ,
74
+ ) -> Result < minijinja:: value:: Value , minijinja:: Error > {
69
75
let _state = state;
70
76
let _args = args;
71
77
Err ( minijinja:: Error :: new (
72
78
minijinja:: ErrorKind :: ImpossibleOperation ,
73
79
"tried to call non callable object" ,
74
80
) )
75
81
}
76
-
77
-
78
82
}
79
83
80
84
#[ derive( Debug , Clone , Copy , Default ) ]
81
85
struct Position ( Vec3 ) ;
82
86
83
-
84
87
impl Serialize for Position {
85
88
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
86
89
where
87
- S : serde:: Serializer {
88
- use serde:: { ser:: SerializeMap } ;
90
+ S : serde:: Serializer ,
91
+ {
92
+ use serde:: ser:: SerializeMap ;
89
93
90
94
let mut map = serializer. serialize_map ( Some ( 3 ) ) ?;
91
95
map. serialize_entry ( "x" , & self . 0 . x ) ?;
@@ -112,23 +116,25 @@ impl PrinterObj {
112
116
}
113
117
}
114
118
115
- fn new ( config : & crate :: planner:: MacroConfiguration , toolhead_position : MacroToolheadPosition ) -> Self {
119
+ fn new (
120
+ config : & crate :: planner:: MacroConfiguration ,
121
+ toolhead_position : MacroToolheadPosition ,
122
+ ) -> Self {
116
123
#[ derive( Debug , Serialize ) ]
117
124
struct Toolhead {
118
125
position : Position ,
119
126
axis_maximum : Position ,
120
127
homed_axes : String ,
121
128
}
122
129
123
- let axis_maximum = Position (
124
- Vec3 :: new (
125
- config. config [ "stepper_x" ] [ "position_max" ] . as_f64 ( ) . unwrap ( ) as f64 ,
126
- config. config [ "stepper_y" ] [ "position_max" ] . as_f64 ( ) . unwrap ( ) as f64 ,
127
- config. config [ "stepper_z" ] [ "position_max" ] . as_f64 ( ) . unwrap ( ) as f64 ,
128
- )
129
- ) ;
130
+ let axis_maximum = Position ( Vec3 :: new (
131
+ config. config [ "stepper_x" ] [ "position_max" ] . as_f64 ( ) . unwrap ( ) as f64 ,
132
+ config. config [ "stepper_y" ] [ "position_max" ] . as_f64 ( ) . unwrap ( ) as f64 ,
133
+ config. config [ "stepper_z" ] [ "position_max" ] . as_f64 ( ) . unwrap ( ) as f64 ,
134
+ ) ) ;
130
135
131
- let mut cfg: HashMap < String , minijinja:: value:: Value > = config. config
136
+ let mut cfg: HashMap < String , minijinja:: value:: Value > = config
137
+ . config
132
138
. iter ( )
133
139
. map ( |( k, v) | ( k. to_owned ( ) , minijinja:: value:: Value :: from_serializable ( v) ) )
134
140
. collect ( ) ;
@@ -140,29 +146,36 @@ impl PrinterObj {
140
146
cfg. insert ( name, values) ;
141
147
}
142
148
143
- PrinterObj { config_map : cfg, toolhead_position, axis_maximum }
149
+ PrinterObj {
150
+ config_map : cfg,
151
+ toolhead_position,
152
+ axis_maximum,
153
+ }
144
154
}
145
155
}
146
156
147
157
fn read_macros < ' a , I > ( macros : I ) -> minijinja:: Source
148
- where I : IntoIterator < Item = & ' a GCodeMacro >
158
+ where
159
+ I : IntoIterator < Item = & ' a GCodeMacro > ,
149
160
{
150
161
let re_braces = regex:: Regex :: new ( r"(?x) ( \{\S\} ) | ( \{[^%] ) | ( [^%]\} )" ) . unwrap ( ) ;
151
162
152
163
let mut src = minijinja:: Source :: new ( ) ;
153
164
154
165
for mac in macros {
155
- let res = re_braces. replace_all ( & mac. gcode , |cap : & regex:: Captures | {
156
- if let Some ( m) = cap. get ( 1 ) {
157
- "{" . to_owned ( ) + m. as_str ( ) + "}"
158
- } else if let Some ( m) = cap. get ( 2 ) {
159
- "{" . to_owned ( ) + m. as_str ( )
160
- } else if let Some ( m) = cap. get ( 3 ) {
161
- m. as_str ( ) . to_owned ( ) + "}"
162
- } else {
163
- unreachable ! ( )
164
- }
165
- } ) . to_ascii_lowercase ( ) ;
166
+ let res = re_braces
167
+ . replace_all ( & mac. gcode , |cap : & regex:: Captures | {
168
+ if let Some ( m) = cap. get ( 1 ) {
169
+ "{" . to_owned ( ) + m. as_str ( ) + "}"
170
+ } else if let Some ( m) = cap. get ( 2 ) {
171
+ "{" . to_owned ( ) + m. as_str ( )
172
+ } else if let Some ( m) = cap. get ( 3 ) {
173
+ m. as_str ( ) . to_owned ( ) + "}"
174
+ } else {
175
+ unreachable ! ( )
176
+ }
177
+ } )
178
+ . to_ascii_lowercase ( ) ;
166
179
167
180
src. add_template ( & mac. name , res) . unwrap ( ) ;
168
181
}
@@ -171,12 +184,17 @@ fn read_macros<'a, I>(macros: I) -> minijinja::Source
171
184
}
172
185
173
186
fn create_macro_environment ( src : minijinja:: Source ) -> minijinja:: Environment < ' static > {
174
- fn any_id ( _state : & minijinja:: State , value : minijinja:: value:: Value ) -> Result < minijinja:: value:: Value , minijinja:: Error > {
187
+ fn any_id (
188
+ _state : & minijinja:: State ,
189
+ value : minijinja:: value:: Value ,
190
+ ) -> Result < minijinja:: value:: Value , minijinja:: Error > {
175
191
Ok ( value)
176
192
}
177
193
178
194
fn min < Item > ( _state : & minijinja:: State , value : Vec < Item > ) -> Result < Item , minijinja:: Error >
179
- where Item : Ord {
195
+ where
196
+ Item : Ord ,
197
+ {
180
198
value. into_iter ( ) . min ( ) . ok_or_else ( || {
181
199
minijinja:: Error :: new (
182
200
minijinja:: ErrorKind :: InvalidArguments ,
@@ -186,7 +204,9 @@ fn create_macro_environment(src: minijinja::Source) -> minijinja::Environment<'s
186
204
}
187
205
188
206
fn max < Item > ( _state : & minijinja:: State , value : Vec < Item > ) -> Result < Item , minijinja:: Error >
189
- where Item : Ord {
207
+ where
208
+ Item : Ord ,
209
+ {
190
210
value. into_iter ( ) . max ( ) . ok_or_else ( || {
191
211
minijinja:: Error :: new (
192
212
minijinja:: ErrorKind :: InvalidArguments ,
@@ -195,7 +215,10 @@ fn create_macro_environment(src: minijinja::Source) -> minijinja::Environment<'s
195
215
} )
196
216
}
197
217
198
- fn action_nop ( _state : & minijinja:: State , _value : minijinja:: value:: Value ) -> Result < ( ) , minijinja:: Error > {
218
+ fn action_nop (
219
+ _state : & minijinja:: State ,
220
+ _value : minijinja:: value:: Value ,
221
+ ) -> Result < ( ) , minijinja:: Error > {
199
222
Ok ( ( ) )
200
223
}
201
224
@@ -219,8 +242,10 @@ fn create_macro_environment(src: minijinja::Source) -> minijinja::Environment<'s
219
242
220
243
#[ derive( Debug , Clone , Default , Serialize , Deserialize ) ]
221
244
#[ serde( default ) ]
222
- pub struct GCodeMacro < S = serde_json:: Value > where
223
- S : Serialize {
245
+ pub struct GCodeMacro < S = serde_json:: Value >
246
+ where
247
+ S : Serialize ,
248
+ {
224
249
pub name : String ,
225
250
pub description : Option < String > ,
226
251
pub variables : HashMap < String , S > ,
@@ -232,7 +257,7 @@ pub struct GCodeMacro<S = serde_json::Value> where
232
257
#[ serde( default ) ]
233
258
pub struct MacroConfiguration {
234
259
pub macros : Vec < GCodeMacro > ,
235
- pub config : HashMap < String , serde_json:: Value >
260
+ pub config : HashMap < String , serde_json:: Value > ,
236
261
}
237
262
238
263
#[ derive( Debug ) ]
@@ -252,14 +277,16 @@ impl MacroManager {
252
277
let mut macro_environment = crate :: macros:: create_macro_environment ( src) ;
253
278
macro_environment. add_global ( "printer" , minijinja:: value:: Value :: from_object ( printer_obj) ) ;
254
279
255
- let macro_variables = config. macros
280
+ let macro_variables = config
281
+ . macros
256
282
. iter ( )
257
283
. map ( |mac| {
258
- ( mac. name . to_owned ( ) ,
259
- mac. variables
260
- . iter ( )
261
- . map ( |( k, v) | ( k. to_owned ( ) , minijinja:: value:: Value :: from_serializable ( v) ) )
262
- . collect ( )
284
+ (
285
+ mac. name . to_owned ( ) ,
286
+ mac. variables
287
+ . iter ( )
288
+ . map ( |( k, v) | ( k. to_owned ( ) , minijinja:: value:: Value :: from_serializable ( v) ) )
289
+ . collect ( ) ,
263
290
)
264
291
} )
265
292
. collect ( ) ;
@@ -271,10 +298,13 @@ impl MacroManager {
271
298
}
272
299
}
273
300
274
- pub fn render_macro ( & self , name : & str , params : crate :: gcode:: GCodeExtendedParams , position : DVec4 ) -> Option < String > {
275
- let vars = self . macro_variables
276
- . get ( name) ?
277
- . to_owned ( ) ;
301
+ pub fn render_macro (
302
+ & self ,
303
+ name : & str ,
304
+ params : crate :: gcode:: GCodeExtendedParams ,
305
+ position : DVec4 ,
306
+ ) -> Option < String > {
307
+ let vars = self . macro_variables . get ( name) ?. to_owned ( ) ;
278
308
279
309
// Update toolhead position shared with global PrinterObj
280
310
self . toolhead_position . update ( position) ;
@@ -293,13 +323,11 @@ impl MacroManager {
293
323
294
324
let template = self . macro_environment . get_template ( name) . ok ( ) ?;
295
325
match template. render ( inner_params) {
296
- Ok ( rendered) => {
297
- Some ( rendered)
298
- } ,
326
+ Ok ( rendered) => Some ( rendered) ,
299
327
Err ( e) => {
300
328
eprintln ! ( "error during template: err={:?}" , e) ;
301
329
None
302
330
}
303
331
}
304
332
}
305
- }
333
+ }
0 commit comments