From c84c38544bc6e81f0b0482e4e82b6c95848c1a0c Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 6 Apr 2021 14:54:54 +0530 Subject: apply clippy lints --- src/app.rs | 101 +++++++++++++++++++++++++++++-------------------------------- 1 file changed, 48 insertions(+), 53 deletions(-) (limited to 'src/app.rs') diff --git a/src/app.rs b/src/app.rs index 33d4934..f42b48c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -108,10 +108,10 @@ impl<'ctx> AppState<'ctx> { let y_min = self.start.y(); let x_max = self.start.x() + (self.width() * self.zoom as u32) as i32; let y_max = self.start.y() + (self.height() * self.zoom as u32) as i32; - return ( + ( Point::new(x_min, y_min), Point::new(x_max as i32, y_max as i32), - ); + ) } pub fn change_active_color(&mut self) { @@ -125,7 +125,7 @@ impl<'ctx> AppState<'ctx> { let rel_p = p - self.start; // reduce p based on zoom and cell size let (sx, sy) = (rel_p.x() / self.zoom as i32, rel_p.y() / self.zoom as i32); - return Some((sx as u32, sy as u32)); + Some((sx as u32, sy as u32)) } else { None } @@ -254,7 +254,7 @@ impl<'ctx> AppState<'ctx> { let op = self .current_operation .drain(..) - .filter(|v| !v.old == v.new) + .filter(|v| v.old != v.new) .collect::>(); self.undo_stack.push(ModifyRecord::Paint(op)); } @@ -267,7 +267,7 @@ impl<'ctx> AppState<'ctx> { let (x2, y2) = (p.0 * (1 + self.zoom as u32), p.1 * (1 + self.zoom as u32)); let diffx = x2 as i32 - x1 as i32; let diffy = y2 as i32 - y1 as i32; - self.start = self.start - Point::from((diffx, diffy)); + self.start -= Point::from((diffx, diffy)); } self.zoom += 1; } @@ -280,7 +280,7 @@ impl<'ctx> AppState<'ctx> { let (x2, y2) = (p.0 * (self.zoom as u32 - 1), p.1 * (self.zoom as u32 - 1)); let diffx = x2 as i32 - x1 as i32; let diffy = y2 as i32 - y1 as i32; - self.start = self.start - Point::from((diffx, diffy)); + self.start -= Point::from((diffx, diffy)); } self.zoom -= 1; } @@ -310,7 +310,7 @@ impl<'ctx> AppState<'ctx> { pub fn eval_command(&mut self) { let lisp_expr = &self.command_box.text; - let mut parser = Parser::new(Lexer::new(lisp_expr, 0)); + let mut parser = Parser::new(Lexer::new(lisp_expr)); let res = parser.parse_single_expr(); match res { Ok(expr) => { @@ -393,7 +393,7 @@ impl<'ctx> AppState<'ctx> { let mouse_coords = if let Some((x, y)) = self.idx_at_coord(self.mouse) { format!("{:3}, {:3}", x + 1, y + 1) } else { - format!("---, ---") + String::from("---, ---") }; let status_text = format!( "{} [PT {}][KIND {}]", @@ -478,32 +478,29 @@ impl<'ctx> AppState<'ctx> { } } } - match self.brush { - Brush::Line(LineBrush { start, size, .. }) => { - let size = self.zoom as u32 * (size as u32 + 5); - if let (Some(from), Some(to)) = (start, pt) { - let line = self.pixmap.get_line(from, to.into()); - draw_text( - &mut self.canvas, - self.ttf_context, - format!("{}°", positive_angle_with_x(from, to.into())), - PINK, - (self.mouse.0 + size as i32, self.mouse.1 + size as i32), - ); - for MapPoint { x, y } in line.into_iter() { - self.canvas.set_draw_color(PINK); - self.canvas - .fill_rect(Rect::new( - x as i32 * cs as i32 + self.start.x(), - y as i32 * cs as i32 + self.start.y(), - cs, - cs, - )) - .unwrap(); - } + if let Brush::Line(LineBrush { start, size, .. }) = self.brush { + let size = self.zoom as u32 * (size as u32 + 5); + if let (Some(from), Some(to)) = (start, pt) { + let line = self.pixmap.get_line(from, to.into()); + draw_text( + &mut self.canvas, + self.ttf_context, + format!("{}°", positive_angle_with_x(from, to.into())), + PINK, + (self.mouse.0 + size as i32, self.mouse.1 + size as i32), + ); + for MapPoint { x, y } in line.into_iter() { + self.canvas.set_draw_color(PINK); + self.canvas + .fill_rect(Rect::new( + x as i32 * cs as i32 + self.start.x(), + y as i32 * cs as i32 + self.start.y(), + cs, + cs, + )) + .unwrap(); } } - _ => {} } } @@ -530,14 +527,12 @@ impl<'ctx> AppState<'ctx> { fn draw_symmetry(&mut self) { let Symmetry { x, y } = self.symmetry; - x.and_then(|line| { - self.draw_line_to_grid(line, Axis::X, CYAN); - Some(()) - }); - y.and_then(|line| { - self.draw_line_to_grid(line, Axis::Y, CYAN); - Some(()) - }); + if let Some(line) = x { + self.draw_line_to_grid(line, Axis::X, CYAN) + } + if let Some(line) = y { + self.draw_line_to_grid(line, Axis::Y, CYAN) + } } fn draw_guides(&mut self) { @@ -626,7 +621,7 @@ impl<'ctx> AppState<'ctx> { .build() .map_err(|e| AppError::Sdl(e.to_string()))?; - let data = start_data.unwrap_or(vec![false; (width * height) as usize]); + let data = start_data.unwrap_or_else(|| vec![false; (width * height) as usize]); let pixmap = Pixmap::new_with(width, height, data); let mut app = Self { active_color: true, @@ -668,7 +663,7 @@ impl<'ctx> AppState<'ctx> { let image = self.export().encode().unwrap(); let mut file = File::create(file_name).map_err(AppError::File)?; file.write_all(&image[..]).map_err(AppError::File)?; - return Ok(()); + Ok(()) } pub fn run(&mut self) { @@ -799,21 +794,21 @@ impl<'ctx> AppState<'ctx> { start, extend, }) => { - if start.is_none() { + if let Some(s) = start { + if let Ok(o) = self.paint_line(s, pt, val, size) { + self.current_operation.extend(o); + self.brush = Brush::Line(LineBrush { + size, + start: if extend { contact } else { None }, + extend, + }); + } + } else { self.brush = Brush::Line(LineBrush { size, start: contact, extend, }); - } else if let Ok(o) = - self.paint_line(start.unwrap(), pt, val, size) - { - self.current_operation.extend(o); - self.brush = Brush::Line(LineBrush { - size, - start: if extend { contact } else { None }, - extend, - }); } } Brush::Fill => { @@ -827,7 +822,7 @@ impl<'ctx> AppState<'ctx> { for o in operation.iter() { // this `set` is unchecked because the returned // value of flood_fill is checked to be within pixmap - self.pixmap.set(o.clone(), replacement); + self.pixmap.set(*o, replacement); } self.current_operation.extend( operation -- cgit v1.2.3