summaryrefslogtreecommitdiff
path: root/rust/src/flowgraph/edge.rs
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2025-05-08 21:39:12 -0400
committerMason Reed <35282038+emesare@users.noreply.github.com>2025-05-12 17:45:24 -0400
commitadbd39ab7c3d4e67e8709af43176e60f31141ba8 (patch)
treec6c937ae1561d5b7cd8f2925ef44b5091c6a86dc /rust/src/flowgraph/edge.rs
parent38fa66115c00ead8dcc38ec5c5451d3ce4444ab8 (diff)
[Rust] Flow graph API improvements
- Fixed some misc bugs - Added real FlowGraphEdge type - Added accessors for node edges - Added interaction handler to the flow graph example to dump the flow graph to stdin - Split out the flow graph API into smaller files
Diffstat (limited to 'rust/src/flowgraph/edge.rs')
-rw-r--r--rust/src/flowgraph/edge.rs113
1 files changed, 113 insertions, 0 deletions
diff --git a/rust/src/flowgraph/edge.rs b/rust/src/flowgraph/edge.rs
new file mode 100644
index 00000000..7862f156
--- /dev/null
+++ b/rust/src/flowgraph/edge.rs
@@ -0,0 +1,113 @@
+use binaryninjacore_sys::*;
+
+use crate::flowgraph::node::FlowGraphNode;
+use crate::flowgraph::{BranchType, EdgePenStyle, ThemeColor};
+use crate::rc::{CoreArrayProvider, CoreArrayProviderInner, Ref};
+
+#[derive(Clone, Debug, PartialEq)]
+pub struct FlowGraphEdge {
+ pub branch_type: BranchType,
+ pub target: Ref<FlowGraphNode>,
+ pub points: Vec<Point>,
+ pub back_edge: bool,
+ pub style: EdgeStyle,
+}
+
+impl FlowGraphEdge {
+ pub fn from_raw(value: &BNFlowGraphEdge) -> Self {
+ let raw_points = unsafe { std::slice::from_raw_parts(value.points, value.pointCount) };
+ let points: Vec<_> = raw_points.iter().copied().map(Point::from).collect();
+ Self {
+ branch_type: value.type_,
+ target: unsafe { FlowGraphNode::from_raw(value.target) }.to_owned(),
+ points,
+ back_edge: value.backEdge,
+ style: value.style.into(),
+ }
+ }
+}
+
+impl CoreArrayProvider for FlowGraphEdge {
+ type Raw = BNFlowGraphEdge;
+ type Context = ();
+ type Wrapped<'a> = FlowGraphEdge;
+}
+
+unsafe impl CoreArrayProviderInner for FlowGraphEdge {
+ unsafe fn free(raw: *mut Self::Raw, count: usize, _: &Self::Context) {
+ BNFreeFlowGraphNodeEdgeList(raw, count);
+ }
+
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
+ Self::from_raw(raw)
+ }
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Default)]
+pub struct Point {
+ pub x: f32,
+ pub y: f32,
+}
+
+impl Point {
+ pub fn new(x: f32, y: f32) -> Self {
+ Self { x, y }
+ }
+}
+
+impl From<BNPoint> for Point {
+ fn from(value: BNPoint) -> Self {
+ Self {
+ x: value.x,
+ y: value.y,
+ }
+ }
+}
+
+impl From<Point> for BNPoint {
+ fn from(value: Point) -> Self {
+ Self {
+ x: value.x,
+ y: value.y,
+ }
+ }
+}
+
+#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
+pub struct EdgeStyle {
+ style: EdgePenStyle,
+ width: usize,
+ color: ThemeColor,
+}
+
+impl EdgeStyle {
+ pub fn new(style: EdgePenStyle, width: usize, color: ThemeColor) -> Self {
+ Self {
+ style,
+ width,
+ color,
+ }
+ }
+}
+
+impl Default for EdgeStyle {
+ fn default() -> Self {
+ Self::new(EdgePenStyle::SolidLine, 0, ThemeColor::AddressColor)
+ }
+}
+
+impl From<BNEdgeStyle> for EdgeStyle {
+ fn from(style: BNEdgeStyle) -> Self {
+ Self::new(style.style, style.width, style.color)
+ }
+}
+
+impl From<EdgeStyle> for BNEdgeStyle {
+ fn from(style: EdgeStyle) -> Self {
+ Self {
+ style: style.style,
+ width: style.width,
+ color: style.color,
+ }
+ }
+}