Skip to content

Commit 2a4c0de

Browse files
committed
cargo fmt
1 parent 726cebd commit 2a4c0de

File tree

5 files changed

+128
-99
lines changed

5 files changed

+128
-99
lines changed

lib/src/gcode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ mod parser {
309309
}
310310

311311
fn traditional_gcode(s: &str) -> IResult<&str, (GCodeOperation, Option<&str>)> {
312-
let (s, letter) = satisfy(|c| matches!(c.to_ascii_lowercase(), 'g' | 'm') )(s)?;
312+
let (s, letter) = satisfy(|c| matches!(c.to_ascii_lowercase(), 'g' | 'm'))(s)?;
313313
let (s, code) = match lexical_core::parse_partial::<u16>(s.as_bytes()) {
314314
Ok((_, 0)) => return Err(Err::Error(Error::from_error_kind(s, ErrorKind::Digit))),
315315
Ok((value, processed)) => (s.slice(processed..), value),

lib/src/macros.rs

Lines changed: 88 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::collections::HashMap;
22
use std::sync::{Arc, Mutex};
33

4-
use glam::{DVec4, Vec4Swizzles};
54
use glam::DVec3 as Vec3;
5+
use glam::{DVec4, Vec4Swizzles};
66

7-
use serde::{Serialize, Deserialize};
7+
use serde::{Deserialize, Serialize};
88

99
#[derive(Debug, Default, Clone)]
1010
struct MacroToolheadPosition(Arc<Mutex<Position>>);
@@ -24,8 +24,7 @@ struct PrinterObj {
2424

2525
impl std::fmt::Debug for PrinterObj {
2626
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27-
f
28-
.debug_struct("PrinterObj")
27+
f.debug_struct("PrinterObj")
2928
// .field("config_map", &self.config_map)
3029
.field("config_map", &"<...>")
3130
.field("toolhead_position", &self.toolhead_position)
@@ -43,20 +42,23 @@ impl std::fmt::Display for PrinterObj {
4342
impl minijinja::value::Object for PrinterObj {
4443
fn get_attr(&self, name: &str) -> Option<minijinja::value::Value> {
4544
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()),
5249
}
5350
}
5451

5552
fn attributes(&self) -> &[&str] {
5653
unimplemented!()
5754
}
5855

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> {
6062
let _state = state;
6163
let _args = args;
6264
Err(minijinja::Error::new(
@@ -65,27 +67,29 @@ impl minijinja::value::Object for PrinterObj {
6567
))
6668
}
6769

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> {
6975
let _state = state;
7076
let _args = args;
7177
Err(minijinja::Error::new(
7278
minijinja::ErrorKind::ImpossibleOperation,
7379
"tried to call non callable object",
7480
))
7581
}
76-
77-
7882
}
7983

8084
#[derive(Debug, Clone, Copy, Default)]
8185
struct Position(Vec3);
8286

83-
8487
impl Serialize for Position {
8588
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
8689
where
87-
S: serde::Serializer {
88-
use serde::{ser::SerializeMap};
90+
S: serde::Serializer,
91+
{
92+
use serde::ser::SerializeMap;
8993

9094
let mut map = serializer.serialize_map(Some(3))?;
9195
map.serialize_entry("x", &self.0.x)?;
@@ -112,23 +116,25 @@ impl PrinterObj {
112116
}
113117
}
114118

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 {
116123
#[derive(Debug, Serialize)]
117124
struct Toolhead {
118125
position: Position,
119126
axis_maximum: Position,
120127
homed_axes: String,
121128
}
122129

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+
));
130135

131-
let mut cfg: HashMap<String, minijinja::value::Value> = config.config
136+
let mut cfg: HashMap<String, minijinja::value::Value> = config
137+
.config
132138
.iter()
133139
.map(|(k, v)| (k.to_owned(), minijinja::value::Value::from_serializable(v)))
134140
.collect();
@@ -140,29 +146,36 @@ impl PrinterObj {
140146
cfg.insert(name, values);
141147
}
142148

143-
PrinterObj { config_map: cfg, toolhead_position, axis_maximum }
149+
PrinterObj {
150+
config_map: cfg,
151+
toolhead_position,
152+
axis_maximum,
153+
}
144154
}
145155
}
146156

147157
fn read_macros<'a, I>(macros: I) -> minijinja::Source
148-
where I: IntoIterator<Item = &'a GCodeMacro>
158+
where
159+
I: IntoIterator<Item = &'a GCodeMacro>,
149160
{
150161
let re_braces = regex::Regex::new(r"(?x) ( \{\S\} ) | ( \{[^%] ) | ( [^%]\} )").unwrap();
151162

152163
let mut src = minijinja::Source::new();
153164

154165
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();
166179

167180
src.add_template(&mac.name, res).unwrap();
168181
}
@@ -171,12 +184,17 @@ fn read_macros<'a, I>(macros: I) -> minijinja::Source
171184
}
172185

173186
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> {
175191
Ok(value)
176192
}
177193

178194
fn min<Item>(_state: &minijinja::State, value: Vec<Item>) -> Result<Item, minijinja::Error>
179-
where Item: Ord {
195+
where
196+
Item: Ord,
197+
{
180198
value.into_iter().min().ok_or_else(|| {
181199
minijinja::Error::new(
182200
minijinja::ErrorKind::InvalidArguments,
@@ -186,7 +204,9 @@ fn create_macro_environment(src: minijinja::Source) -> minijinja::Environment<'s
186204
}
187205

188206
fn max<Item>(_state: &minijinja::State, value: Vec<Item>) -> Result<Item, minijinja::Error>
189-
where Item: Ord {
207+
where
208+
Item: Ord,
209+
{
190210
value.into_iter().max().ok_or_else(|| {
191211
minijinja::Error::new(
192212
minijinja::ErrorKind::InvalidArguments,
@@ -195,7 +215,10 @@ fn create_macro_environment(src: minijinja::Source) -> minijinja::Environment<'s
195215
})
196216
}
197217

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> {
199222
Ok(())
200223
}
201224

@@ -219,8 +242,10 @@ fn create_macro_environment(src: minijinja::Source) -> minijinja::Environment<'s
219242

220243
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
221244
#[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+
{
224249
pub name: String,
225250
pub description: Option<String>,
226251
pub variables: HashMap<String, S>,
@@ -232,7 +257,7 @@ pub struct GCodeMacro<S = serde_json::Value> where
232257
#[serde(default)]
233258
pub struct MacroConfiguration {
234259
pub macros: Vec<GCodeMacro>,
235-
pub config: HashMap<String, serde_json::Value>
260+
pub config: HashMap<String, serde_json::Value>,
236261
}
237262

238263
#[derive(Debug)]
@@ -252,14 +277,16 @@ impl MacroManager {
252277
let mut macro_environment = crate::macros::create_macro_environment(src);
253278
macro_environment.add_global("printer", minijinja::value::Value::from_object(printer_obj));
254279

255-
let macro_variables = config.macros
280+
let macro_variables = config
281+
.macros
256282
.iter()
257283
.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(),
263290
)
264291
})
265292
.collect();
@@ -271,10 +298,13 @@ impl MacroManager {
271298
}
272299
}
273300

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();
278308

279309
// Update toolhead position shared with global PrinterObj
280310
self.toolhead_position.update(position);
@@ -293,13 +323,11 @@ impl MacroManager {
293323

294324
let template = self.macro_environment.get_template(name).ok()?;
295325
match template.render(inner_params) {
296-
Ok(rendered) => {
297-
Some(rendered)
298-
},
326+
Ok(rendered) => Some(rendered),
299327
Err(e) => {
300328
eprintln!("error during template: err={:?}", e);
301329
None
302330
}
303331
}
304332
}
305-
}
333+
}

lib/src/planner.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::{VecDeque, HashMap};
1+
use std::collections::{HashMap, VecDeque};
22
use std::f64::EPSILON;
33
use std::time::Duration;
44

@@ -11,7 +11,6 @@ use glam::Vec4Swizzles;
1111
use glam::{DVec3 as Vec3, DVec4 as Vec4};
1212
use serde::{Deserialize, Serialize};
1313

14-
1514
#[derive(Debug)]
1615
pub struct Planner {
1716
operations: OperationSequence,
@@ -22,7 +21,6 @@ pub struct Planner {
2221
macro_manager: crate::macros::MacroManager,
2322
}
2423

25-
2624
impl Planner {
2725
pub fn from_limits(config: PrinterConfiguration) -> Self {
2826
let firmware_retraction = config
@@ -158,8 +156,11 @@ impl Planner {
158156
// todo!()
159157
}
160158
other => {
161-
if let Some(rendered_macro) =
162-
self.macro_manager.render_macro(other, params.clone(), self.toolhead_state.position) {
159+
if let Some(rendered_macro) = self.macro_manager.render_macro(
160+
other,
161+
params.clone(),
162+
self.toolhead_state.position,
163+
) {
163164
for cmd_line in rendered_macro.lines() {
164165
let gc = crate::gcode::parse_gcode(cmd_line).unwrap();
165166
self.process_cmd(&gc);
@@ -719,7 +720,6 @@ impl MoveSequence {
719720
while self.flush_count < self.moves.len() && self.moves[self.flush_count].is_fill() {
720721
self.flush_count += 1;
721722
}
722-
723723
}
724724

725725
fn flush(&mut self) {

tool/src/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::error::Error;
22

3-
use lib_klipper::planner::{Planner, PrinterLimits, PrinterConfiguration};
3+
use lib_klipper::planner::{Planner, PrinterConfiguration, PrinterLimits};
44

55
use clap::Parser;
66
use once_cell::sync::OnceCell;
@@ -68,7 +68,11 @@ impl Opts {
6868
// Was moonraker config requested? If so, try to grab that first.
6969
if let Some(url) = &self.config_moonraker {
7070
let mut limits = PrinterLimits::default();
71-
let config = moonraker::moonraker_config(url, self.config_moonraker_api_key.as_deref(), &mut limits)?;
71+
let config = moonraker::moonraker_config(
72+
url,
73+
self.config_moonraker_api_key.as_deref(),
74+
&mut limits,
75+
)?;
7276
Ok(config)
7377
} else {
7478
Ok(PrinterConfiguration::default())
@@ -82,7 +86,6 @@ impl Opts {
8286
}
8387
}
8488

85-
8689
#[derive(Parser, Debug)]
8790
enum SubCommand {
8891
Estimate(cmd::estimate::EstimateCmd),

0 commit comments

Comments
 (0)