Skip to content

Commit

Permalink
Clean unwraps
Browse files Browse the repository at this point in the history
Use pattern matching to avoid .is_none() and .unwrap()
  • Loading branch information
LouisDISPA committed Feb 29, 2024
1 parent b61879a commit 8434413
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 47 deletions.
9 changes: 3 additions & 6 deletions src/dll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,9 @@ fn shell_change_notify() {
#[allow(non_snake_case)]
#[doc(hidden)]
pub unsafe extern "system" fn DllRegisterServer() -> HRESULT {
let module_path = {
let result = get_module_path(DLL_INSTANCE);
if let Err(err) = result {
return err;
}
result.unwrap()
let module_path = match get_module_path(DLL_INSTANCE) {
Ok(path) => path,
Err(err) => return err,
};
if register(&module_path).is_ok() {
shell_change_notify();
Expand Down
68 changes: 33 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ impl IWICBitmapDecoder_Impl for JXLWICBitmapDecoder {

fn GetMetadataQueryReader(&self) -> windows::core::Result<IWICMetadataQueryReader> {
log::trace!("JXLWICBitmapDecoder::GetMetadataQueryReader");
Err(WINCODEC_ERR_UNSUPPORTEDOPERATION.ok().unwrap_err())
Err(WINCODEC_ERR_UNSUPPORTEDOPERATION.into())
}

fn GetPreview(&self) -> windows::core::Result<IWICBitmapSource> {
log::trace!("JXLWICBitmapDecoder::GetPreview");
Err(WINCODEC_ERR_UNSUPPORTEDOPERATION.ok().unwrap_err())
Err(WINCODEC_ERR_UNSUPPORTEDOPERATION.into())
}

fn GetColorContexts(
Expand All @@ -125,10 +125,10 @@ impl IWICBitmapDecoder_Impl for JXLWICBitmapDecoder {
pcactualcount: *mut u32,
) -> windows::core::Result<()> {
let decoded_ref = self.decoded.borrow();
if decoded_ref.is_none() {

let Some(decoded) = decoded_ref.as_ref() else {
return WINCODEC_ERR_NOTINITIALIZED.ok();
}
let decoded = decoded_ref.as_ref().unwrap();
};

log::trace!(
"JXLWICBitmapDecoder::GetColorContexts {} {:?} {:?}",
Expand All @@ -138,13 +138,13 @@ impl IWICBitmapDecoder_Impl for JXLWICBitmapDecoder {
);
// TODO: Proper color context
unsafe {
if !ppicolorcontexts.is_null() && ccount == 1 {
ppicolorcontexts
.as_mut()
.unwrap()
.as_mut()
.expect("There should be a color context here")
.InitializeFromMemory(&decoded.icc[..])?;
if let Some(context) = ppicolorcontexts.as_mut() {
if ccount == 1 {
context
.as_mut()
.expect("There should be a color context here")
.InitializeFromMemory(&decoded.icc[..])?;
}
}
if !pcactualcount.is_null() {
*pcactualcount = 1;
Expand All @@ -155,31 +155,30 @@ impl IWICBitmapDecoder_Impl for JXLWICBitmapDecoder {

fn GetThumbnail(&self) -> windows::core::Result<IWICBitmapSource> {
log::trace!("JXLWICBitmapDecoder::GetThumbnail");
Err(WINCODEC_ERR_CODECNOTHUMBNAIL.ok().unwrap_err())
Err(WINCODEC_ERR_CODECNOTHUMBNAIL.into())
}

fn GetFrameCount(&self) -> windows::core::Result<u32> {
let decoded_ref = self.decoded.borrow();
if decoded_ref.is_none() {
return Err(WINCODEC_ERR_NOTINITIALIZED.ok().unwrap_err());
}
let frame_count = decoded_ref.as_ref().unwrap().frame_count;
let Some(decoded) = decoded_ref.as_ref() else {
return Err(WINCODEC_ERR_NOTINITIALIZED.into());
};
let frame_count = decoded.frame_count;

log::trace!("JXLWICBitmapDecoder::GetFrameCount: {}", frame_count);
Ok(frame_count as u32)
}

fn GetFrame(&self, index: u32) -> windows::core::Result<IWICBitmapFrameDecode> {
let mut decoded_ref = self.decoded.borrow_mut();
if decoded_ref.is_none() {
return Err(WINCODEC_ERR_NOTINITIALIZED.ok().unwrap_err());
}
let Some(decoded) = decoded_ref.as_mut() else {
return Err(WINCODEC_ERR_NOTINITIALIZED.into());
};

let decoded = decoded_ref.as_mut().unwrap();
log::trace!("[{}/{}]", index, decoded.frame_count);

if index >= decoded.frame_count as u32 {
return Err(WINCODEC_ERR_FRAMEMISSING.ok().unwrap_err());
return Err(WINCODEC_ERR_FRAMEMISSING.into());
}

let render = decoded.image.render_frame(index as usize).map_err(|err| {
Expand Down Expand Up @@ -281,11 +280,10 @@ impl IWICBitmapSource_Impl for JXLWICBitmapFrameDecode {
) -> windows::core::Result<()> {
log::trace!("JXLWICBitmapFrameDecode::CopyPixels");

if prc.is_null() {
return Err(E_INVALIDARG.ok().unwrap_err());
}
let Some(prc) = (unsafe { prc.as_ref() }) else {
return Err(E_INVALIDARG.into());
};

let prc = unsafe { prc.as_ref().unwrap() };
log::trace!("JXLWICBitmapFrameDecode::CopyPixels::WICRect {:?}", prc);

let channels = self.frame.channels();
Expand All @@ -312,7 +310,7 @@ impl IWICBitmapSource_Impl for JXLWICBitmapFrameDecode {
impl IWICBitmapFrameDecode_Impl for JXLWICBitmapFrameDecode {
fn GetMetadataQueryReader(&self) -> windows::core::Result<IWICMetadataQueryReader> {
log::trace!("JXLWICBitmapFrameDecode::GetMetadataQueryReader");
Err(WINCODEC_ERR_UNSUPPORTEDOPERATION.ok().unwrap_err())
Err(WINCODEC_ERR_UNSUPPORTEDOPERATION.into())
}

fn GetColorContexts(
Expand All @@ -328,13 +326,13 @@ impl IWICBitmapFrameDecode_Impl for JXLWICBitmapFrameDecode {
pcactualcount
);
unsafe {
if !ppicolorcontexts.is_null() && ccount == 1 {
ppicolorcontexts
.as_mut()
.unwrap()
.as_mut()
.expect("There should be a color context here")
.InitializeFromMemory(&self.icc[..])?;
if let Some(context) = ppicolorcontexts.as_mut() {
if ccount == 1 {
context
.as_mut()
.expect("There should be a color context here")
.InitializeFromMemory(&self.icc[..])?;
}
}
if !pcactualcount.is_null() {
*pcactualcount = 1;
Expand All @@ -345,6 +343,6 @@ impl IWICBitmapFrameDecode_Impl for JXLWICBitmapFrameDecode {

fn GetThumbnail(&self) -> windows::core::Result<IWICBitmapSource> {
log::trace!("JXLWICBitmapFrameDecode::GetThumbnail");
Err(WINCODEC_ERR_CODECNOTHUMBNAIL.ok().unwrap_err())
Err(WINCODEC_ERR_CODECNOTHUMBNAIL.into())
}
}
16 changes: 10 additions & 6 deletions src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ impl JXLPropertyStore {
pub const CLSID: GUID = GUID::from_u128(0x95ffe0f8_ab15_4751_a2f3_cfafdbf13664);

fn get_props(&self) -> windows::core::Result<&IPropertyStoreCache> {
if self.props.is_none() {
return Err(windows::core::Error::new(
match self.props {
Some(ref props) => Ok(props),
None => Err(windows::core::Error::new(
WINCODEC_ERR_NOTINITIALIZED,
"Property store not initialized",
));
)),
}

Ok(self.props.as_ref().unwrap())
}
}

Expand Down Expand Up @@ -65,7 +64,12 @@ impl IInitializeWithStream_Impl for JXLPropertyStore {
)?
};

let props = self.props.as_ref().unwrap();
let Some(props) = self.props.as_ref() else {
return Err(windows::core::Error::new(
WINCODEC_ERR_NOTINITIALIZED,
"Property store not initialized",
));
};

// XXX: This is copied from um/propkey.h.
// https://github.com/microsoft/win32metadata/issues/730
Expand Down

0 comments on commit 8434413

Please sign in to comment.