binaryninja/flowgraph/
edge.rs

1//! Represents the connection between two [`FlowGraphNode`]s.
2
3use crate::architecture::BranchType;
4use crate::flowgraph::node::FlowGraphNode;
5use crate::flowgraph::{EdgePenStyle, ThemeColor};
6use crate::rc::{CoreArrayProvider, CoreArrayProviderInner, Ref};
7use binaryninjacore_sys::*;
8
9#[derive(Clone, Debug, PartialEq)]
10pub struct FlowGraphEdge {
11    pub branch_type: BranchType,
12    pub target: Ref<FlowGraphNode>,
13    pub points: Vec<Point>,
14    pub back_edge: bool,
15    pub style: EdgeStyle,
16}
17
18impl FlowGraphEdge {
19    pub fn from_raw(value: &BNFlowGraphEdge) -> Self {
20        let raw_points = unsafe { std::slice::from_raw_parts(value.points, value.pointCount) };
21        let points: Vec<_> = raw_points.iter().copied().map(Point::from).collect();
22        Self {
23            branch_type: value.type_,
24            target: unsafe { FlowGraphNode::from_raw(value.target) }.to_owned(),
25            points,
26            back_edge: value.backEdge,
27            style: value.style.into(),
28        }
29    }
30}
31
32impl CoreArrayProvider for FlowGraphEdge {
33    type Raw = BNFlowGraphEdge;
34    type Context = ();
35    type Wrapped<'a> = FlowGraphEdge;
36}
37
38unsafe impl CoreArrayProviderInner for FlowGraphEdge {
39    unsafe fn free(raw: *mut Self::Raw, count: usize, _: &Self::Context) {
40        BNFreeFlowGraphNodeEdgeList(raw, count);
41    }
42
43    unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
44        Self::from_raw(raw)
45    }
46}
47
48#[derive(Clone, Copy, Debug, PartialEq, Default)]
49pub struct Point {
50    pub x: f32,
51    pub y: f32,
52}
53
54impl Point {
55    pub fn new(x: f32, y: f32) -> Self {
56        Self { x, y }
57    }
58}
59
60impl From<BNPoint> for Point {
61    fn from(value: BNPoint) -> Self {
62        Self {
63            x: value.x,
64            y: value.y,
65        }
66    }
67}
68
69impl From<Point> for BNPoint {
70    fn from(value: Point) -> Self {
71        Self {
72            x: value.x,
73            y: value.y,
74        }
75    }
76}
77
78#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
79pub struct EdgeStyle {
80    style: EdgePenStyle,
81    width: usize,
82    color: ThemeColor,
83}
84
85impl EdgeStyle {
86    pub fn new(style: EdgePenStyle, width: usize, color: ThemeColor) -> Self {
87        Self {
88            style,
89            width,
90            color,
91        }
92    }
93}
94
95impl Default for EdgeStyle {
96    fn default() -> Self {
97        Self::new(EdgePenStyle::SolidLine, 0, ThemeColor::AddressColor)
98    }
99}
100
101impl From<BNEdgeStyle> for EdgeStyle {
102    fn from(style: BNEdgeStyle) -> Self {
103        Self::new(style.style, style.width, style.color)
104    }
105}
106
107impl From<EdgeStyle> for BNEdgeStyle {
108    fn from(style: EdgeStyle) -> Self {
109        Self {
110            style: style.style,
111            width: style.width,
112            color: style.color,
113        }
114    }
115}