use std::ffi::CStr;
use std::fmt;
use crate::sys;
pub type LightGBMResult<T> = std::result::Result<T, LightGBMError>;
#[derive(Debug, Eq, PartialEq)]
pub struct LightGBMError {
pub description: String,
}
impl LightGBMError {
pub fn check_return_value(ret_val: i32) -> LightGBMResult<()> {
if ret_val == 0 {
Ok(())
} else {
Err(LightGBMError::fetch_lightgbm_error())
}
}
fn fetch_lightgbm_error() -> Self {
let c_str = unsafe { CStr::from_ptr(sys::LGBM_GetLastError()) };
let str_slice = c_str.to_str().unwrap_or("Unknown error");
LightGBMError {
description: str_slice.to_owned(),
}
}
}
impl fmt::Display for LightGBMError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.description)
}
}
impl std::error::Error for LightGBMError {}