Skip to content

Commit

Permalink
Add anyhow
Browse files Browse the repository at this point in the history
  • Loading branch information
bircni committed Sep 24, 2024
1 parent 0de97d3 commit ab48f76
Show file tree
Hide file tree
Showing 13 changed files with 284 additions and 156 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ petgraph = { version = "0.6", default-features = false, features = [
crossbeam = { version = "0.8", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
serde_json = { version = "1.0", optional = true }
anyhow = "1.0.89"

[features]
egui_persistence = ["dep:serde"]
Expand All @@ -37,7 +38,7 @@ 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
unwrap_used = { level = "deny", priority = 4 }
# expect_used = { level = "deny", priority = 5 }
module_name_repetitions = { level = "allow", priority = 6 }
cast_precision_loss = { level = "allow", priority = 7 }
Expand Down
1 change: 1 addition & 0 deletions examples/animated_nodes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ license = "MIT"
edition = "2021"

[dependencies]
anyhow = "1.0.89"
egui_graphs = { path = "../../" }
egui = "0.28"
eframe = "0.28"
Expand Down
12 changes: 7 additions & 5 deletions examples/animated_nodes/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Context;
use eframe::{run_native, App, CreationContext};
use egui::Context;
use egui_graphs::{
default_edge_transform, default_node_transform, to_graph_custom, DefaultEdgeShape, Graph,
GraphView, SettingsInteraction, SettingsNavigation,
Expand Down Expand Up @@ -33,13 +33,14 @@ impl AnimatedNodesApp {
}
},
default_edge_transform,
),
)
.expect("Failed to convert graph"),
}
}
}

impl App for AnimatedNodesApp {
fn update(&mut self, ctx: &Context, _: &mut eframe::Frame) {
fn update(&mut self, ctx: &egui::Context, _: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.add(
&mut GraphView::<_, _, _, _, NodeShapeAnimated, DefaultEdgeShape>::new(&mut self.g)
Expand Down Expand Up @@ -73,12 +74,13 @@ fn generate_graph() -> StableGraph<node::NodeData, ()> {
g
}

fn main() {
fn main() -> anyhow::Result<()> {
let native_options = eframe::NativeOptions::default();
run_native(
"egui_graphs_animated_nodes_demo",
native_options,
Box::new(|cc| Ok(Box::new(AnimatedNodesApp::new(cc)))),
)
.unwrap();
.map_err(|e| anyhow::anyhow!(e.to_string()))
.context("Failed to run native")
}
11 changes: 7 additions & 4 deletions examples/demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ impl DemoApp {
random_n.location().y + 10. + rng.gen_range(0. ..50.),
);

let g_idx = self.g.add_node_with_location((), location);
let g_idx = self
.g
.add_node_with_location((), location)
.expect("Could not add node");

let sim_node = egui_graphs::Node::new(());
let sim_node_loc = fdg::nalgebra::Point2::new(location.x, location.y);
Expand All @@ -195,7 +198,7 @@ impl DemoApp {
}

fn remove_node(&mut self, idx: NodeIndex) {
self.g.remove_node(idx);
let _ = self.g.remove_node(idx);

self.sim.remove_node(idx).unwrap();

Expand All @@ -211,7 +214,7 @@ impl DemoApp {
}

fn add_edge(&mut self, start: NodeIndex, end: NodeIndex) {
self.g.add_edge(start, end, ());
let _ = self.g.add_edge(start, end, ());

self.sim.add_edge(start, end, egui_graphs::Edge::new(()));
}
Expand Down Expand Up @@ -541,7 +544,7 @@ fn generate_random_graph(node_count: usize, edge_count: usize) -> Graph {
graph.add_edge(NodeIndex::new(source), NodeIndex::new(target), ());
}

to_graph(&graph)
to_graph(&graph).expect("Could not convert graph")
}

fn init_force(settings: &settings::SettingsSimulation) -> FruchtermanReingold<f32, 2> {
Expand Down
14 changes: 11 additions & 3 deletions src/draw/displays_default/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,13 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType, D: DisplayNode<N, E, Ty, I
.looped(start.location(), size, self.loop_size, self.order)
.with_scaler(ctx.meta)
.build();
let line_looped_shape = line_looped_shapes.clone().pop().unwrap();
let line_looped_shape = line_looped_shapes
.clone()
.pop()
.expect("invalid shape type");
res.push(line_looped_shape);

#[allow(clippy::unwrap_used)] // Its safe to unwrap here
let Shape::CubicBezier(line_looped) = line_looped_shapes.pop().unwrap() else {
panic!("invalid shape type")
};
Expand All @@ -107,7 +111,9 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType, D: DisplayNode<N, E, Ty, I
});

let flattened_curve = line_looped.flatten(None);
let median = *flattened_curve.get(flattened_curve.len() / 2).unwrap();
let median = *flattened_curve
.get(flattened_curve.len() / 2)
.expect("Could not get median point");

let label_width = galley.rect.width();
let label_height = galley.rect.height();
Expand Down Expand Up @@ -198,7 +204,9 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType, D: DisplayNode<N, E, Ty, I
});

let flattened_curve = line_curved.flatten(None);
let median = *flattened_curve.get(flattened_curve.len() / 2).unwrap();
let median = *flattened_curve
.get(flattened_curve.len() / 2)
.expect("Could not get median point");

let label_width = galley.rect.width();
let label_height = galley.rect.height();
Expand Down
8 changes: 6 additions & 2 deletions src/draw/displays_default/edge_shape_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ impl<'a> EdgeShapeBuilder<'a> {
let tip_start_2 = end - arrow_tip_dir_2;

// replace end of an edge with start of tip
*points_line.get_mut(1).unwrap() = end - tip_props.size * tip_dir;
*points_line
.get_mut(1)
.expect("Could not get end of the edge") = end - tip_props.size * tip_dir;

vec![end, tip_start_1, tip_start_2]
}
Expand Down Expand Up @@ -223,7 +225,9 @@ impl<'a> EdgeShapeBuilder<'a> {
let tip_start_2 = end - arrow_tip_dir_2;

// replace end of an edge with start of tip
*points_curve.get_mut(3).unwrap() = end - tip_props.size * tip_dir;
*points_curve
.get_mut(3)
.expect("Could not get end of the edge") = end - tip_props.size * tip_dir;

vec![end, tip_start_1, tip_start_2]
}
Expand Down
50 changes: 33 additions & 17 deletions src/draw/drawer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::marker::PhantomData;

use egui::{Context, Painter, Shape};
use anyhow::Context;
use egui::{Painter, Shape};
use petgraph::graph::IndexType;
use petgraph::EdgeType;

Expand All @@ -10,7 +11,7 @@ use super::{DisplayEdge, DisplayNode};

/// Contains all the data about current widget state which is needed for custom drawing functions.
pub struct DrawContext<'a> {
pub ctx: &'a Context,
pub ctx: &'a egui::Context,
pub painter: &'a Painter,
pub style: &'a SettingsStyle,
pub is_directed: bool,
Expand Down Expand Up @@ -50,10 +51,12 @@ where
}
}

pub fn draw(mut self) {
self.draw_edges();
self.draw_nodes();
pub fn draw(mut self) -> anyhow::Result<()> {
self.draw_edges()?;
self.draw_nodes()?;
self.draw_postponed();

Ok(())
}

fn draw_postponed(&mut self) {
Expand All @@ -62,14 +65,14 @@ where
});
}

fn draw_nodes(&mut self) {
fn draw_nodes(&mut self) -> anyhow::Result<()> {
self.g
.g
.node_indices()
.collect::<Vec<_>>()
.into_iter()
.for_each(|idx| {
let n = self.g.node_mut(idx).unwrap();
.try_for_each(|idx| -> anyhow::Result<()> {
let n = self.g.node_mut(idx)?;
let props = n.props().clone();

let display = n.display_mut();
Expand All @@ -85,23 +88,33 @@ where
self.ctx.painter.add(s);
}
}
});
Ok(())
})?;
Ok(())
}

fn draw_edges(&mut self) {
fn draw_edges(&mut self) -> anyhow::Result<()> {
self.g
.g
.edge_indices()
.collect::<Vec<_>>()
.into_iter()
.for_each(|idx| {
let (idx_start, idx_end) = self.g.edge_endpoints(idx).unwrap();
.try_for_each(|idx| -> anyhow::Result<()> {
let (idx_start, idx_end) = self.g.edge_endpoints(idx)?;

// FIXME: not a good decision to clone nodes for every edge
let start = self.g.node(idx_start).cloned().unwrap();
let end = self.g.node(idx_end).cloned().unwrap();

let e = self.g.edge_mut(idx).unwrap();
let start = self
.g
.node(idx_start)
.cloned()
.context("Start node not found")?;
let end = self
.g
.node(idx_end)
.cloned()
.context("End node not found")?;

let e = self.g.edge_mut(idx).context("Edge not found")?;
let props = e.props().clone();

let display = e.display_mut();
Expand All @@ -117,6 +130,9 @@ where
self.ctx.painter.add(s);
}
}
});

Ok(())
})?;
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/elements/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<

#[allow(clippy::missing_panics_doc)] // TODO: Add panic message
pub fn id(&self) -> EdgeIndex<Ix> {
self.id.unwrap()
self.id.expect("Edge is not bound to the graph")
}

pub fn order(&self) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion src/elements/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ where

#[allow(clippy::missing_panics_doc)] // TODO: Add panic message
pub fn id(&self) -> NodeIndex<Ix> {
self.id.unwrap()
self.id.expect("Node is not bound to the graph")
}

pub fn payload(&self) -> &N {
Expand Down
Loading

0 comments on commit ab48f76

Please sign in to comment.