diff --git a/src/converter.rs b/src/converter.rs index ad26532..42c0c2d 100644 --- a/src/converter.rs +++ b/src/converter.rs @@ -95,7 +95,7 @@ pub fn svg2program(doc: &Document, options: ProgramOptions, mach: Machine) -> Ve ) } - if transforms.len() != 0 { + if !transforms.is_empty() { let transform = transforms .iter() .fold(Transform2D::identity(), |acc, t| acc.post_transform(t)); @@ -121,7 +121,7 @@ pub fn svg2program(doc: &Document, options: ProgramOptions, mach: Machine) -> Ve if node.has_children() { node_stack.push((node, node.children())); name_stack.push(node_name(&node)); - } else if transforms.len() != 0 { + } else if !transforms.is_empty() { // Pop transform early, since this is the only element that has it turtle.pop_transform(); } diff --git a/src/gcode/mod.rs b/src/gcode/mod.rs index 47737af..aee17bd 100644 --- a/src/gcode/mod.rs +++ b/src/gcode/mod.rs @@ -66,7 +66,7 @@ pub fn parse_gcode(gcode: &str) -> Vec { if let Some(l) = letter { vec.push(Word { letter: l, - value: parse_value(&gcode[value_range.clone()]), + value: parse_value(&gcode[value_range]), }); } vec @@ -78,7 +78,7 @@ fn parse_value(word: &str) -> Value { } else { let index_of_dot = word.find('.'); Value::Fractional( - word[..index_of_dot.unwrap_or(word.len())] + word[..index_of_dot.unwrap_or_else(|| word.len())] .parse::() .unwrap(), index_of_dot.map(|j| word[j + 1..].parse::().unwrap()), diff --git a/src/gcode/spec.rs b/src/gcode/spec.rs index f083861..0304e3a 100644 --- a/src/gcode/spec.rs +++ b/src/gcode/spec.rs @@ -87,11 +87,11 @@ macro_rules! commands { } } - pub fn word<'a>(&'a self) -> &'a CommandWord { + pub fn word(&'_ self) -> &'_ CommandWord { &self.command_word } - pub fn get<'a>(&'a self, letter: char) -> Option<&'a Word> { + pub fn get(&'_ self, letter: char) -> Option<&'_ Word> { let letter = letter.to_ascii_uppercase(); self.arguments.iter().find(|arg| arg.letter == letter) } diff --git a/src/main.rs b/src/main.rs index d60b5d8..2be2b34 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ mod postprocess; mod turtle; fn main() -> io::Result<()> { - if let Err(_) = env::var("RUST_LOG") { + if env::var("RUST_LOG").is_err() { env::set_var("RUST_LOG", "svg2gcode=info") } env_logger::init(); diff --git a/src/postprocess.rs b/src/postprocess.rs index 5d68b04..7c2b576 100644 --- a/src/postprocess.rs +++ b/src/postprocess.rs @@ -8,17 +8,17 @@ pub fn set_origin(commands: &mut [Command], origin: F64Point) { let mut is_relative = false; let mut current_position = point(0f64, 0f64); - for i in 0..commands.len() { - match &commands[i].word() { + for command in commands { + match command.word() { RapidPositioning | LinearInterpolation => { - let x: f64 = (&commands[i].get('X').unwrap().value).into(); - let y: f64 = (&commands[i].get('Y').unwrap().value).into(); + let x: f64 = (&command.get('X').unwrap().value).into(); + let y: f64 = (&command.get('Y').unwrap().value).into(); if is_relative { current_position += vector(x, y); } else { current_position = point(x, y); - commands[i].set('X', Value::Float((current_position + offset).x)); - commands[i].set('Y', Value::Float((current_position + offset).y)); + command.set('X', Value::Float((current_position + offset).x)); + command.set('Y', Value::Float((current_position + offset).y)); } } AbsoluteDistanceMode => { @@ -36,8 +36,7 @@ fn get_bounding_box(commands: &[Command]) -> (F64Point, F64Point) { let (mut minimum, mut maximum) = (point(0f64, 0f64), point(0f64, 0f64)); let mut is_relative = false; let mut current_position = point(0f64, 0f64); - for i in 0..commands.len() { - let command = &commands[i]; + for command in commands { match command.word() { AbsoluteDistanceMode => { is_relative = false; diff --git a/src/turtle.rs b/src/turtle.rs index ef0eed8..814d88f 100644 --- a/src/turtle.rs +++ b/src/turtle.rs @@ -123,8 +123,8 @@ impl Turtle { .iter() .chain(self.machine.absolute().iter()) .chain(std::iter::once(&Self::linear_interpolation( - self.initial_position.x.into(), - self.initial_position.y.into(), + self.initial_position.x, + self.initial_position.y, z.into(), f.into(), ))) @@ -174,8 +174,8 @@ impl Turtle { .iter() .chain(self.machine.absolute().iter()) .chain(std::iter::once(&Self::linear_interpolation( - to.x.into(), - to.y.into(), + to.x, + to.y, z.into(), f.into(), ))) @@ -200,7 +200,7 @@ impl Turtle { .flattened(tolerance) .map(|point| { last_point.set(point); - Self::linear_interpolation(point.x.into(), point.y.into(), z.into(), f.into()) + Self::linear_interpolation(point.x, point.y, z, f) }) .collect(); self.current_position = last_point.get(); @@ -419,10 +419,10 @@ impl Turtle { let mut ellipse = vec![]; arc.for_each_flattened(tolerance, &mut |point: F64Point| { ellipse.push(Self::linear_interpolation( - point.x.into(), - point.y.into(), - z.into(), - f.into(), + point.x, + point.y, + z, + f, )); last_point.set(point); }); @@ -457,9 +457,7 @@ impl Turtle { /// Remove all transforms, returning to true absolute coordinates pub fn pop_all_transforms(&mut self) { - while self.transform_stack.len() != 0 { - self.pop_transform(); - } + self.transform_stack.clear(); self.current_transform = Transform2D::identity(); }