#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Failure = blosc2_sys::BLOSC2_ERROR_FAILURE as _,
Stream = blosc2_sys::BLOSC2_ERROR_STREAM as _,
Data = blosc2_sys::BLOSC2_ERROR_DATA as _,
MemoryAlloc = blosc2_sys::BLOSC2_ERROR_MEMORY_ALLOC as _,
ReadBuffer = blosc2_sys::BLOSC2_ERROR_READ_BUFFER as _,
WriteBuffer = blosc2_sys::BLOSC2_ERROR_WRITE_BUFFER as _,
CodecSupport = blosc2_sys::BLOSC2_ERROR_CODEC_SUPPORT as _,
CodecParam = blosc2_sys::BLOSC2_ERROR_CODEC_PARAM as _,
CodecDict = blosc2_sys::BLOSC2_ERROR_CODEC_DICT as _,
VersionSupport = blosc2_sys::BLOSC2_ERROR_VERSION_SUPPORT as _,
InvalidHeader = blosc2_sys::BLOSC2_ERROR_INVALID_HEADER as _,
InvalidParam = blosc2_sys::BLOSC2_ERROR_INVALID_PARAM as _,
FileRead = blosc2_sys::BLOSC2_ERROR_FILE_READ as _,
FileWrite = blosc2_sys::BLOSC2_ERROR_FILE_WRITE as _,
FileOpen = blosc2_sys::BLOSC2_ERROR_FILE_OPEN as _,
NotFound = blosc2_sys::BLOSC2_ERROR_NOT_FOUND as _,
RunLength = blosc2_sys::BLOSC2_ERROR_RUN_LENGTH as _,
FilterPipeline = blosc2_sys::BLOSC2_ERROR_FILTER_PIPELINE as _,
ChunkInsert = blosc2_sys::BLOSC2_ERROR_CHUNK_INSERT as _,
ChunkAppend = blosc2_sys::BLOSC2_ERROR_CHUNK_APPEND as _,
ChunkUpdate = blosc2_sys::BLOSC2_ERROR_CHUNK_UPDATE as _,
TwoGbLimit = blosc2_sys::BLOSC2_ERROR_2GB_LIMIT as _,
SchunkCopy = blosc2_sys::BLOSC2_ERROR_SCHUNK_COPY as _,
FrameType = blosc2_sys::BLOSC2_ERROR_FRAME_TYPE as _,
FileTruncate = blosc2_sys::BLOSC2_ERROR_FILE_TRUNCATE as _,
ThreadCreate = blosc2_sys::BLOSC2_ERROR_THREAD_CREATE as _,
Postfilter = blosc2_sys::BLOSC2_ERROR_POSTFILTER as _,
FrameSpecial = blosc2_sys::BLOSC2_ERROR_FRAME_SPECIAL as _,
SChunkSpecial = blosc2_sys::BLOSC2_ERROR_SCHUNK_SPECIAL as _,
PluginIO = blosc2_sys::BLOSC2_ERROR_PLUGIN_IO as _,
FileRemove = blosc2_sys::BLOSC2_ERROR_FILE_REMOVE as _,
NullPointer = blosc2_sys::BLOSC2_ERROR_NULL_POINTER as _,
InvalidIndex = blosc2_sys::BLOSC2_ERROR_INVALID_INDEX as _,
MetalayerNotFound = blosc2_sys::BLOSC2_ERROR_METALAYER_NOT_FOUND as _,
MaxBufsizeExceeded = blosc2_sys::BLOSC2_ERROR_MAX_BUFSIZE_EXCEEDED as _,
Tuner = blosc2_sys::BLOSC2_ERROR_TUNER as _,
}
impl Error {
pub(crate) fn from_int(code: core::ffi::c_int) -> Self {
match code {
blosc2_sys::BLOSC2_ERROR_FAILURE => Error::Failure,
blosc2_sys::BLOSC2_ERROR_STREAM => Error::Stream,
blosc2_sys::BLOSC2_ERROR_DATA => Error::Data,
blosc2_sys::BLOSC2_ERROR_MEMORY_ALLOC => Error::MemoryAlloc,
blosc2_sys::BLOSC2_ERROR_READ_BUFFER => Error::ReadBuffer,
blosc2_sys::BLOSC2_ERROR_WRITE_BUFFER => Error::WriteBuffer,
blosc2_sys::BLOSC2_ERROR_CODEC_SUPPORT => Error::CodecSupport,
blosc2_sys::BLOSC2_ERROR_CODEC_PARAM => Error::CodecParam,
blosc2_sys::BLOSC2_ERROR_CODEC_DICT => Error::CodecDict,
blosc2_sys::BLOSC2_ERROR_VERSION_SUPPORT => Error::VersionSupport,
blosc2_sys::BLOSC2_ERROR_INVALID_HEADER => Error::InvalidHeader,
blosc2_sys::BLOSC2_ERROR_INVALID_PARAM => Error::InvalidParam,
blosc2_sys::BLOSC2_ERROR_FILE_READ => Error::FileRead,
blosc2_sys::BLOSC2_ERROR_FILE_WRITE => Error::FileWrite,
blosc2_sys::BLOSC2_ERROR_FILE_OPEN => Error::FileOpen,
blosc2_sys::BLOSC2_ERROR_NOT_FOUND => Error::NotFound,
blosc2_sys::BLOSC2_ERROR_RUN_LENGTH => Error::RunLength,
blosc2_sys::BLOSC2_ERROR_FILTER_PIPELINE => Error::FilterPipeline,
blosc2_sys::BLOSC2_ERROR_CHUNK_INSERT => Error::ChunkInsert,
blosc2_sys::BLOSC2_ERROR_CHUNK_APPEND => Error::ChunkAppend,
blosc2_sys::BLOSC2_ERROR_CHUNK_UPDATE => Error::ChunkUpdate,
blosc2_sys::BLOSC2_ERROR_2GB_LIMIT => Error::TwoGbLimit,
blosc2_sys::BLOSC2_ERROR_SCHUNK_COPY => Error::SchunkCopy,
blosc2_sys::BLOSC2_ERROR_FRAME_TYPE => Error::FrameType,
blosc2_sys::BLOSC2_ERROR_FILE_TRUNCATE => Error::FileTruncate,
blosc2_sys::BLOSC2_ERROR_THREAD_CREATE => Error::ThreadCreate,
blosc2_sys::BLOSC2_ERROR_POSTFILTER => Error::Postfilter,
blosc2_sys::BLOSC2_ERROR_FRAME_SPECIAL => Error::FrameSpecial,
blosc2_sys::BLOSC2_ERROR_SCHUNK_SPECIAL => Error::SChunkSpecial,
blosc2_sys::BLOSC2_ERROR_PLUGIN_IO => Error::PluginIO,
blosc2_sys::BLOSC2_ERROR_FILE_REMOVE => Error::FileRemove,
blosc2_sys::BLOSC2_ERROR_NULL_POINTER => Error::NullPointer,
blosc2_sys::BLOSC2_ERROR_INVALID_INDEX => Error::InvalidIndex,
blosc2_sys::BLOSC2_ERROR_METALAYER_NOT_FOUND => Error::MetalayerNotFound,
blosc2_sys::BLOSC2_ERROR_MAX_BUFSIZE_EXCEEDED => Error::MaxBufsizeExceeded,
blosc2_sys::BLOSC2_ERROR_TUNER => Error::Tuner,
unknown => {
crate::trace!("Unknown blosc2 error code: {}", unknown);
Error::Failure
}
}
}
pub(crate) fn to_int(&self) -> core::ffi::c_int {
match self {
Error::Failure => blosc2_sys::BLOSC2_ERROR_FAILURE,
Error::Stream => blosc2_sys::BLOSC2_ERROR_STREAM,
Error::Data => blosc2_sys::BLOSC2_ERROR_DATA,
Error::MemoryAlloc => blosc2_sys::BLOSC2_ERROR_MEMORY_ALLOC,
Error::ReadBuffer => blosc2_sys::BLOSC2_ERROR_READ_BUFFER,
Error::WriteBuffer => blosc2_sys::BLOSC2_ERROR_WRITE_BUFFER,
Error::CodecSupport => blosc2_sys::BLOSC2_ERROR_CODEC_SUPPORT,
Error::CodecParam => blosc2_sys::BLOSC2_ERROR_CODEC_PARAM,
Error::CodecDict => blosc2_sys::BLOSC2_ERROR_CODEC_DICT,
Error::VersionSupport => blosc2_sys::BLOSC2_ERROR_VERSION_SUPPORT,
Error::InvalidHeader => blosc2_sys::BLOSC2_ERROR_INVALID_HEADER,
Error::InvalidParam => blosc2_sys::BLOSC2_ERROR_INVALID_PARAM,
Error::FileRead => blosc2_sys::BLOSC2_ERROR_FILE_READ,
Error::FileWrite => blosc2_sys::BLOSC2_ERROR_FILE_WRITE,
Error::FileOpen => blosc2_sys::BLOSC2_ERROR_FILE_OPEN,
Error::NotFound => blosc2_sys::BLOSC2_ERROR_NOT_FOUND,
Error::RunLength => blosc2_sys::BLOSC2_ERROR_RUN_LENGTH,
Error::FilterPipeline => blosc2_sys::BLOSC2_ERROR_FILTER_PIPELINE,
Error::ChunkInsert => blosc2_sys::BLOSC2_ERROR_CHUNK_INSERT,
Error::ChunkAppend => blosc2_sys::BLOSC2_ERROR_CHUNK_APPEND,
Error::ChunkUpdate => blosc2_sys::BLOSC2_ERROR_CHUNK_UPDATE,
Error::TwoGbLimit => blosc2_sys::BLOSC2_ERROR_2GB_LIMIT,
Error::SchunkCopy => blosc2_sys::BLOSC2_ERROR_SCHUNK_COPY,
Error::FrameType => blosc2_sys::BLOSC2_ERROR_FRAME_TYPE,
Error::FileTruncate => blosc2_sys::BLOSC2_ERROR_FILE_TRUNCATE,
Error::ThreadCreate => blosc2_sys::BLOSC2_ERROR_THREAD_CREATE,
Error::Postfilter => blosc2_sys::BLOSC2_ERROR_POSTFILTER,
Error::FrameSpecial => blosc2_sys::BLOSC2_ERROR_FRAME_SPECIAL,
Error::SChunkSpecial => blosc2_sys::BLOSC2_ERROR_SCHUNK_SPECIAL,
Error::PluginIO => blosc2_sys::BLOSC2_ERROR_PLUGIN_IO,
Error::FileRemove => blosc2_sys::BLOSC2_ERROR_FILE_REMOVE,
Error::NullPointer => blosc2_sys::BLOSC2_ERROR_NULL_POINTER,
Error::InvalidIndex => blosc2_sys::BLOSC2_ERROR_INVALID_INDEX,
Error::MetalayerNotFound => blosc2_sys::BLOSC2_ERROR_METALAYER_NOT_FOUND,
Error::MaxBufsizeExceeded => blosc2_sys::BLOSC2_ERROR_MAX_BUFSIZE_EXCEEDED,
Error::Tuner => blosc2_sys::BLOSC2_ERROR_TUNER,
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let err_str = unsafe { blosc2_sys::blosc2_error_string(self.to_int()) };
assert!(!err_str.is_null());
let len = unsafe { libc::strlen(err_str) };
let err_str: &'static [u8] = unsafe { std::slice::from_raw_parts(err_str.cast(), len + 1) };
let err_str = std::ffi::CStr::from_bytes_with_nul(err_str).unwrap();
let err_str = err_str.to_str().unwrap();
f.write_str(err_str)
}
}
impl std::error::Error for Error {}
pub(crate) trait ErrorCode: Sized {
fn into_result(self) -> Result<Self, Error>;
}
macro_rules! impl_status_check {
($t:ty) => {
impl ErrorCode for $t {
fn into_result(self) -> Result<Self, Error> {
if self < 0 {
Err(Error::from_int(self as _))
} else {
Ok(self)
}
}
}
};
}
impl_status_check!(i32);
impl_status_check!(i64);