binaryninja/flowgraph/
edge.rs

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