Skip to content

Commit

Permalink
add linting options
Browse files Browse the repository at this point in the history
  • Loading branch information
bircni committed Sep 23, 2024
1 parent 2005e8f commit a4ad47a
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 66 deletions.
21 changes: 21 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,24 @@ serde = ["dep:serde", "egui/serde", "petgraph/serde", "petgraph/serde-1"]

[workspace]
members = ["examples/*"]

[lints.rust]
unsafe_code = "forbid"

[lints.clippy]
pedantic = { level = "deny", priority = 0 }
enum_glob_use = { level = "deny", priority = 1 }
perf = { level = "deny", priority = 2 }
style = { level = "deny", priority = 3 }
# unwrap_used = { level = "deny", priority = 4 } These should enabled in the future
# expect_used = { level = "deny", priority = 5 }
module_name_repetitions = { level = "allow", priority = 6 }
cast_precision_loss = { level = "allow", priority = 7 }
float_cmp = { level = "allow", priority = 8 }
cast_possible_truncation = { level = "allow", priority = 9 }
cast_sign_loss = { level = "allow", priority = 10 }
out_of_bounds_indexing = { level = "allow", priority = 11 }

must_use_candidate = { level = "allow", priority = 12 }
struct_excessive_bools = { level = "allow", priority = 13 }
return_self_not_must_use = { level = "allow", priority = 14 }
6 changes: 3 additions & 3 deletions src/draw/displays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ where
/// Use `ctx.painter` to have low level access to egui painting process.
fn shapes(&mut self, ctx: &DrawContext) -> Vec<Shape>;

/// Is called on every frame. Can be used for updating state of the implementation of [DisplayNode]
/// Is called on every frame. Can be used for updating state of the implementation of [`DisplayNode`]
fn update(&mut self, state: &NodeProps<N>);

/// Checks if the provided `pos` is inside the shape.
Expand All @@ -45,7 +45,7 @@ where
Ix: IndexType,
D: DisplayNode<N, E, Ty, Ix>,
{
/// Draws shapes of the edge. Uses [DisplayNode] implementation from node endpoints to get start and end coordinates using [closest_boundary_point](DisplayNode::closest_boundary_point).
/// Draws shapes of the edge. Uses [`DisplayNode`] implementation from node endpoints to get start and end coordinates using [`closest_boundary_point`](DisplayNode::closest_boundary_point).
/// If the node is interacted these shapes will be used for drawing on foreground layer, otherwise on background layer.
/// Has mutable reference to itself for possibility to change internal state for the visualizations where this is important.
///
Expand All @@ -61,7 +61,7 @@ where
ctx: &DrawContext,
) -> Vec<Shape>;

/// Is called on every frame. Can be used for updating state of the implementation of [DisplayNode]
/// Is called on every frame. Can be used for updating state of the implementation of [`DisplayNode`]
fn update(&mut self, state: &EdgeProps<E>);

/// Checks if the provided `pos` is inside the shape.
Expand Down
26 changes: 13 additions & 13 deletions src/draw/displays_default/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType, D: DisplayNode<N, E, Ty, I
self.is_inside_curve(start, end, pos)
}

#[allow(clippy::too_many_lines)] // TODO: refactor
fn shapes(
&mut self,
start: &Node<N, E, Ty, Ix, D>,
Expand All @@ -73,9 +74,10 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType, D: DisplayNode<N, E, Ty, I

let label_visible = ctx.style.labels_always || self.selected;

let style = match self.selected {
true => ctx.ctx.style().visuals.widgets.active,
false => ctx.ctx.style().visuals.widgets.inactive,
let style = if self.selected {
ctx.ctx.style().visuals.widgets.active
} else {
ctx.ctx.style().visuals.widgets.inactive
};
let color = style.fg_stroke.color;
let stroke = Stroke::new(self.width, color);
Expand All @@ -90,9 +92,8 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType, D: DisplayNode<N, E, Ty, I
let line_looped_shape = line_looped_shapes.clone().pop().unwrap();
res.push(line_looped_shape);

let line_looped = match line_looped_shapes.pop().unwrap() {
Shape::CubicBezier(cubic) => cubic,
_ => panic!("invalid shape type"),
let Shape::CubicBezier(line_looped) = line_looped_shapes.pop().unwrap() else {
panic!("invalid shape type")
};

// TODO: export to func
Expand Down Expand Up @@ -181,9 +182,8 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType, D: DisplayNode<N, E, Ty, I
builder = builder.with_tip(&tip_props);
};
let curved_shapes = builder.build();
let line_curved = match curved_shapes.first() {
Some(Shape::CubicBezier(curve)) => curve,
_ => panic!("invalid shape type"),
let Some(Shape::CubicBezier(line_curved)) = curved_shapes.first() else {
panic!("invalid shape type")
};
res.extend(curved_shapes.clone());

Expand Down Expand Up @@ -237,7 +237,7 @@ impl DefaultEdgeShape {
.build();

match shape.first() {
Some(Shape::CubicBezier(cubic)) => is_point_on_curve(pos, cubic.clone()),
Some(Shape::CubicBezier(cubic)) => is_point_on_curve(pos, cubic),
_ => panic!("invalid shape type"),
}
}
Expand Down Expand Up @@ -271,7 +271,7 @@ impl DefaultEdgeShape {
_ => panic!("invalid shape type"),
};

is_point_on_curve(pos, curved_shape)
is_point_on_curve(pos, &curved_shape)
}
}

Expand All @@ -287,7 +287,7 @@ fn node_size<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType, D: DisplayNode<N,
}

/// Returns the distance from line segment `a``b` to point `c`.
/// Adapted from https://stackoverflow.com/questions/1073336/circle-line-segment-collision-detection-algorithm
/// Adapted from <https://stackoverflow.com/questions/1073336/circle-line-segment-collision-detection-algorithm>
fn distance_segment_to_point(a: Pos2, b: Pos2, point: Pos2) -> f32 {
let ac = point - a;
let ab = b - a;
Expand Down Expand Up @@ -322,7 +322,7 @@ fn proj(a: Vec2, b: Vec2) -> Vec2 {
Vec2::new(k * b.x, k * b.y)
}

fn is_point_on_curve(point: Pos2, curve: CubicBezierShape) -> bool {
fn is_point_on_curve(point: Pos2, curve: &CubicBezierShape) -> bool {
for p in curve.flatten(None) {
if p.distance(point) < curve.stroke.width {
return true;
Expand Down
7 changes: 4 additions & 3 deletions src/draw/displays_default/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType> DisplayNode<N, E, Ty, Ix>

let is_interacted = self.selected || self.dragged;

let style = match is_interacted {
true => ctx.ctx.style().visuals.widgets.active,
false => ctx.ctx.style().visuals.widgets.inactive,
let style = if is_interacted {
ctx.ctx.style().visuals.widgets.active
} else {
ctx.ctx.style().visuals.widgets.inactive
};
let color = style.fg_stroke.color;

Expand Down
16 changes: 8 additions & 8 deletions src/draw/drawer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ where
let shapes = display.shapes(self.ctx);

if n.selected() || n.dragged() {
shapes.into_iter().for_each(|s| {
for s in shapes {
self.postponed.push(s);
});
}
} else {
shapes.into_iter().for_each(|s| {
for s in shapes {
self.ctx.painter.add(s);
});
}
}
});
}
Expand All @@ -109,13 +109,13 @@ where
let shapes = display.shapes(&start, &end, self.ctx);

if e.selected() {
shapes.into_iter().for_each(|s| {
for s in shapes {
self.postponed.push(s);
});
}
} else {
shapes.into_iter().for_each(|s| {
for s in shapes {
self.ctx.painter.add(s);
});
}
}
});
}
Expand Down
7 changes: 4 additions & 3 deletions src/elements/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ impl<
props,
display,

id: Default::default(),
_marker: Default::default(),
id: Option::default(),
_marker: PhantomData,
}
}

Expand All @@ -82,6 +82,7 @@ impl<
&mut self.display
}

#[allow(clippy::missing_panics_doc)] // TODO: Add panic message
pub fn id(&self) -> EdgeIndex<Ix> {
self.id.unwrap()
}
Expand Down Expand Up @@ -111,7 +112,7 @@ impl<
}

pub fn set_label(&mut self, label: String) {
self.props.label = label
self.props.label = label;
}

pub fn with_label(mut self, label: String) -> Self {
Expand Down
10 changes: 6 additions & 4 deletions src/elements/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ where
_marker: PhantomData<(E, Ty)>,
}

#[allow(clippy::missing_fields_in_debug)] // TODO: add all fields or remove this and fix all warnings
impl<N, E, Ty, Ix, D> Debug for Node<N, E, Ty, Ix, D>
where
N: Clone,
Expand Down Expand Up @@ -97,8 +98,8 @@ where
props,
display,

id: Default::default(),
_marker: Default::default(),
id: Option::default(),
_marker: PhantomData,
}
}

Expand All @@ -120,6 +121,7 @@ where
self.props.location = location;
}

#[allow(clippy::missing_panics_doc)] // TODO: Add panic message
pub fn id(&self) -> NodeIndex<Ix> {
self.id.unwrap()
}
Expand All @@ -137,7 +139,7 @@ where
}

pub fn set_location(&mut self, loc: Pos2) {
self.props.location = loc
self.props.location = loc;
}

pub fn selected(&self) -> bool {
Expand All @@ -161,7 +163,7 @@ where
}

pub fn set_label(&mut self, label: String) {
self.props.label = label
self.props.label = label;
}

pub fn with_label(mut self, label: String) -> Self {
Expand Down
16 changes: 11 additions & 5 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ impl<
}

/// Finds edge by position.
#[allow(clippy::missing_panics_doc)] // TODO: add panics doc
pub fn edge_by_screen_pos(&self, meta: &Metadata, screen_pos: Pos2) -> Option<EdgeIndex<Ix>> {
let pos_in_graph = meta.screen_to_canvas_pos(screen_pos);
for (idx, e) in self.edges_iter() {
let (idx_start, idx_end) = match self.g.edge_endpoints(e.id()) {
Some(se) => se,
None => continue,
let Some((idx_start, idx_end)) = self.g.edge_endpoints(e.id()) else {
continue;
};
let start = self.g.node_weight(idx_start).unwrap();
let end = self.g.node_weight(idx_end).unwrap();
Expand All @@ -101,6 +101,7 @@ impl<
}

/// Adds node to graph setting default location and default label values
#[allow(clippy::missing_panics_doc)] // TODO: add panics doc
pub fn add_node(&mut self, payload: N) -> NodeIndex<Ix> {
let node = Node::new(payload);

Expand All @@ -114,6 +115,7 @@ impl<
}

/// Adds node to graph setting custom location and default label value
#[allow(clippy::missing_panics_doc)] // TODO: add panics doc
pub fn add_node_with_location(&mut self, payload: N, location: Pos2) -> NodeIndex<Ix> {
let node = Node::new(payload);

Expand All @@ -132,6 +134,7 @@ impl<
}

/// Adds node to graph setting custom location and custom label value
#[allow(clippy::missing_panics_doc)] // TODO: add panics doc
pub fn add_node_with_label_and_location(
&mut self,
payload: N,
Expand Down Expand Up @@ -162,6 +165,7 @@ impl<
}

/// Removes all edges between start and end node. Returns removed edges count.
#[allow(clippy::missing_panics_doc)] // TODO: add panics doc
pub fn remove_edges_between(&mut self, start: NodeIndex<Ix>, end: NodeIndex<Ix>) -> usize {
let idxs = self
.g
Expand All @@ -173,15 +177,16 @@ impl<
}

let mut removed = 0;
idxs.iter().for_each(|e| {
for e in &idxs {
self.g.remove_edge(*e).unwrap();
removed += 1;
});
}

removed
}

/// Adds edge between start and end node with default label.
#[allow(clippy::missing_panics_doc)] // TODO: add panics doc
pub fn add_edge(
&mut self,
start: NodeIndex<Ix>,
Expand All @@ -200,6 +205,7 @@ impl<
}

/// Adds edge between start and end node with custom label setting correct order.
#[allow(clippy::missing_panics_doc)] // TODO: add panics doc
pub fn add_edge_with_label(
&mut self,
start: NodeIndex<Ix>,
Expand Down
Loading

0 comments on commit a4ad47a

Please sign in to comment.