#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
pub type DaqResult<T> = Result<T, i32>;
pub fn check_result(result: i32) -> DaqResult<()> {
if result == ResultCode_RESULT_SUCCESS {
Ok(())
} else {
Err(result)
}
}
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
pub fn error_message(result: i32) -> String {
unsafe {
let msg_ptr = hat_error_message(result);
if msg_ptr.is_null() {
format!("Unknown error code: {}", result)
} else {
std::ffi::CStr::from_ptr(msg_ptr)
.to_string_lossy()
.into_owned()
}
}
}
#[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))]
pub fn error_message(result: i32) -> String {
match result {
0 => "Success, no errors".to_string(),
-1 => "A parameter passed to the function was incorrect".to_string(),
-2 => "The device is busy".to_string(),
-3 => "There was a timeout accessing a resource".to_string(),
-4 => "There was a timeout while obtaining a resource lock".to_string(),
-5 => "The device at the specified address is not the correct type".to_string(),
-6 => "A needed resource was not available".to_string(),
-7 => "Could not communicate with the device".to_string(),
-10 => "Some other error occurred".to_string(),
_ => format!("Unknown error code: {}", result),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_constants() {
assert_eq!(ResultCode_RESULT_SUCCESS, 0);
assert_eq!(HatIDs_HAT_ID_MCC_118, 322);
assert_eq!(MAX_NUMBER_HATS, 8);
}
#[test]
fn test_error_message() {
let msg = error_message(ResultCode_RESULT_SUCCESS);
assert!(!msg.is_empty());
assert!(msg.contains("Success") || msg.contains("success"));
}
#[test]
fn test_check_result() {
assert!(check_result(ResultCode_RESULT_SUCCESS).is_ok());
assert!(check_result(ResultCode_RESULT_BAD_PARAMETER).is_err());
}
}