1use core::ffi::CStr;
2use core::fmt::{Display, Debug};
3use core::ptr::null_mut;
4use libsoxr_ax_sys as sys;
5
6pub struct Error(&'static CStr);
7
8pub(crate) const CHANNEL_COUNT_TOO_LARGE: Error = Error(
9 unsafe { CStr::from_bytes_with_nul_unchecked(b"channel count does not fit in c_uint\0") }
10);
11
12impl Error {
13 pub unsafe fn from_raw(error: sys::soxr_error_t) -> Self {
14 Error(CStr::from_ptr(error))
15 }
16
17 pub(crate) unsafe fn check(error: sys::soxr_error_t) -> Result<(), Error> {
18 if error == null_mut() {
19 Ok(())
20 } else {
21 Err(Error::from_raw(error))
22 }
23 }
24
25 pub fn as_cstr(&self) -> &'static CStr {
26 self.0
27 }
28
29 pub fn as_str(&self) -> &'static str {
30 unsafe { core::str::from_utf8_unchecked(self.0.to_bytes()) }
32 }
33}
34
35impl Display for Error {
36 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
37 write!(f, "{}", self.as_str())
38 }
39}
40
41impl Debug for Error {
42 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
43 f.debug_tuple("Error").field(&self.as_str()).finish()
44 }
45}