From 5ebbd0620903403fb30a61157b16f33c89afb584 Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Sat, 10 May 2025 23:09:10 -0400 Subject: [Rust] Interaction handler API fixes --- rust/src/interaction/handler.rs | 56 ++++++++++++++++++++++++++--------------- rust/src/interaction/report.rs | 11 +++++--- 2 files changed, 43 insertions(+), 24 deletions(-) (limited to 'rust/src/interaction') diff --git a/rust/src/interaction/handler.rs b/rust/src/interaction/handler.rs index 23ca66e4..f854f612 100644 --- a/rust/src/interaction/handler.rs +++ b/rust/src/interaction/handler.rs @@ -53,13 +53,13 @@ pub trait InteractionHandler: Sync + Send + 'static { task: &InteractionHandlerTask, ) -> bool; - fn show_plain_text_report(&mut self, view: &BinaryView, title: &str, contents: &str); + fn show_plain_text_report(&mut self, view: Option<&BinaryView>, title: &str, contents: &str); - fn show_graph_report(&mut self, view: &BinaryView, title: &str, graph: &FlowGraph); + fn show_graph_report(&mut self, view: Option<&BinaryView>, title: &str, graph: &FlowGraph); fn show_markdown_report( &mut self, - view: &BinaryView, + view: Option<&BinaryView>, title: &str, _contents: &str, plain_text: &str, @@ -69,7 +69,7 @@ pub trait InteractionHandler: Sync + Send + 'static { fn show_html_report( &mut self, - view: &BinaryView, + view: Option<&BinaryView>, title: &str, _contents: &str, plain_text: &str, @@ -80,24 +80,28 @@ pub trait InteractionHandler: Sync + Send + 'static { fn show_report_collection(&mut self, _title: &str, reports: &ReportCollection) { for report in reports { match &report { - Report::PlainText(rpt) => { - self.show_plain_text_report(&report.view(), &report.title(), &rpt.contents()) - } + Report::PlainText(rpt) => self.show_plain_text_report( + report.view().as_deref(), + &report.title(), + &rpt.contents(), + ), Report::Markdown(rm) => self.show_markdown_report( - &report.view(), + report.view().as_deref(), &report.title(), &rm.contents(), &rm.plaintext(), ), Report::Html(rh) => self.show_html_report( - &report.view(), + report.view().as_deref(), &report.title(), &rh.contents(), &rh.plaintext(), ), - Report::FlowGraph(rfg) => { - self.show_graph_report(&report.view(), &report.title(), &rfg.flow_graph()) - } + Report::FlowGraph(rfg) => self.show_graph_report( + report.view().as_deref(), + &report.title(), + &rfg.flow_graph(), + ), } } } @@ -292,7 +296,11 @@ unsafe extern "C" fn cb_show_plain_text_report( let ctxt = ctxt as *mut R; let title = raw_to_string(title).unwrap(); let contents = raw_to_string(contents).unwrap(); - (*ctxt).show_plain_text_report(&BinaryView::from_raw(view), &title, &contents) + let view = match !view.is_null() { + true => Some(BinaryView::from_raw(view)), + false => None, + }; + (*ctxt).show_plain_text_report(view.as_ref(), &title, &contents) } unsafe extern "C" fn cb_show_markdown_report( @@ -306,7 +314,11 @@ unsafe extern "C" fn cb_show_markdown_report( let title = raw_to_string(title).unwrap(); let contents = raw_to_string(contents).unwrap(); let plaintext = raw_to_string(plaintext).unwrap(); - (*ctxt).show_markdown_report(&BinaryView::from_raw(view), &title, &contents, &plaintext) + let view = match !view.is_null() { + true => Some(BinaryView::from_raw(view)), + false => None, + }; + (*ctxt).show_markdown_report(view.as_ref(), &title, &contents, &plaintext) } unsafe extern "C" fn cb_show_html_report( @@ -320,7 +332,11 @@ unsafe extern "C" fn cb_show_html_report( let title = raw_to_string(title).unwrap(); let contents = raw_to_string(contents).unwrap(); let plaintext = raw_to_string(plaintext).unwrap(); - (*ctxt).show_html_report(&BinaryView::from_raw(view), &title, &contents, &plaintext) + let view = match !view.is_null() { + true => Some(BinaryView::from_raw(view)), + false => None, + }; + (*ctxt).show_html_report(view.as_ref(), &title, &contents, &plaintext) } unsafe extern "C" fn cb_show_graph_report( @@ -331,11 +347,11 @@ unsafe extern "C" fn cb_show_graph_report( ) { let ctxt = ctxt as *mut R; let title = raw_to_string(title).unwrap(); - (*ctxt).show_graph_report( - &BinaryView::from_raw(view), - &title, - &FlowGraph::from_raw(graph), - ) + let view = match !view.is_null() { + true => Some(BinaryView::from_raw(view)), + false => None, + }; + (*ctxt).show_graph_report(view.as_ref(), &title, &FlowGraph::from_raw(graph)) } unsafe extern "C" fn cb_show_report_collection( diff --git a/rust/src/interaction/report.rs b/rust/src/interaction/report.rs index a20d8f1f..689b815b 100644 --- a/rust/src/interaction/report.rs +++ b/rust/src/interaction/report.rs @@ -46,9 +46,12 @@ impl ReportCollection { Report::new(self, i) } - fn view(&self, i: usize) -> Ref { + fn view(&self, i: usize) -> Option> { let raw = unsafe { BNGetReportView(self.handle.as_ptr(), i) }; - unsafe { BinaryView::ref_from_raw(raw) } + if raw.is_null() { + return None; + } + Some(unsafe { BinaryView::ref_from_raw(raw) }) } fn title(&self, i: usize) -> String { @@ -190,7 +193,7 @@ impl<'a> Report<'a> { } } - pub fn view(&self) -> Ref { + pub fn view(&self) -> Option> { self._inner().view() } @@ -253,7 +256,7 @@ impl ReportInner<'_> { self.collection.report_type(self.index) } - fn view(&self) -> Ref { + fn view(&self) -> Option> { self.collection.view(self.index) } -- cgit v1.3.1