ncbi_vdb_sys/safe/
error.rs

1use crate::core::{klib::RCExplain, rc_t};
2
3/// Type alias for `Result<T, Error>`
4pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8    #[error("VDB error: {0}")]
9    Vdb(String),
10    #[error("Unexpected input VDB error: Cannot open input as DB or Table")]
11    UnexpectedInputVdb,
12    #[error("Unable to find column '{0}' in table")]
13    UnexpectedColumnName(String),
14    #[error("Provided range ({0}, {1}) is out of bounds for expected range ({2}, {3})")]
15    CursorRangeError(usize, usize, usize, usize),
16}
17
18impl From<rc_t> for Error {
19    fn from(rc: rc_t) -> Self {
20        let details = rc_details(rc);
21        Error::Vdb(details)
22    }
23}
24
25pub fn rc_details(rc: rc_t) -> String {
26    let mut buffer = [0 as std::os::raw::c_char; 1024];
27    let mut num_writ = 0;
28    unsafe {
29        RCExplain(rc, buffer.as_mut_ptr(), buffer.len(), &mut num_writ);
30    }
31    let c_str = unsafe { std::ffi::CStr::from_ptr(buffer.as_ptr()) };
32    c_str.to_string_lossy().into_owned()
33}