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 = let ptr =
unsafe { bindings::MagickQueryFonts(c_string.as_ptr(), &mut number_fonts as *mut size_t) }; unsafe { bindings::MagickQueryFonts(c_string.as_ptr(), &mut number_fonts as *mut size_t) };
if ptr.is_null() { 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 { } else {
let mut v = Vec::new(); let mut v = Vec::new();
let c_str_ptr_slice = unsafe { ::std::slice::from_raw_parts(ptr, number_fonts as usize) }; 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>; pub type Result<T> = std::result::Result<T, MagickError>;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct MagickError(pub &'static str); pub struct MagickError(pub String);
impl From<&'static str> for MagickError { impl From<&'static str> for MagickError {
fn from(s: &'static str) -> Self { fn from(s: &'static str) -> Self {
MagickError(s) MagickError(s.to_string())
} }
} }
impl Display for MagickError { impl Display for MagickError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { 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> { pub fn build(&self) -> Result<KernelInfo> {
let size = self.size.ok_or(MagickError("no kernel size 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"))?; let values = self.values.as_ref().ok_or(MagickError("no kernel values given".to_string()))?;
if values.len() != size.0 * size.1 { 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 // Create kernel string
@ -241,7 +241,7 @@ impl KernelBuilder {
}; };
if kernel_info.is_null() { 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)) Ok(KernelInfo::new(kernel_info))
@ -260,8 +260,8 @@ impl KernelBuilder {
} }
pub fn build_builtin(&self) -> Result<KernelInfo> { pub fn build_builtin(&self) -> Result<KernelInfo> {
let info_type = self.info_type.ok_or(MagickError("no info type 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"))?; let mut geom_info = self.geom_info.ok_or(MagickError("no geometry info given".to_string()))?;
// Create kernel info // Create kernel info
let kernel_info = unsafe { let kernel_info = unsafe {
@ -273,7 +273,7 @@ impl KernelBuilder {
}; };
if kernel_info.is_null() { 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)) Ok(KernelInfo::new(kernel_info))

View File

@ -44,7 +44,7 @@ macro_rules! wand_common {
"failed to clear", "failed to clear",
stringify!($wand), stringify!($wand),
"exception" "exception"
))), ).to_string())),
} }
} }
@ -55,24 +55,28 @@ macro_rules! wand_common {
pub fn get_exception(&self) -> Result<(String, ::bindings::ExceptionType)> { pub fn get_exception(&self) -> Result<(String, ::bindings::ExceptionType)> {
let mut severity: ::bindings::ExceptionType = let mut severity: ::bindings::ExceptionType =
::bindings::ExceptionType_UndefinedException; ::bindings::ExceptionType_UndefinedException;
// TODO: memory management
let ptr = unsafe { ::bindings::$get_exc(self.wand, &mut severity as *mut _) }; let ptr = unsafe { ::bindings::$get_exc(self.wand, &mut severity as *mut _) };
if ptr.is_null() { if ptr.is_null() {
Err(MagickError(concat!( Err(MagickError(concat!(
"null ptr returned by", "null ptr returned by",
stringify!($wand), stringify!($wand),
"get_exception" "get_exception"
))) ).to_string()))
} else { } else {
let c_str = unsafe { CStr::from_ptr(ptr) }; 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<()> { pub fn is_wand(&self) -> Result<()> {
match unsafe { ::bindings::$is_wand(self.wand) } { match unsafe { ::bindings::$is_wand(self.wand) } {
::bindings::MagickBooleanType_MagickTrue => Ok(()), ::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<()> { pub fn $set(&mut self, v: $typ) -> Result<()> {
match unsafe { ::bindings::$c_set(self.wand, v.into()) } { match unsafe { ::bindings::$c_set(self.wand, v.into()) } {
::bindings::MagickBooleanType_MagickTrue => Ok(()), ::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!( Err(MagickError(concat!(
"null ptr returned by ", "null ptr returned by ",
stringify!($get) stringify!($get)
))) ).to_string()))
} else { } else {
let c_str = unsafe { ::std::ffi::CStr::from_ptr(ptr) }; let c_str = unsafe { ::std::ffi::CStr::from_ptr(ptr) };
let result: String = c_str.to_string_lossy().into_owned(); 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")?; 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()) } { match unsafe { ::bindings::$c_set(self.wand, c_string.as_ptr()) } {
::bindings::MagickBooleanType_MagickTrue => Ok(()), ::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<()> { pub fn $fun(&self $(, $arg: $ty)*) -> Result<()> {
match unsafe { bindings::$c_fun(self.wand $(, $arg.into())*) } { match unsafe { bindings::$c_fun(self.wand $(, $arg.into())*) } {
bindings::MagickBooleanType_MagickTrue => Ok(()), 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 { impl PixelWand {
pub fn is_similar(&self, other: &PixelWand, fuzz: f64) -> Result<()> { pub fn is_similar(&self, other: &PixelWand, fuzz: f64) -> bool {
match unsafe { bindings::IsPixelWandSimilar(self.wand, other.wand, fuzz) } { let result = unsafe { bindings::IsPixelWandSimilar(self.wand, other.wand, fuzz) };
MagickTrue => Ok(()), return result == MagickTrue;
_ => Err(MagickError("not similar")),
}
} }
pub fn get_hsl(&self) -> HSL { 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")?; let c_string = CString::new(s).map_err(|_| "could not convert to cstring")?;
match unsafe { bindings::PixelSetColor(self.wand, c_string.as_ptr()) } { match unsafe { bindings::PixelSetColor(self.wand, c_string.as_ptr()) } {
MagickTrue => Ok(()), 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 // retrieve a property that does not exist
let missing_value = wand.get_image_property("exif:Foobar"); let missing_value = wand.get_image_property("exif:Foobar");
assert!(missing_value.is_err()); 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] #[test]