Skip to content

Commit aa04173

Browse files
committed
Misc interaction handler fixes for Rust API
1 parent cda608d commit aa04173

File tree

3 files changed

+50
-26
lines changed

3 files changed

+50
-26
lines changed

rust/src/interaction/handler.rs

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ pub trait InteractionHandler: Sync + Send + 'static {
5353
task: &InteractionHandlerTask,
5454
) -> bool;
5555

56-
fn show_plain_text_report(&mut self, view: &BinaryView, title: &str, contents: &str);
56+
fn show_plain_text_report(&mut self, view: Option<&BinaryView>, title: &str, contents: &str);
5757

58-
fn show_graph_report(&mut self, view: &BinaryView, title: &str, graph: &FlowGraph);
58+
fn show_graph_report(&mut self, view: Option<&BinaryView>, title: &str, graph: &FlowGraph);
5959

6060
fn show_markdown_report(
6161
&mut self,
62-
view: &BinaryView,
62+
view: Option<&BinaryView>,
6363
title: &str,
6464
_contents: &str,
6565
plain_text: &str,
@@ -69,7 +69,7 @@ pub trait InteractionHandler: Sync + Send + 'static {
6969

7070
fn show_html_report(
7171
&mut self,
72-
view: &BinaryView,
72+
view: Option<&BinaryView>,
7373
title: &str,
7474
_contents: &str,
7575
plain_text: &str,
@@ -80,24 +80,28 @@ pub trait InteractionHandler: Sync + Send + 'static {
8080
fn show_report_collection(&mut self, _title: &str, reports: &ReportCollection) {
8181
for report in reports {
8282
match &report {
83-
Report::PlainText(rpt) => {
84-
self.show_plain_text_report(&report.view(), &report.title(), &rpt.contents())
85-
}
83+
Report::PlainText(rpt) => self.show_plain_text_report(
84+
report.view().as_deref(),
85+
&report.title(),
86+
&rpt.contents(),
87+
),
8688
Report::Markdown(rm) => self.show_markdown_report(
87-
&report.view(),
89+
report.view().as_deref(),
8890
&report.title(),
8991
&rm.contents(),
9092
&rm.plaintext(),
9193
),
9294
Report::Html(rh) => self.show_html_report(
93-
&report.view(),
95+
report.view().as_deref(),
9496
&report.title(),
9597
&rh.contents(),
9698
&rh.plaintext(),
9799
),
98-
Report::FlowGraph(rfg) => {
99-
self.show_graph_report(&report.view(), &report.title(), &rfg.flow_graph())
100-
}
100+
Report::FlowGraph(rfg) => self.show_graph_report(
101+
report.view().as_deref(),
102+
&report.title(),
103+
&rfg.flow_graph(),
104+
),
101105
}
102106
}
103107
}
@@ -292,7 +296,11 @@ unsafe extern "C" fn cb_show_plain_text_report<R: InteractionHandler>(
292296
let ctxt = ctxt as *mut R;
293297
let title = raw_to_string(title).unwrap();
294298
let contents = raw_to_string(contents).unwrap();
295-
(*ctxt).show_plain_text_report(&BinaryView::from_raw(view), &title, &contents)
299+
let view = match !view.is_null() {
300+
true => Some(BinaryView::from_raw(view)),
301+
false => None,
302+
};
303+
(*ctxt).show_plain_text_report(view.as_ref(), &title, &contents)
296304
}
297305

298306
unsafe extern "C" fn cb_show_markdown_report<R: InteractionHandler>(
@@ -306,7 +314,11 @@ unsafe extern "C" fn cb_show_markdown_report<R: InteractionHandler>(
306314
let title = raw_to_string(title).unwrap();
307315
let contents = raw_to_string(contents).unwrap();
308316
let plaintext = raw_to_string(plaintext).unwrap();
309-
(*ctxt).show_markdown_report(&BinaryView::from_raw(view), &title, &contents, &plaintext)
317+
let view = match !view.is_null() {
318+
true => Some(BinaryView::from_raw(view)),
319+
false => None,
320+
};
321+
(*ctxt).show_markdown_report(view.as_ref(), &title, &contents, &plaintext)
310322
}
311323

312324
unsafe extern "C" fn cb_show_html_report<R: InteractionHandler>(
@@ -320,7 +332,11 @@ unsafe extern "C" fn cb_show_html_report<R: InteractionHandler>(
320332
let title = raw_to_string(title).unwrap();
321333
let contents = raw_to_string(contents).unwrap();
322334
let plaintext = raw_to_string(plaintext).unwrap();
323-
(*ctxt).show_html_report(&BinaryView::from_raw(view), &title, &contents, &plaintext)
335+
let view = match !view.is_null() {
336+
true => Some(BinaryView::from_raw(view)),
337+
false => None,
338+
};
339+
(*ctxt).show_html_report(view.as_ref(), &title, &contents, &plaintext)
324340
}
325341

326342
unsafe extern "C" fn cb_show_graph_report<R: InteractionHandler>(
@@ -331,11 +347,11 @@ unsafe extern "C" fn cb_show_graph_report<R: InteractionHandler>(
331347
) {
332348
let ctxt = ctxt as *mut R;
333349
let title = raw_to_string(title).unwrap();
334-
(*ctxt).show_graph_report(
335-
&BinaryView::from_raw(view),
336-
&title,
337-
&FlowGraph::from_raw(graph),
338-
)
350+
let view = match !view.is_null() {
351+
true => Some(BinaryView::from_raw(view)),
352+
false => None,
353+
};
354+
(*ctxt).show_graph_report(view.as_ref(), &title, &FlowGraph::from_raw(graph))
339355
}
340356

341357
unsafe extern "C" fn cb_show_report_collection<R: InteractionHandler>(

rust/src/interaction/report.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ impl ReportCollection {
4646
Report::new(self, i)
4747
}
4848

49-
fn view(&self, i: usize) -> Ref<BinaryView> {
49+
fn view(&self, i: usize) -> Option<Ref<BinaryView>> {
5050
let raw = unsafe { BNGetReportView(self.handle.as_ptr(), i) };
51-
unsafe { BinaryView::ref_from_raw(raw) }
51+
if raw.is_null() {
52+
return None;
53+
}
54+
Some(unsafe { BinaryView::ref_from_raw(raw) })
5255
}
5356

5457
fn title(&self, i: usize) -> String {
@@ -190,7 +193,7 @@ impl<'a> Report<'a> {
190193
}
191194
}
192195

193-
pub fn view(&self) -> Ref<BinaryView> {
196+
pub fn view(&self) -> Option<Ref<BinaryView>> {
194197
self._inner().view()
195198
}
196199

@@ -253,7 +256,7 @@ impl ReportInner<'_> {
253256
self.collection.report_type(self.index)
254257
}
255258

256-
fn view(&self) -> Ref<BinaryView> {
259+
fn view(&self) -> Option<Ref<BinaryView>> {
257260
self.collection.view(self.index)
258261
}
259262

rust/tests/interaction.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ impl InteractionHandler for MyInteractionHandler {
3636
todo!()
3737
}
3838

39-
fn show_plain_text_report(&mut self, _view: &BinaryView, _title: &str, _contents: &str) {
39+
fn show_plain_text_report(
40+
&mut self,
41+
_view: Option<&BinaryView>,
42+
_title: &str,
43+
_contents: &str,
44+
) {
4045
todo!()
4146
}
4247

43-
fn show_graph_report(&mut self, _view: &BinaryView, _title: &str, _graph: &FlowGraph) {
48+
fn show_graph_report(&mut self, _view: Option<&BinaryView>, _title: &str, _graph: &FlowGraph) {
4449
todo!()
4550
}
4651

0 commit comments

Comments
 (0)