use std::ffi::CStr;
use mountpoint_s3_crt_sys::{aws_error_debug_str, aws_last_error, AWS_OP_SUCCESS};
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Error(i32);
impl Error {
pub(crate) unsafe fn last_error() -> Self {
Self(aws_last_error())
}
pub fn is_err(&self) -> bool {
self.0 != AWS_OP_SUCCESS
}
pub fn raw_error(&self) -> i32 {
self.0
}
}
fn err_code_to_debug_str(code: i32) -> &'static str {
unsafe {
let s = CStr::from_ptr(aws_error_debug_str(code));
s.to_str().expect("aws_error_debug_str should return valid ASCII")
}
}
impl From<i32> for Error {
fn from(err: i32) -> Self {
Self(err)
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "CRT error {}: {}", self.0, err_code_to_debug_str(self.0))
}
}
impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Error")
.field(&self.0)
.field(&err_code_to_debug_str(self.0))
.finish()
}
}
impl std::error::Error for Error {}