Skip to content

Commit 818e158

Browse files
committed
Return Option from table_info()
1 parent 7a2c4fa commit 818e158

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

crates/ark/src/data_explorer/r_data_explorer.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use amalthea::comm::data_explorer_comm::TableState;
2424
use amalthea::comm::event::CommManagerEvent;
2525
use amalthea::socket::comm::CommInitiator;
2626
use amalthea::socket::comm::CommSocket;
27+
use anyhow::anyhow;
2728
use anyhow::bail;
2829
use crossbeam::channel::Sender;
2930
use harp::exec::RFunction;
@@ -195,6 +196,9 @@ impl RDataExplorer {
195196
let table = self.table.get().clone();
196197
let object = *table;
197198

199+
let info =
200+
harp::table_info(object).ok_or(anyhow!("Unsupported type for data viewer"))?;
201+
198202
let harp::TableInfo {
199203
kind,
200204
dims:
@@ -203,7 +207,7 @@ impl RDataExplorer {
203207
num_cols: total_num_columns,
204208
},
205209
col_names: column_names,
206-
} = harp::table_info(object)?;
210+
} = info;
207211

208212
let lower_bound = cmp::min(start_index, total_num_columns) as isize;
209213
let upper_bound = cmp::min(total_num_columns, start_index + num_columns) as isize;
@@ -258,7 +262,7 @@ impl RDataExplorer {
258262
num_cols: num_columns,
259263
},
260264
col_names: _,
261-
} = harp::table_info(object)?;
265+
} = harp::table_info(object).ok_or(anyhow!("Unsupported type for data viewer"))?;
262266

263267
let state = TableState {
264268
table_shape: TableShape {
@@ -280,14 +284,16 @@ impl RDataExplorer {
280284
let table = self.table.get().clone();
281285
let object = *table;
282286

287+
let info = harp::table_info(object).ok_or(anyhow!("Unsupported type for data viewer"))?;
288+
283289
let harp::TableInfo {
284290
dims:
285291
harp::TableDim {
286292
num_rows: total_num_rows,
287293
num_cols: total_num_cols,
288294
},
289295
..
290-
} = harp::table_info(object)?;
296+
} = info;
291297

292298
let lower_bound = cmp::min(row_start_index, total_num_rows) as isize;
293299
let upper_bound = cmp::min(row_start_index + num_rows, total_num_rows) as isize;

crates/harp/src/table.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use libr::*;
22

33
use crate::exec::RFunction;
44
use crate::exec::RFunctionExt;
5+
use crate::object::r_length;
56
use crate::utils::r_is_data_frame;
67
use crate::utils::r_is_matrix;
78
use crate::utils::r_typeof;
@@ -19,17 +20,18 @@ pub struct TableInfo {
1920
pub col_names: ColumnNames,
2021
}
2122

22-
pub fn table_info(x: SEXP) -> anyhow::Result<TableInfo> {
23+
// TODO: Might want to encode as types with methods so that we can make
24+
// assumptions about memory layout more safely
25+
pub fn table_info(x: SEXP) -> Option<TableInfo> {
2326
if r_is_data_frame(x) {
24-
return df_info(x);
27+
return df_info(x).ok();
2528
}
2629

2730
if r_is_matrix(x) {
28-
return mat_info(x);
31+
return mat_info(x).ok();
2932
}
3033

31-
// TODO: better error message
32-
anyhow::bail!("Unsupported type for data viewer");
34+
None
3335
}
3436

3537
pub fn df_info(x: SEXP) -> anyhow::Result<TableInfo> {
@@ -81,6 +83,14 @@ pub fn mat_dim(x: SEXP) -> TableDim {
8183
unsafe {
8284
let dims = Rf_getAttrib(x, R_DimSymbol);
8385

86+
// Might want to return an error instead, or take a strongly typed input
87+
if r_typeof(dims) != INTSXP || r_length(dims) != 2 {
88+
return TableDim {
89+
num_rows: r_length(x) as i32,
90+
num_cols: 1,
91+
};
92+
}
93+
8494
TableDim {
8595
num_rows: INTEGER_ELT(dims, 0),
8696
num_cols: INTEGER_ELT(dims, 1),

0 commit comments

Comments
 (0)