catboost_rs/
error.rs

1use catboost_sys;
2use std::ffi::CStr;
3use std::fmt;
4
5pub type CatBoostResult<T> = std::result::Result<T, CatBoostError>;
6
7#[derive(Debug, Eq, PartialEq)]
8pub struct CatBoostError {
9    description: String,
10}
11
12impl CatBoostError {
13    /// Check the return value from an CatBoost FFI call, and return the last error message on error.
14    /// Return values of true are treated as success, returns values of false are treated as errors.
15    pub fn check_return_value(ret_val: bool) -> CatBoostResult<()> {
16        if ret_val {
17            Ok(())
18        } else {
19            Err(CatBoostError::fetch_catboost_error())
20        }
21    }
22
23    /// Fetch current error message from CatBoost.
24    fn fetch_catboost_error() -> Self {
25        let c_str = unsafe { CStr::from_ptr(catboost_sys::GetErrorString()) };
26        let str_slice = c_str.to_str().unwrap();
27        CatBoostError {
28            description: str_slice.to_owned(),
29        }
30    }
31}
32
33impl fmt::Display for CatBoostError {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        write!(f, "{}", self.description)
36    }
37}
38
39impl std::error::Error for CatBoostError {}