Skip to content

Commit

Permalink
Merge pull request #121 from 5ohue/exceptions
Browse files Browse the repository at this point in the history
Add exceptions
  • Loading branch information
nlfiedler authored May 17, 2024
2 parents d97f4fc + 3d47823 commit 7fcf43e
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 247 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
Expand Down
8 changes: 4 additions & 4 deletions src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/types/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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 {
Expand All @@ -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))
Expand Down
22 changes: 13 additions & 9 deletions src/wand/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ macro_rules! wand_common {
"failed to clear",
stringify!($wand),
"exception"
))),
).to_string())),
}
}

Expand All @@ -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())),
}
}
}
Expand Down Expand Up @@ -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()))
}
}
)*
Expand Down Expand Up @@ -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();
Expand All @@ -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()))
}
}
)*
Expand Down Expand Up @@ -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()))
}
}
)*
Expand Down
Loading

0 comments on commit 7fcf43e

Please sign in to comment.