Move to exception

This commit is contained in:
5ohue
2024-05-14 13:52:29 +03:00
parent e870041015
commit 3d4782390d
7 changed files with 237 additions and 254 deletions

View File

@ -73,7 +73,7 @@ pub fn magick_query_fonts(pattern: &str) -> Result<Vec<String>> {
let ptr =
unsafe { bindings::MagickQueryFonts(c_string.as_ptr(), &mut number_fonts as *mut size_t) };
if ptr.is_null() {
Err(MagickError("null ptr returned by magick_query_fonts"))
Err(MagickError("null ptr returned by magick_query_fonts".to_string()))
} else {
let mut v = Vec::new();
let c_str_ptr_slice = unsafe { ::std::slice::from_raw_parts(ptr, number_fonts as usize) };

View File

@ -2,18 +2,18 @@ use std::fmt::{Debug, Display, Formatter};
pub type Result<T> = std::result::Result<T, MagickError>;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct MagickError(pub &'static str);
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct MagickError(pub String);
impl From<&'static str> for MagickError {
fn from(s: &'static str) -> Self {
MagickError(s)
MagickError(s.to_string())
}
}
impl Display for MagickError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self.0, f)
Display::fmt(&self.0, f)
}
}

View File

@ -197,11 +197,11 @@ impl KernelBuilder {
}
pub fn build(&self) -> Result<KernelInfo> {
let size = self.size.ok_or(MagickError("no kernel size given"))?;
let values = self.values.as_ref().ok_or(MagickError("no kernel values given"))?;
let size = self.size.ok_or(MagickError("no kernel size given".to_string()))?;
let values = self.values.as_ref().ok_or(MagickError("no kernel values given".to_string()))?;
if values.len() != size.0 * size.1 {
return Err(MagickError("kernel size doesn't match kernel values size"));
return Err(MagickError("kernel size doesn't match kernel values size".to_string()));
}
// Create kernel string
@ -241,7 +241,7 @@ impl KernelBuilder {
};
if kernel_info.is_null() {
return Err(MagickError("failed to acquire kernel info"));
return Err(MagickError("failed to acquire kernel info".to_string()));
}
Ok(KernelInfo::new(kernel_info))
@ -260,8 +260,8 @@ impl KernelBuilder {
}
pub fn build_builtin(&self) -> Result<KernelInfo> {
let info_type = self.info_type.ok_or(MagickError("no info type given"))?;
let mut geom_info = self.geom_info.ok_or(MagickError("no geometry info given"))?;
let info_type = self.info_type.ok_or(MagickError("no info type given".to_string()))?;
let mut geom_info = self.geom_info.ok_or(MagickError("no geometry info given".to_string()))?;
// Create kernel info
let kernel_info = unsafe {
@ -273,7 +273,7 @@ impl KernelBuilder {
};
if kernel_info.is_null() {
return Err(MagickError("failed to acquire builtin kernel info"));
return Err(MagickError("failed to acquire builtin kernel info".to_string()));
}
Ok(KernelInfo::new(kernel_info))

View File

@ -44,7 +44,7 @@ macro_rules! wand_common {
"failed to clear",
stringify!($wand),
"exception"
))),
).to_string())),
}
}
@ -55,24 +55,28 @@ macro_rules! wand_common {
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(MagickError(concat!(
"null ptr returned by",
stringify!($wand),
"get_exception"
)))
).to_string()))
} else {
let c_str = unsafe { CStr::from_ptr(ptr) };
Ok((c_str.to_string_lossy().into_owned(), severity))
let exception = c_str.to_string_lossy().into_owned();
unsafe {
::bindings::RelinquishMagickMemory(ptr as *mut ::libc::c_void)
};
Ok((exception, severity))
}
}
pub fn is_wand(&self) -> Result<()> {
match unsafe { ::bindings::$is_wand(self.wand) } {
::bindings::MagickBooleanType_MagickTrue => Ok(()),
_ => Err(MagickError(concat!(stringify!($wand), " not a wand"))),
_ => Err(MagickError(concat!(stringify!($wand), " not a wand").to_string())),
}
}
}
@ -115,7 +119,7 @@ macro_rules! set_get {
pub fn $set(&mut self, v: $typ) -> Result<()> {
match unsafe { ::bindings::$c_set(self.wand, v.into()) } {
::bindings::MagickBooleanType_MagickTrue => Ok(()),
_ => Err(MagickError(concat!(stringify!($set), " returned false")))
_ => Err(MagickError(concat!(stringify!($set), " returned false").to_string()))
}
}
)*
@ -151,7 +155,7 @@ macro_rules! string_get {
Err(MagickError(concat!(
"null ptr returned by ",
stringify!($get)
)))
).to_string()))
} else {
let c_str = unsafe { ::std::ffi::CStr::from_ptr(ptr) };
let result: String = c_str.to_string_lossy().into_owned();
@ -170,7 +174,7 @@ macro_rules! string_set_get {
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(MagickError(concat!(stringify!($set), " returned false")))
_ => Err(MagickError(concat!(stringify!($set), " returned false").to_string()))
}
}
)*
@ -260,7 +264,7 @@ macro_rules! mutations {
pub fn $fun(&self $(, $arg: $ty)*) -> Result<()> {
match unsafe { bindings::$c_fun(self.wand $(, $arg.into())*) } {
bindings::MagickBooleanType_MagickTrue => Ok(()),
_ => Err(MagickError(concat!(stringify!($c_fun), " invocation failed")))
_ => Err(MagickError(concat!(stringify!($c_fun), " invocation failed").to_string()))
}
}
)*

File diff suppressed because it is too large Load Diff

View File

@ -42,11 +42,9 @@ wand_common!(
);
impl PixelWand {
pub fn is_similar(&self, other: &PixelWand, fuzz: f64) -> Result<()> {
match unsafe { bindings::IsPixelWandSimilar(self.wand, other.wand, fuzz) } {
MagickTrue => Ok(()),
_ => Err(MagickError("not similar")),
}
pub fn is_similar(&self, other: &PixelWand, fuzz: f64) -> bool {
let result = unsafe { bindings::IsPixelWandSimilar(self.wand, other.wand, fuzz) };
return result == MagickTrue;
}
pub fn get_hsl(&self) -> HSL {
@ -83,7 +81,7 @@ impl PixelWand {
let c_string = CString::new(s).map_err(|_| "could not convert to cstring")?;
match unsafe { bindings::PixelSetColor(self.wand, c_string.as_ptr()) } {
MagickTrue => Ok(()),
_ => Err(MagickError("failed to set color")),
_ => Err(MagickError(self.get_exception()?.0)),
}
}

View File

@ -171,7 +171,7 @@ fn test_get_image_property() {
// retrieve a property that does not exist
let missing_value = wand.get_image_property("exif:Foobar");
assert!(missing_value.is_err());
assert_eq!(MagickError("missing property"), missing_value.unwrap_err());
assert_eq!(MagickError("missing property: exif:Foobar".to_string()), missing_value.unwrap_err());
}
#[test]