feat: std error compatible error

This commit is contained in:
DCjanus
2021-12-25 02:41:45 +08:00
parent 679ccc43fa
commit 409a583b22
8 changed files with 210 additions and 191 deletions

View File

@ -37,10 +37,14 @@ macro_rules! wand_common {
unsafe { ::bindings::$clear_wand(self.wand) }
}
pub fn clear_exception(&mut self) -> Result<(), &'static str> {
pub fn clear_exception(&mut self) -> Result<()> {
match unsafe { ::bindings::$clear_exc(self.wand) } {
::bindings::MagickBooleanType_MagickTrue => Ok(()),
_ => Err(concat!("failed to clear", stringify!($wand), "exception")),
_ => Err(MagickError(concat!(
"failed to clear",
stringify!($wand),
"exception"
))),
}
}
@ -48,29 +52,27 @@ macro_rules! wand_common {
unsafe { ::bindings::$get_exc_type(self.wand) }
}
pub fn get_exception(
&self,
) -> Result<(String, ::bindings::ExceptionType), &'static str> {
pub fn get_exception(&self) -> Result<(String, ::bindings::ExceptionType)> {
let mut severity: ::bindings::ExceptionType =
::bindings::ExceptionType_UndefinedException;
// TODO: memory management
let ptr = unsafe { ::bindings::$get_exc(self.wand, &mut severity as *mut _) };
if ptr.is_null() {
Err(concat!(
Err(MagickError(concat!(
"null ptr returned by",
stringify!($wand),
"get_exception"
))
)))
} else {
let c_str = unsafe { CStr::from_ptr(ptr) };
Ok((c_str.to_string_lossy().into_owned(), severity))
}
}
pub fn is_wand(&self) -> Result<(), &'static str> {
pub fn is_wand(&self) -> Result<()> {
match unsafe { ::bindings::$is_wand(self.wand) } {
::bindings::MagickBooleanType_MagickTrue => Ok(()),
_ => Err(concat!(stringify!($wand), " not a wand")),
_ => Err(MagickError(concat!(stringify!($wand), " not a wand"))),
}
}
}
@ -110,10 +112,10 @@ macro_rules! set_get {
pub fn $get(&self) -> $typ {
unsafe { ::bindings::$c_get(self.wand) }
}
pub fn $set(&mut self, v: $typ) -> Result<(), &'static str> {
pub fn $set(&mut self, v: $typ) -> Result<()> {
match unsafe { ::bindings::$c_set(self.wand, v) } {
::bindings::MagickBooleanType_MagickTrue => Ok(()),
_ => Err(concat!(stringify!($set), " returned false"))
_ => Err(MagickError(concat!(stringify!($set), " returned false")))
}
}
)*
@ -143,10 +145,13 @@ macro_rules! set_get_unchecked {
macro_rules! string_get {
($get:ident, $c_get:ident) => {
pub fn $get(&self) -> Result<String, &'static str> {
pub fn $get(&self) -> Result<String> {
let ptr = unsafe { ::bindings::$c_get(self.wand) };
if ptr.is_null() {
Err(concat!("null ptr returned by ", stringify!($get)))
Err(MagickError(concat!(
"null ptr returned by ",
stringify!($get)
)))
} else {
let c_str = unsafe { ::std::ffi::CStr::from_ptr(ptr) };
let result: String = c_str.to_string_lossy().into_owned();
@ -161,11 +166,11 @@ macro_rules! string_set_get {
($($get:ident, $set:ident, $c_get:ident, $c_set:ident)*) => {
$(
string_get!($get, $c_get);
pub fn $set(&mut self, s: &str) -> Result<(), &'static str> {
pub fn $set(&mut self, s: &str) -> Result<()> {
let c_string = std::ffi::CString::new(s).map_err(|_| "could not convert to cstring")?;
match unsafe { ::bindings::$c_set(self.wand, c_string.as_ptr()) } {
::bindings::MagickBooleanType_MagickTrue => Ok(()),
_ => Err(concat!(stringify!($set), " returned false"))
_ => Err(MagickError(concat!(stringify!($set), " returned false")))
}
}
)*
@ -180,7 +185,7 @@ macro_rules! string_set_get_unchecked {
($($get:ident, $set:ident, $c_get:ident, $c_set:ident )*) => {
$(
string_get!($get, $c_get);
pub fn $set(&mut self, s: &str) -> Result<(), &'static str> {
pub fn $set(&mut self, s: &str) -> Result<()> {
let c_string = ::std::ffi::CString::new(s).map_err(|_| "could not convert to cstring")?;
unsafe { ::bindings::$c_set(self.wand, c_string.as_ptr()) };
Ok(())
@ -252,10 +257,10 @@ macro_rules! mutations {
($($(#[$attr:meta])* $c_fun:ident => $fun:ident($($arg:ident: $ty:ty),*))*) => {
$(
$(#[$attr])*
pub fn $fun(&self $(, $arg: $ty)*) -> Result<(), &'static str> {
pub fn $fun(&self $(, $arg: $ty)*) -> Result<()> {
match unsafe { bindings::$c_fun(self.wand $(, $arg)*) } {
bindings::MagickBooleanType_MagickTrue => Ok(()),
_ => Err(concat!(stringify!($c_fun), " invocation failed"))
_ => Err(MagickError(concat!(stringify!($c_fun), " invocation failed")))
}
}
)*