diff options
author | Jovansonlee Cesar <ivanceras@gmail.com> | 2022-09-23 17:28:05 +0800 |
---|---|---|
committer | Jovansonlee Cesar <ivanceras@gmail.com> | 2022-09-23 17:28:05 +0800 |
commit | 850c00f83bc7cd2e53239e5d97eb404e6afad553 (patch) | |
tree | f533d5571d6d4f5de5bc736c48c925051757513c | |
parent | e02dd111dd849e7af24aaec38fd506256ef2cba3 (diff) |
refactor: more clippy fix warnings
-rw-r--r-- | TODO.md | 19 | ||||
-rw-r--r-- | packages/svgbob/src/buffer/cell_buffer.rs | 45 | ||||
-rw-r--r-- | packages/svgbob/src/buffer/cell_buffer/cell.rs | 8 | ||||
-rw-r--r-- | packages/svgbob/src/buffer/cell_buffer/span.rs | 6 | ||||
-rw-r--r-- | packages/svgbob/src/buffer/fragment_buffer/direction.rs | 37 | ||||
-rw-r--r-- | packages/svgbob/src/buffer/fragment_buffer/fragment/arc.rs | 38 | ||||
-rw-r--r-- | packages/svgbob/src/buffer/fragment_buffer/fragment/circle.rs | 36 | ||||
-rw-r--r-- | packages/svgbob/src/buffer/fragment_buffer/fragment/line.rs | 26 | ||||
-rw-r--r-- | packages/svgbob/src/buffer/fragment_buffer/fragment/marker_line.rs | 19 | ||||
-rw-r--r-- | packages/svgbob/src/buffer/fragment_buffer/fragment/polygon.rs | 24 |
10 files changed, 102 insertions, 156 deletions
@@ -12,23 +12,26 @@ - [x] Support for geometric shapes https://en.wikipedia.org/wiki/Geometric_Shapes - [x] Add enhance circle, detect circles then enhance it. - [ ] Add test cases -- [ ] Re-implement the escape string with double quotes. +- [X] Re-implement the escape string with double quotes. - [ ] Use perfect hashmap [phf](https://crates.io/crates/phf) to efficiently build the maps(unicode_map, circle_map, ascii_map) at compile time. - [ ] Fix the double arrow issue When there is 2 arrows in the middle of a line `---->>-------` or `----<<----` -- [ ] Clean the project enforce deny warnings. -- [ ] Fix a bug where an escaped text has whitespaces, the whitespaces are gone. +- [X] Clean the project enforce deny warnings. +- [~] Fix a bug where an escaped text has whitespaces, the whitespaces are gone. + - ~~This is a rendering bug in the browser where spaces are compressed into narrow widths~~ - [X] Revise calculation of Circle and Arc center by basing on the number of chars/width - [ ] Add more circle art, dynamically created + - use `ito-canvas` - [ ] Enhance quarter arc to be able to merge 2 or 3 quarters to form bigger arcs - [ ] Support for pills, elongated ovals - [X] Update to library to latest version - nalgebra - ncollide2d -> parry2d -- [ ] Make the top-level directory a workspace and put svgbob and cli into packages/ -- [ ] Make a trait for the merging algorithmns - - [ ] merge_recursive - - [ ] first_pass_merge - - [ ] second_pass_merge +- [X] Make the top-level directory a workspace and put svgbob and cli into packages/ +- [X] Make a trait for the merging algorithmns + - [X] merge_recursive + - [X] first_pass_merge + - [X] second_pass_merge - [ ] Try again to endorse the span of the grouped fragments that are in the same span but not reduces into a single shape +- [ ] Move the modules into flat structure as possible rather than deep diff --git a/packages/svgbob/src/buffer/cell_buffer.rs b/packages/svgbob/src/buffer/cell_buffer.rs index c9d4444..5d3f57f 100644 --- a/packages/svgbob/src/buffer/cell_buffer.rs +++ b/packages/svgbob/src/buffer/cell_buffer.rs @@ -30,7 +30,7 @@ mod span; /// The simplest buffer. /// This is maps which char belong to which cell skipping the whitespaces -#[derive(Debug)] +#[derive(Debug, Default)] pub struct CellBuffer { map: BTreeMap<Cell, char>, /// class, <style> @@ -59,11 +59,7 @@ impl DerefMut for CellBuffer { impl CellBuffer { pub fn new() -> Self { - CellBuffer { - map: BTreeMap::new(), - css_styles: vec![], - escaped_text: vec![], - } + Self::default() } pub fn add_css_styles(&mut self, css_styles: Vec<(String, String)>) { @@ -95,7 +91,7 @@ impl CellBuffer { /// return the group of contacting fragments pub fn group_contacts(&self) -> (Vec<Span>, Vec<Contacts>) { - let groups: Vec<(Vec<Span>, Vec<Contacts>)> = self + let (spans, contacts): (Vec<Vec<Span>>, Vec<Vec<Contacts>>) = self .group_adjacents() .into_iter() .map(|span| { @@ -106,10 +102,7 @@ impl CellBuffer { (vec![], contacts) } }) - .collect(); - - let (spans, contacts): (Vec<Vec<Span>>, Vec<Vec<Contacts>>) = - groups.into_iter().unzip(); + .unzip(); let spans: Vec<Span> = spans.into_iter().flatten().collect(); let contacts: Vec<Contacts> = contacts.into_iter().flatten().collect(); @@ -151,7 +144,7 @@ impl CellBuffer { &self, settings: &Settings, ) -> (Node<MSG>, f32, f32) { - let (w, h) = self.get_size(&settings); + let (w, h) = self.get_size(settings); let (group_nodes, fragments) = self.group_nodes_and_fragments(settings); @@ -226,24 +219,12 @@ impl CellBuffer { let single_member_fragments: Vec<FragmentSpan> = single_member .into_iter() - .flat_map(|contact| { - contact - .as_ref() - .into_iter() - .map(|frag| frag.clone()) - .collect::<Vec<FragmentSpan>>() - }) + .flat_map(|contact| contact.as_ref().to_vec()) .collect(); let vec_groups: Vec<Vec<FragmentSpan>> = vec_groups .into_iter() - .map(|contact| { - contact - .as_ref() - .into_iter() - .map(|frag| frag.clone()) - .collect::<Vec<FragmentSpan>>() - }) + .map(|contact| contact.as_ref().to_vec()) .collect(); let endorsed_fragments: Vec<FragmentSpan> = @@ -329,7 +310,7 @@ impl CellBuffer { let element_styles = sauron::jss! { "line, path, circle, rect, polygon": { stroke: stroke_color.clone(), - stroke_width: stroke_width.clone(), + stroke_width: stroke_width, stroke_opacity: 1, fill_opacity: 1, stroke_linecap: "round", @@ -339,7 +320,7 @@ impl CellBuffer { "text": { /* This fix the spacing bug in svg text*/ white_space: "pre", - fill: stroke_color.clone(), + fill: stroke_color, }, "rect.backdrop":{ @@ -352,7 +333,7 @@ impl CellBuffer { }, ".filled":{ - fill: fill_color.clone(), + fill: fill_color, }, ".bg_filled":{ @@ -360,12 +341,12 @@ impl CellBuffer { }, ".nofill":{ - fill: background.clone(), + fill: background, }, "text": { - font_family: font_family.clone(), - font_size: px(font_size.clone()), + font_family: font_family, + font_size: px(font_size), }, ".end_marked_arrow":{ diff --git a/packages/svgbob/src/buffer/cell_buffer/cell.rs b/packages/svgbob/src/buffer/cell_buffer/cell.rs index c7c779f..11388f7 100644 --- a/packages/svgbob/src/buffer/cell_buffer/cell.rs +++ b/packages/svgbob/src/buffer/cell_buffer/cell.rs @@ -29,7 +29,7 @@ pub use cell_grid::CellGrid; /// A single element in the terminal that /// can fit 1 character. /// Describe the exact location of a point/subcell in a grid. -#[derive(Debug, PartialEq, Hash, PartialOrd, Clone, Copy)] +#[derive(Debug, PartialEq, Hash, Clone, Copy, Eq)] pub struct Cell { pub x: i32, pub y: i32, @@ -41,7 +41,11 @@ impl fmt::Display for Cell { } } -impl Eq for Cell {} +impl PartialOrd for Cell { + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + Some(self.cmp(other)) + } +} impl Ord for Cell { fn cmp(&self, other: &Self) -> Ordering { self.y.cmp(&other.y).then(self.x.cmp(&other.x)) diff --git a/packages/svgbob/src/buffer/cell_buffer/span.rs b/packages/svgbob/src/buffer/cell_buffer/span.rs index 9c2856e..109a9fa 100644 --- a/packages/svgbob/src/buffer/cell_buffer/span.rs +++ b/packages/svgbob/src/buffer/cell_buffer/span.rs @@ -284,10 +284,10 @@ impl Bounds { } /// create a property buffer for all the cells of this span -impl<'p> Into<PropertyBuffer<'p>> for Span { - fn into(self) -> PropertyBuffer<'p> { +impl<'p> From<Span> for PropertyBuffer<'p> { + fn from(span: Span) -> Self { let mut pb = PropertyBuffer::new(); - for (cell, ch) in self.iter() { + for (cell, ch) in span.iter() { if let Some(property) = Property::from_char(*ch) { pb.as_mut().insert(*cell, property); } diff --git a/packages/svgbob/src/buffer/fragment_buffer/direction.rs b/packages/svgbob/src/buffer/fragment_buffer/direction.rs index 4325db1..870ec53 100644 --- a/packages/svgbob/src/buffer/fragment_buffer/direction.rs +++ b/packages/svgbob/src/buffer/fragment_buffer/direction.rs @@ -1,6 +1,6 @@ use crate::buffer::CellGrid; -#[derive(Debug, Clone, PartialEq, Copy)] +#[derive(Debug, Clone, PartialEq, Eq, Copy)] pub enum Direction { TopLeft, Top, @@ -26,41 +26,6 @@ impl Direction { Direction::BottomRight => Direction::TopLeft, } } - /* - pub(crate) fn any_along_side(&self, tags: &[PolygonTag]) -> bool { - tags.iter().any(|tag| self.is_along_side(tag)) - } - /// diamon matches alongside for everything. - pub(crate) fn is_along_side(&self, tag: &PolygonTag) -> bool { - if *tag == PolygonTag::DiamondBullet { - return true; - } - match self { - Direction::TopLeft | Direction::BottomRight => match tag { - PolygonTag::ArrowTopLeft - | PolygonTag::ArrowBottomRight - | PolygonTag::ArrowTop - | PolygonTag::ArrowBottom => true, - _ => false, - }, - Direction::Top | Direction::Bottom => match tag { - PolygonTag::ArrowTop | PolygonTag::ArrowBottom => true, - _ => false, - }, - Direction::TopRight | Direction::BottomLeft => match tag { - PolygonTag::ArrowTopRight - | PolygonTag::ArrowBottomLeft - | PolygonTag::ArrowTop - | PolygonTag::ArrowBottom => true, - _ => false, - }, - Direction::Left | Direction::Right => match tag { - PolygonTag::ArrowLeft | PolygonTag::ArrowRight => true, - _ => false, - }, - } - } - */ /// calculate the threshold length which is the basis /// if the arrow and the line is connected diff --git a/packages/svgbob/src/buffer/fragment_buffer/fragment/arc.rs b/packages/svgbob/src/buffer/fragment_buffer/fragment/arc.rs index dd9c81b..2f09b3b 100644 --- a/packages/svgbob/src/buffer/fragment_buffer/fragment/arc.rs +++ b/packages/svgbob/src/buffer/fragment_buffer/fragment/arc.rs @@ -20,7 +20,7 @@ pub struct Arc { impl Arc { /// create an arc from start to end with a radius /// direction is counter clock wise - pub(in crate) fn new(start: Point, end: Point, radius: f32) -> Self { + pub(crate) fn new(start: Point, end: Point, radius: f32) -> Self { let mut arc = Arc { start, end, @@ -36,14 +36,14 @@ impl Arc { /// check if this arcs to point a, b /// disregarding radius - pub(in crate) fn arcs_to(&self, a: Point, b: Point) -> bool { + pub(crate) fn arcs_to(&self, a: Point, b: Point) -> bool { let arc = Arc::new(a, b, 1.0); self.start == arc.start && self.end == arc.end && self.sweep_flag == arc.sweep_flag } - pub(in crate) fn new_with_sweep( + pub(crate) fn new_with_sweep( start: Point, end: Point, radius: f32, @@ -62,7 +62,7 @@ impl Arc { arc } - pub(in crate) fn absolute_position(&self, cell: Cell) -> Self { + pub(crate) fn absolute_position(&self, cell: Cell) -> Self { Arc { start: cell.absolute_position(self.start), end: cell.absolute_position(self.end), @@ -72,11 +72,9 @@ impl Arc { /// reverse the order of points and also set the flag to true, to /// make the rotation clockwise - pub(in crate) fn sort_reorder_end_points(&mut self) { + pub(crate) fn sort_reorder_end_points(&mut self) { if self.start > self.end { - let tmp_start = self.start; - self.start = self.end; - self.end = tmp_start; + std::mem::swap(&mut self.start, &mut self.end); self.sweep_flag = !self.sweep_flag; } } @@ -91,7 +89,7 @@ impl Arc { } /// check to see of this arc is touching the other arc - pub(in crate) fn is_touching(&self, other: &Self) -> bool { + pub(crate) fn is_touching(&self, other: &Self) -> bool { self.start == other.start || self.end == other.end || self.start == other.end @@ -158,19 +156,19 @@ impl fmt::Display for Arc { } } -impl<MSG> Into<Node<MSG>> for Arc { - fn into(self) -> Node<MSG> { +impl<MSG> From<Arc> for Node<MSG> { + fn from(arc: Arc) -> Node<MSG> { let dv = format!( "M {},{} A {},{} {},{},{} {},{}", - self.start.x, - self.start.y, - self.radius, - self.radius, - self.rotation_flag as u8, - self.major_flag as u8, - self.sweep_flag as u8, - self.end.x, - self.end.y + arc.start.x, + arc.start.y, + arc.radius, + arc.radius, + arc.rotation_flag as u8, + arc.major_flag as u8, + arc.sweep_flag as u8, + arc.end.x, + arc.end.y ); path(vec![d(dv), class("nofill")], vec![]) } diff --git a/packages/svgbob/src/buffer/fragment_buffer/fragment/circle.rs b/packages/svgbob/src/buffer/fragment_buffer/fragment/circle.rs index b49c38c..1cf22f9 100644 --- a/packages/svgbob/src/buffer/fragment_buffer/fragment/circle.rs +++ b/packages/svgbob/src/buffer/fragment_buffer/fragment/circle.rs @@ -18,7 +18,7 @@ pub struct Circle { } impl Circle { - pub(in crate) fn new(center: Point, radius: f32, is_filled: bool) -> Self { + pub(crate) fn new(center: Point, radius: f32, is_filled: bool) -> Self { Circle { center, radius, @@ -44,7 +44,7 @@ impl Circle { } /// offset the circles parameter from the arg cell - pub(in crate) fn absolute_position(&self, cell: Cell) -> Self { + pub(crate) fn absolute_position(&self, cell: Cell) -> Self { Circle { center: cell.absolute_position(self.center), ..*self @@ -72,16 +72,16 @@ impl fmt::Display for Circle { } } -impl<MSG> Into<Node<MSG>> for Circle { - fn into(self) -> Node<MSG> { +impl<MSG> From<Circle> for Node<MSG> { + fn from(c: Circle) -> Node<MSG> { circle( [ - cx(self.center.x), - cy(self.center.y), - r(self.radius), + cx(c.center.x), + cy(c.center.y), + r(c.radius), classes_flag([ - ("filled", self.is_filled), - ("nofill", !self.is_filled), + ("filled", c.is_filled), + ("nofill", !c.is_filled), ]), ], [], @@ -115,22 +115,22 @@ impl PartialEq for Circle { } } -impl Into<Polyline> for Circle { - fn into(self) -> Polyline { - let points: Vec<Point2<f32>> = extract_circle_points(self.radius, 64) +impl From<Circle> for Polyline { + fn from(c: Circle) -> Polyline { + let points: Vec<Point2<f32>> = extract_circle_points(c.radius, 64) .into_iter() - .map(|p| Point2::new(p.x + self.center.x, p.y + self.center.y)) + .map(|p| Point2::new(p.x + c.center.x, p.y + c.center.y)) .collect(); Polyline::new(points, None) } } -impl Into<ConvexPolygon> for Circle { - fn into(self) -> ConvexPolygon { - let points: Vec<Point2<f32>> = extract_circle_points(self.radius, 64) +impl From<Circle> for ConvexPolygon { + fn from(c: Circle) -> ConvexPolygon { + let points: Vec<Point2<f32>> = extract_circle_points(c.radius, 64) .into_iter() - .map(|p| Point2::new(p.x + self.center.x, p.y + self.center.y)) + .map(|p| Point2::new(p.x + c.center.x, p.y + c.center.y)) .collect(); ConvexPolygon::from_convex_polyline(points) @@ -155,7 +155,7 @@ fn push_xy_arc(radius: f32, nsubdiv: u32, dtheta: f32) -> Vec<Point> { let y = curr_theta.sin() * radius; out.push(Point::new(x, y)); - curr_theta = curr_theta + dtheta; + curr_theta += dtheta; } out } diff --git a/packages/svgbob/src/buffer/fragment_buffer/fragment/line.rs b/packages/svgbob/src/buffer/fragment_buffer/fragment/line.rs index d98681a..b7d2337 100644 --- a/packages/svgbob/src/buffer/fragment_buffer/fragment/line.rs +++ b/packages/svgbob/src/buffer/fragment_buffer/fragment/line.rs @@ -56,9 +56,7 @@ impl Line { } fn swap(&mut self) { - let tmp_start = self.start; - self.start = self.end; - self.end = tmp_start; + std::mem::swap(&mut self.start, &mut self.end); } /// does this line can completely cover line a b? @@ -511,17 +509,17 @@ impl fmt::Display for Line { } } -impl<MSG> Into<Node<MSG>> for Line { - fn into(self) -> Node<MSG> { +impl<MSG> From<Line> for Node<MSG> { + fn from(line: Line) -> Node<MSG> { svg::line( [ - x1(self.start.x), - y1(self.start.y), - x2(self.end.x), - y2(self.end.y), + x1(line.start.x), + y1(line.start.y), + x2(line.end.x), + y2(line.end.y), classes_flag([ - ("broken", self.is_broken), - ("solid", !self.is_broken), + ("broken", line.is_broken), + ("solid", !line.is_broken), ]), ], [], @@ -529,9 +527,9 @@ impl<MSG> Into<Node<MSG>> for Line { } } -impl Into<Segment> for Line { - fn into(self) -> Segment { - Segment::new(*self.start, *self.end) +impl From<Line> for Segment { + fn from(line: Line) -> Segment { + Segment::new(*line.start, *line.end) } } diff --git a/packages/svgbob/src/buffer/fragment_buffer/fragment/marker_line.rs b/packages/svgbob/src/buffer/fragment_buffer/fragment/marker_line.rs index e52f965..183cad0 100644 --- a/packages/svgbob/src/buffer/fragment_buffer/fragment/marker_line.rs +++ b/packages/svgbob/src/buffer/fragment_buffer/fragment/marker_line.rs @@ -5,7 +5,7 @@ use crate::{ use sauron::{html::attributes::class, Node}; use std::fmt; -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum Marker { // --> Arrow, @@ -27,14 +27,11 @@ pub enum Marker { impl Marker { fn is_arrow(&self) -> bool { - match self { - Marker::Arrow | Marker::ClearArrow => true, - _ => false, - } + matches!(self, Marker::Arrow | Marker::ClearArrow) } } -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct MarkerLine { pub line: Line, pub start_marker: Option<Marker>, @@ -167,14 +164,14 @@ impl fmt::Display for MarkerLine { } } -impl<MSG> Into<Node<MSG>> for MarkerLine { - fn into(self) -> Node<MSG> { - let node: Node<MSG> = self.line.into(); +impl<MSG> From<MarkerLine> for Node<MSG> { + fn from(ml: MarkerLine) -> Node<MSG> { + let node: Node<MSG> = ml.line.into(); let mut classes = vec![]; - if let Some(start_marker) = self.start_marker { + if let Some(start_marker) = ml.start_marker { classes.push(class(format!("start_marked_{}", start_marker))); } - if let Some(end_marker) = self.end_marker { + if let Some(end_marker) = ml.end_marker { classes.push(class(format!("end_marked_{}", end_marker))); } node.add_attributes(classes) diff --git a/packages/svgbob/src/buffer/fragment_buffer/fragment/polygon.rs b/packages/svgbob/src/buffer/fragment_buffer/fragment/polygon.rs index 6ec42b9..3516b9c 100644 --- a/packages/svgbob/src/buffer/fragment_buffer/fragment/polygon.rs +++ b/packages/svgbob/src/buffer/fragment_buffer/fragment/polygon.rs @@ -12,7 +12,7 @@ use sauron::{ }; use std::{cmp::Ordering, fmt}; -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum PolygonTag { // ^ // \ @@ -90,7 +90,7 @@ impl PolygonTag { } impl Polygon { - pub(in crate) fn new( + pub(crate) fn new( points: Vec<Point>, is_filled: bool, tags: Vec<PolygonTag>, @@ -102,7 +102,7 @@ impl Polygon { } } - pub(in crate) fn absolute_position(&self, cell: Cell) -> Self { + pub(crate) fn absolute_position(&self, cell: Cell) -> Self { let points: Vec<Point> = self .points .iter() @@ -147,7 +147,7 @@ impl Polygon { } } - pub(in crate) fn scale(&self, scale: f32) -> Self { + pub(crate) fn scale(&self, scale: f32) -> Self { let points: Vec<Point> = self.points.iter().map(|p| p.scale(scale)).collect(); Polygon { @@ -195,28 +195,28 @@ impl fmt::Display for Polygon { } } -impl Into<Polyline> for Polygon { - fn into(self) -> Polyline { +impl From<Polygon> for Polyline { + fn from(polygon: Polygon) -> Polyline { let points: Vec<Point2<f32>> = - self.points.iter().map(|p| **p).collect(); + polygon.points.iter().map(|p| **p).collect(); Polyline::new(points, None) } } -impl<MSG> Into<Node<MSG>> for Polygon { - fn into(self) -> Node<MSG> { +impl<MSG> From<Polygon> for Node<MSG> { + fn from(pl: Polygon) -> Node<MSG> { polygon( [ points( - self.points + pl.points .iter() .map(|p| format!("{},{}", p.x, p.y)) .collect::<Vec<String>>() .join(" "), ), classes_flag([ - ("filled", self.is_filled), - ("nofill", !self.is_filled), + ("filled", pl.is_filled), + ("nofill", !pl.is_filled), ]), ], [], |