diff --git a/src/context.rs b/src/context.rs index 084a3113..ddc545e1 100644 --- a/src/context.rs +++ b/src/context.rs @@ -18,10 +18,9 @@ use libc; use crate::{ callbacks, edit, engine::EngineInfo, - error::return_err, notation::SignatureNotations, results, - utils::{CStrArgument, SmallVec}, + utils::{convert_err, CStrArgument, SmallVec}, Data, EditInteractor, Error, ExportMode, Interactor, IntoData, Key, KeyListMode, NonNull, PassphraseProvider, ProgressReporter, Protocol, Result, SignMode, StatusHandler, }; @@ -47,7 +46,7 @@ impl Context { crate::init(); unsafe { let mut ctx = ptr::null_mut(); - return_err!(ffi::gpgme_new(&mut ctx)); + convert_err(ffi::gpgme_new(&mut ctx))?; Ok(Context::from_raw(ctx)) } } @@ -62,7 +61,7 @@ impl Context { pub fn from_protocol(proto: Protocol) -> Result { let ctx = Context::new()?; unsafe { - return_err!(ffi::gpgme_set_protocol(ctx.as_raw(), proto.raw())); + convert_err(ffi::gpgme_set_protocol(ctx.as_raw(), proto.raw()))?; } Ok(ctx) } @@ -149,13 +148,12 @@ impl Context { let name = name.into_cstr(); let value = value.into_cstr(); unsafe { - return_err!(ffi::gpgme_set_ctx_flag( + convert_err(ffi::gpgme_set_ctx_flag( self.as_raw(), name.as_ref().as_ptr(), value.as_ref().as_ptr(), - )); + )) } - Ok(()) } /// Upstream documentation: @@ -173,14 +171,13 @@ impl Context { .home_dir_raw() .map_or(ptr::null(), CStr::as_ptr); unsafe { - return_err!(ffi::gpgme_ctx_set_engine_info( + convert_err(ffi::gpgme_ctx_set_engine_info( self.as_raw(), self.protocol().raw(), path.as_ref().as_ptr(), home_dir, - )); + )) } - Ok(()) } #[inline] @@ -191,14 +188,13 @@ impl Context { .map_or(ptr::null(), CStr::as_ptr); let home_dir = home_dir.into_cstr(); unsafe { - return_err!(ffi::gpgme_ctx_set_engine_info( + convert_err(ffi::gpgme_ctx_set_engine_info( self.as_raw(), self.protocol().raw(), path, home_dir.as_ref().as_ptr(), - )); + )) } - Ok(()) } /// Upstream documentation: @@ -216,14 +212,13 @@ impl Context { let home_dir = home_dir .as_ref() .map_or(ptr::null(), |s| s.as_ref().as_ptr()); - return_err!(ffi::gpgme_ctx_set_engine_info( + convert_err(ffi::gpgme_ctx_set_engine_info( self.as_raw(), self.protocol().raw(), path, home_dir, - )); + )) } - Ok(()) } /// Upstream documentation: @@ -243,10 +238,7 @@ impl Context { { return Err(Error::NOT_SUPPORTED); } - unsafe { - return_err!(ffi::gpgme_set_pinentry_mode(self.as_raw(), mode.raw())); - } - Ok(()) + unsafe { convert_err(ffi::gpgme_set_pinentry_mode(self.as_raw(), mode.raw())) } } /// Upstream documentation: @@ -412,7 +404,7 @@ impl Context { /// [`gpgme_get_keylist_mode`](https://www.gnupg.org/documentation/manuals/gpgme/Key-Listing-Mode.html#index-gpgme_005fget_005fkeylist_005fmode) #[inline] pub fn key_list_mode(&self) -> KeyListMode { - unsafe { crate::KeyListMode::from_bits_retain(ffi::gpgme_get_keylist_mode(self.as_raw())) } + unsafe { KeyListMode::from_bits_retain(ffi::gpgme_get_keylist_mode(self.as_raw())) } } /// Adds all flags set in the provided key listing mode to the `Context`'s current mode. @@ -423,22 +415,18 @@ impl Context { pub fn add_key_list_mode(&mut self, mask: KeyListMode) -> Result<()> { unsafe { let old = ffi::gpgme_get_keylist_mode(self.as_raw()); - return_err!(ffi::gpgme_set_keylist_mode( + convert_err(ffi::gpgme_set_keylist_mode( self.as_raw(), mask.bits() | old, - )); + )) } - Ok(()) } /// Upstream documentation: /// [`gpgme_set_keylist_mode`](https://www.gnupg.org/documentation/manuals/gpgme/Key-Listing-Mode.html#index-gpgme_005fset_005fkeylist_005fmode) #[inline] pub fn set_key_list_mode(&mut self, mode: KeyListMode) -> Result<()> { - unsafe { - return_err!(ffi::gpgme_set_keylist_mode(self.as_raw(), mode.bits())); - } - Ok(()) + unsafe { convert_err(ffi::gpgme_set_keylist_mode(self.as_raw(), mode.bits())) } } /// Returns an iterator over all public keys available in the keyring. @@ -475,12 +463,12 @@ impl Context { let fingerprint = fpr.into_cstr(); unsafe { let mut key = ptr::null_mut(); - return_err!(ffi::gpgme_get_key( + convert_err(ffi::gpgme_get_key( self.as_raw(), fingerprint.as_ref().as_ptr(), &mut key, 0, - )); + ))?; Ok(Key::from_raw(key)) } } @@ -495,12 +483,12 @@ impl Context { let fingerprint = fpr.into_cstr(); unsafe { let mut key = ptr::null_mut(); - return_err!(ffi::gpgme_get_key( + convert_err(ffi::gpgme_get_key( self.as_raw(), fingerprint.as_ref().as_ptr(), &mut key, 1, - )); + ))?; Ok(Key::from_raw(key)) } } @@ -555,12 +543,12 @@ impl Context { ptr::null_mut() }; unsafe { - return_err!(ffi::gpgme_op_keylist_ext_start( + convert_err(ffi::gpgme_op_keylist_ext_start( self.as_raw(), ptr, if secret_only { 1 } else { 0 }, 0, - )); + ))?; } Ok(Keys { ctx: self, @@ -611,7 +599,7 @@ impl Context { let mut public = public.map_or(Ok(None), |d| d.into_data().map(Some))?; let mut secret = secret.map_or(Ok(None), |d| d.into_data().map(Some))?; unsafe { - return_err!(ffi::gpgme_op_genkey( + convert_err(ffi::gpgme_op_genkey( self.as_raw(), params.as_ref().as_ptr(), public @@ -620,7 +608,7 @@ impl Context { secret .as_mut() .map_or(ptr::null_mut(), |d| d.borrow_mut().as_raw()), - )); + ))?; } Ok(self.get_result().unwrap()) } @@ -664,7 +652,7 @@ impl Context { let algo = algo.into_cstr(); let expires = expires.as_secs().value_into().unwrap_or_saturate(); unsafe { - return_err!(ffi::gpgme_op_createkey( + convert_err(ffi::gpgme_op_createkey( self.as_raw(), userid.as_ref().as_ptr(), algo.as_ref().as_ptr(), @@ -672,7 +660,7 @@ impl Context { expires, ptr::null_mut(), flags.bits(), - )); + ))?; } Ok(self.get_result().unwrap()) } @@ -702,14 +690,14 @@ impl Context { let algo = algo.into_cstr(); let expires = expires.as_secs().value_into().unwrap_or_saturate(); unsafe { - return_err!(ffi::gpgme_op_createsubkey( + convert_err(ffi::gpgme_op_createsubkey( self.as_raw(), key.as_raw(), algo.as_ref().as_ptr(), 0, expires, flags.bits(), - )); + ))?; } Ok(self.get_result().unwrap()) } @@ -720,15 +708,14 @@ impl Context { pub fn set_expire_all(&mut self, key: &Key, expires: Duration) -> Result<()> { let expires = expires.as_secs().value_into().unwrap_or_saturate(); unsafe { - return_err!(ffi::gpgme_op_setexpire( + convert_err(ffi::gpgme_op_setexpire( self.as_raw(), key.as_raw(), expires, b"*\0".into_cstr().as_ref().as_ptr(), 0, - )); + )) } - Ok(()) } /// Upstream documentation: @@ -740,17 +727,14 @@ impl Context { I::Item: CStrArgument, { let expires = expires.as_secs().value_into().unwrap_or_saturate(); - self::with_joined_cstr(subkeys, |subkeys, _| { - unsafe { - return_err!(ffi::gpgme_op_setexpire( + self::with_joined_cstr(subkeys, |subkeys, _| unsafe { + convert_err(ffi::gpgme_op_setexpire( self.as_raw(), key.as_raw(), expires, subkeys.map_or(ptr::null(), |subkeys| subkeys.as_ptr()), 0, - )); - } - Ok(()) + )) }) } @@ -760,14 +744,13 @@ impl Context { pub fn add_uid(&mut self, key: &Key, userid: impl CStrArgument) -> Result<()> { let userid = userid.into_cstr(); unsafe { - return_err!(ffi::gpgme_op_adduid( + convert_err(ffi::gpgme_op_adduid( self.as_raw(), key.as_raw(), userid.as_ref().as_ptr(), 0, - )); + )) } - Ok(()) } /// Upstream documentation: @@ -776,14 +759,13 @@ impl Context { pub fn revoke_uid(&mut self, key: &Key, userid: impl CStrArgument) -> Result<()> { let userid = userid.into_cstr(); unsafe { - return_err!(ffi::gpgme_op_revuid( + convert_err(ffi::gpgme_op_revuid( self.as_raw(), key.as_raw(), userid.as_ref().as_ptr(), 0, - )); + )) } - Ok(()) } /// Upstream documentation: @@ -800,15 +782,14 @@ impl Context { let name = name.into_cstr(); let value = value.map(CStrArgument::into_cstr); unsafe { - return_err!(ffi::gpgme_op_set_uid_flag( + convert_err(ffi::gpgme_op_set_uid_flag( self.as_raw(), key.as_raw(), userid.as_ref().as_ptr(), name.as_ref().as_ptr(), value.as_ref().map_or(ptr::null(), |s| s.as_ref().as_ptr()), - )); + )) } - Ok(()) } #[inline] @@ -817,12 +798,10 @@ impl Context { } /// Signs the given key with the default signing key, or the keys specified via - /// [`add_signer`]. + /// [`Context::add_signer()`]. /// /// Upstream documentation: /// [`gpgme_op_keysign`](https://www.gnupg.org/documentation/manuals/gpgme/Signing-Keys.html#index-gpgme_005fop_005fkeysign) - /// - /// [`add_signer`]: struct.Context.html#method.add_signer #[inline] pub fn sign_key(&mut self, key: &Key, userids: I, expires: Duration) -> Result<()> where @@ -833,12 +812,10 @@ impl Context { } /// Signs the given key with the default signing key, or the keys specified via - /// [`add_signer`]. + /// [`Context::add_signer()`]. /// /// Upstream documentation: /// [`gpgme_op_keysign`](https://www.gnupg.org/documentation/manuals/gpgme/Signing-Keys.html#index-gpgme_005fop_005fkeysign) - /// - /// [`add_signer`]: struct.Context.html#method.add_signer pub fn sign_key_with_flags( &mut self, key: &Key, @@ -856,15 +833,14 @@ impl Context { flags |= crate::KeySigningFlags::LFSEP; } unsafe { - return_err!(ffi::gpgme_op_keysign( + convert_err(ffi::gpgme_op_keysign( self.as_raw(), key.as_raw(), userids.map_or(ptr::null(), |uid| uid.as_ptr()), expires, flags.bits(), - )); + )) } - Ok(()) }) } @@ -884,15 +860,14 @@ impl Context { 0 }; unsafe { - return_err!(ffi::gpgme_op_revsig( + convert_err(ffi::gpgme_op_revsig( self.as_raw(), key.as_raw(), signing_key.as_raw(), userids.map_or(ptr::null(), |uid| uid.as_ptr()), flags, - )); + )) } - Ok(()) }) } @@ -901,23 +876,19 @@ impl Context { #[inline] pub fn change_key_tofu_policy(&mut self, key: &Key, policy: crate::TofuPolicy) -> Result<()> { unsafe { - return_err!(ffi::gpgme_op_tofu_policy( + convert_err(ffi::gpgme_op_tofu_policy( self.as_raw(), key.as_raw(), policy.raw(), - )); + )) } - Ok(()) } /// Upstream documentation: /// [`gpgme_op_passwd`](https://www.gnupg.org/documentation/manuals/gpgme/Changing-Passphrases.html#index-gpgme_005fop_005fpasswd) #[inline] pub fn change_key_passphrase(&mut self, key: &Key) -> Result<()> { - unsafe { - return_err!(ffi::gpgme_op_passwd(self.as_raw(), key.as_raw(), 0)); - } - Ok(()) + unsafe { convert_err(ffi::gpgme_op_passwd(self.as_raw(), key.as_raw(), 0)) } } /// Upstream documentation: @@ -936,15 +907,14 @@ impl Context { response: data.borrow_mut(), }; unsafe { - return_err!(ffi::gpgme_op_edit( + convert_err(ffi::gpgme_op_edit( self.as_raw(), key.as_raw(), Some(callbacks::edit_cb::), ptr::addr_of_mut!(hook).cast(), (*hook.response).as_raw(), - )); + )) } - Ok(()) } /// Upstream documentation: @@ -963,15 +933,14 @@ impl Context { response: data.borrow_mut(), }; unsafe { - return_err!(ffi::gpgme_op_card_edit( + convert_err(ffi::gpgme_op_card_edit( self.as_raw(), key.as_raw(), Some(callbacks::edit_cb::), ptr::addr_of_mut!(hook).cast(), (*hook.response).as_raw(), - )); + )) } - Ok(()) } /// Upstream documentation: @@ -1050,36 +1019,29 @@ impl Context { response: data.borrow_mut(), }; unsafe { - return_err!(ffi::gpgme_op_interact( + convert_err(ffi::gpgme_op_interact( self.as_raw(), key.map_or(ptr::null_mut(), |k| k.as_raw()), flags.bits(), Some(callbacks::interact_cb::), ptr::addr_of_mut!(hook).cast(), (*hook.response).as_raw(), - )); + )) } - Ok(()) } /// Upstream documentation: /// [`gpgme_op_delete`](https://www.gnupg.org/documentation/manuals/gpgme/Deleting-Keys.html#index-gpgme_005fop_005fdelete) #[inline] pub fn delete_key(&mut self, key: &Key) -> Result<()> { - unsafe { - return_err!(ffi::gpgme_op_delete(self.as_raw(), key.as_raw(), 0)); - } - Ok(()) + unsafe { convert_err(ffi::gpgme_op_delete(self.as_raw(), key.as_raw(), 0)) } } /// Upstream documentation: /// [`gpgme_op_delete`](https://www.gnupg.org/documentation/manuals/gpgme/Deleting-Keys.html#index-gpgme_005fop_005fdelete) #[inline] pub fn delete_secret_key(&mut self, key: &Key) -> Result<()> { - unsafe { - return_err!(ffi::gpgme_op_delete(self.as_raw(), key.as_raw(), 1)); - } - Ok(()) + unsafe { convert_err(ffi::gpgme_op_delete(self.as_raw(), key.as_raw(), 1)) } } /// Upstream documentation: @@ -1087,13 +1049,12 @@ impl Context { #[inline] pub fn delete_key_with_flags(&mut self, key: &Key, flags: crate::DeleteKeyFlags) -> Result<()> { unsafe { - return_err!(ffi::gpgme_op_delete_ext( + convert_err(ffi::gpgme_op_delete_ext( self.as_raw(), key.as_raw(), - flags.bits() - )); + flags.bits(), + )) } - Ok(()) } /// Upstream documentation: @@ -1105,10 +1066,10 @@ impl Context { { let mut src = src.into_data()?; unsafe { - return_err!(ffi::gpgme_op_import( + convert_err(ffi::gpgme_op_import( self.as_raw(), src.borrow_mut().as_raw(), - )); + ))?; } Ok(self.get_result().unwrap()) } @@ -1127,7 +1088,7 @@ impl Context { ptr::null_mut() }; unsafe { - return_err!(ffi::gpgme_op_import_keys(self.as_raw(), keys)); + convert_err(ffi::gpgme_op_import_keys(self.as_raw(), keys))?; } Ok(self.get_result().unwrap()) } @@ -1147,7 +1108,7 @@ impl Context { .chain(Some(ptr::null())) .collect(); unsafe { - return_err!(ffi::gpgme_op_receive_keys(self.as_raw(), uids.as_ptr())); + convert_err(ffi::gpgme_op_receive_keys(self.as_raw(), uids.as_ptr()))?; } Ok(self.get_result().unwrap()) } @@ -1219,14 +1180,13 @@ impl Context { ptr::null_mut() }; unsafe { - return_err!(ffi::gpgme_op_export_ext( + convert_err(ffi::gpgme_op_export_ext( self.as_raw(), ptr, mode.bits(), dst, - )); + )) } - Ok(()) } /// Upstream documentation: @@ -1269,24 +1229,20 @@ impl Context { ptr::null_mut() }; unsafe { - return_err!(ffi::gpgme_op_export_keys( + convert_err(ffi::gpgme_op_export_keys( self.as_raw(), keys, mode.bits(), dst, - )); + )) } - Ok(()) } /// Upstream documentation: /// [`gpgme_set_sender`](https://www.gnupg.org/documentation/manuals/gpgme/Setting-the-Sender.html#index-gpgme_005fset_005fsender) #[inline] pub fn clear_sender(&mut self) -> Result<()> { - unsafe { - return_err!(ffi::gpgme_set_sender(self.as_raw(), ptr::null())); - } - Ok(()) + unsafe { convert_err(ffi::gpgme_set_sender(self.as_raw(), ptr::null())) } } /// Upstream documentation: @@ -1295,12 +1251,11 @@ impl Context { pub fn set_sender(&mut self, sender: impl CStrArgument) -> Result<()> { let sender = sender.into_cstr(); unsafe { - return_err!(ffi::gpgme_set_sender( + convert_err(ffi::gpgme_set_sender( self.as_raw(), sender.as_ref().as_ptr(), - )); + )) } - Ok(()) } /// Upstream documentation: @@ -1333,10 +1288,7 @@ impl Context { /// [`gpgme_signers_add`](https://www.gnupg.org/documentation/manuals/gpgme/Selecting-Signers.html#index-gpgme_005fsigners_005fadd) #[inline] pub fn add_signer(&mut self, key: &Key) -> Result<()> { - unsafe { - return_err!(ffi::gpgme_signers_add(self.as_raw(), key.as_raw())); - } - Ok(()) + unsafe { convert_err(ffi::gpgme_signers_add(self.as_raw(), key.as_raw())) } } /// Upstream documentation: @@ -1370,14 +1322,13 @@ impl Context { let name = name.into_cstr(); let value = value.into_cstr(); unsafe { - return_err!(ffi::gpgme_sig_notation_add( + convert_err(ffi::gpgme_sig_notation_add( self.as_raw(), name.as_ref().as_ptr(), value.as_ref().as_ptr(), flags.bits(), - )); + )) } - Ok(()) } /// Upstream documentation: @@ -1395,14 +1346,13 @@ impl Context { } else { 0 }; - return_err!(ffi::gpgme_sig_notation_add( + convert_err(ffi::gpgme_sig_notation_add( self.as_raw(), ptr::null(), url.as_ref().as_ptr(), critical, - )); + )) } - Ok(()) } /// Upstream documentation: @@ -1513,12 +1463,12 @@ impl Context { let mut signature = signature.into_data()?; let mut plain = plaintext.into_data()?; unsafe { - return_err!(ffi::gpgme_op_sign( + convert_err(ffi::gpgme_op_sign( self.as_raw(), plain.borrow_mut().as_raw(), signature.borrow_mut().as_raw(), mode.raw(), - )); + ))?; } Ok(self.get_result().unwrap()) } @@ -1566,12 +1516,12 @@ impl Context { unsafe { let signed = signedtext.map_or(ptr::null_mut(), |d| d.as_raw()); let plain = plaintext.map_or(ptr::null_mut(), |d| d.as_raw()); - return_err!(ffi::gpgme_op_verify( + convert_err(ffi::gpgme_op_verify( self.as_raw(), signature.as_raw(), signed, plain, - )); + ))?; } Ok(self.get_result().unwrap()) } @@ -1589,13 +1539,13 @@ impl Context { unsafe { let signed = signedtext.map_or(ptr::null_mut(), |d| d.as_raw()); let plain = plaintext.map_or(ptr::null_mut(), |d| d.as_raw()); - return_err!(ffi::gpgme_op_verify_ext( + convert_err(ffi::gpgme_op_verify_ext( self.as_raw(), flags.bits(), signature.as_raw(), signed, plain, - )); + ))?; } Ok(self.get_result().unwrap()) } @@ -1656,13 +1606,13 @@ impl Context { }; unsafe { - return_err!(ffi::gpgme_op_encrypt( + convert_err(ffi::gpgme_op_encrypt( self.as_raw(), keys, flags.bits(), plain.borrow_mut().as_raw(), cipher.borrow_mut().as_raw(), - )); + ))?; } Ok(self.get_result().unwrap()) } @@ -1751,13 +1701,13 @@ impl Context { }; unsafe { - return_err!(ffi::gpgme_op_encrypt_sign( + convert_err(ffi::gpgme_op_encrypt_sign( self.as_raw(), keys, flags.bits(), plain.borrow_mut().as_raw(), cipher.borrow_mut().as_raw(), - )) + ))?; } Ok((self.get_result().unwrap(), self.get_result().unwrap())) } @@ -1791,11 +1741,11 @@ impl Context { let mut cipher = ciphertext.into_data()?; let mut plain = plaintext.into_data()?; unsafe { - return_err!(ffi::gpgme_op_decrypt( + convert_err(ffi::gpgme_op_decrypt( self.as_raw(), cipher.borrow_mut().as_raw(), plain.borrow_mut().as_raw(), - )); + ))?; } Ok(self.get_result().unwrap()) } @@ -1816,12 +1766,12 @@ impl Context { let mut cipher = ciphertext.into_data()?; let mut plain = plaintext.into_data()?; unsafe { - return_err!(ffi::gpgme_op_decrypt_ext( + convert_err(ffi::gpgme_op_decrypt_ext( self.as_raw(), flags.bits(), cipher.borrow_mut().as_raw(), plain.borrow_mut().as_raw(), - )); + ))?; } Ok(self.get_result().unwrap()) } @@ -1855,11 +1805,11 @@ impl Context { let mut cipher = ciphertext.into_data()?; let mut plain = plaintext.into_data()?; unsafe { - return_err!(ffi::gpgme_op_decrypt_verify( + convert_err(ffi::gpgme_op_decrypt_verify( self.as_raw(), cipher.borrow_mut().as_raw(), plain.borrow_mut().as_raw(), - )) + ))?; } Ok((self.get_result().unwrap(), self.get_result().unwrap())) } @@ -1896,7 +1846,7 @@ impl Context { let iversion = iversion .as_ref() .map_or(ptr::null(), |s| s.as_ref().as_ptr()); - return_err!(ffi::gpgme_op_query_swdb(self.as_raw(), name, iversion, 0)); + convert_err(ffi::gpgme_op_query_swdb(self.as_raw(), name, iversion, 0))?; } Ok(self.get_result().unwrap()) } @@ -1910,13 +1860,12 @@ impl Context { { let mut dst = dst.into_data()?; unsafe { - return_err!(ffi::gpgme_op_getauditlog( + convert_err(ffi::gpgme_op_getauditlog( self.as_raw(), dst.borrow_mut().as_raw(), flags.bits(), - )); + )) } - Ok(()) } fn get_result(&self) -> Option { @@ -1959,7 +1908,7 @@ impl Keys<'_, D> { pub fn finish(self) -> Result { let this = ManuallyDrop::new(self); unsafe { - return_err!(ffi::gpgme_op_keylist_end(this.ctx.as_raw())); + convert_err(ffi::gpgme_op_keylist_end(this.ctx.as_raw()))?; } Ok(this.ctx.get_result().unwrap()) } @@ -1976,11 +1925,11 @@ impl<'ctx> Keys<'ctx, ()> { { let mut src = src.into_data()?; unsafe { - return_err!(ffi::gpgme_op_keylist_from_data_start( + convert_err(ffi::gpgme_op_keylist_from_data_start( ctx.as_raw(), src.borrow_mut().as_raw(), 0, - )); + ))?; } Ok(Keys { _src: src, ctx }) } diff --git a/src/data.rs b/src/data.rs index 58194931..c07f44e4 100644 --- a/src/data.rs +++ b/src/data.rs @@ -18,7 +18,7 @@ use ffi::{self, gpgme_off_t}; use libc; use static_assertions::{assert_impl_all, assert_not_impl_any}; -use crate::{error::return_err, utils::CStrArgument, Error, NonNull, Result}; +use crate::{utils::convert_err, utils::CStrArgument, Error, NonNull, Result}; assert_impl_all!(Data<'_>: Send); assert_not_impl_any!(Data<'_>: Sync); @@ -142,7 +142,7 @@ impl<'data> Data<'data> { crate::init(); unsafe { let mut data = ptr::null_mut(); - return_err!(ffi::gpgme_data_new(&mut data)); + convert_err(ffi::gpgme_data_new(&mut data))?; Ok(Data::from_raw(data)) } } @@ -158,11 +158,11 @@ impl<'data> Data<'data> { let path = path.into_cstr(); unsafe { let mut data = ptr::null_mut(); - return_err!(ffi::gpgme_data_new_from_file( + convert_err(ffi::gpgme_data_new_from_file( &mut data, path.as_ref().as_ptr(), 1, - )); + ))?; Ok(Data::from_raw(data)) } } @@ -178,7 +178,7 @@ impl<'data> Data<'data> { unsafe { let (buf, len) = (bytes.as_ptr(), bytes.len()); let mut data = ptr::null_mut(); - return_err!(ffi::gpgme_data_new_from_mem(&mut data, buf.cast(), len, 1)); + convert_err(ffi::gpgme_data_new_from_mem(&mut data, buf.cast(), len, 1))?; Ok(Data::from_raw(data)) } } @@ -194,7 +194,7 @@ impl<'data> Data<'data> { unsafe { let (buf, len) = (buf.as_ptr(), buf.len()); let mut data = ptr::null_mut(); - return_err!(ffi::gpgme_data_new_from_mem(&mut data, buf.cast(), len, 0)); + convert_err(ffi::gpgme_data_new_from_mem(&mut data, buf.cast(), len, 0))?; Ok(Data::from_raw(data)) } } @@ -221,7 +221,7 @@ impl<'data> Data<'data> { crate::init(); unsafe { let mut data = ptr::null_mut(); - return_err!(ffi::gpgme_data_new_from_fd(&mut data, file.as_raw_fd())); + convert_err(ffi::gpgme_data_new_from_fd(&mut data, file.as_raw_fd()))?; Ok(Data::from_raw(data)) } } @@ -232,7 +232,7 @@ impl<'data> Data<'data> { pub unsafe fn from_raw_file(file: *mut libc::FILE) -> Result { crate::init(); let mut data = ptr::null_mut(); - return_err!(ffi::gpgme_data_new_from_stream(&mut data, file)); + convert_err(ffi::gpgme_data_new_from_stream(&mut data, file))?; Ok(Data::from_raw(data)) } @@ -244,11 +244,9 @@ impl<'data> Data<'data> { let src = Box::into_raw(Box::new(CallbackWrapper { inner: src, cbs })); let cbs = ptr::addr_of_mut!((*src).cbs); let mut data = ptr::null_mut(); - let result = ffi::gpgme_data_new_from_cbs(&mut data, cbs, src.cast()); - if result == 0 { - Ok(Data::from_raw(data)) - } else { - Err(WrappedError(Error::new(result), Box::from_raw(src).inner)) + match convert_err(ffi::gpgme_data_new_from_cbs(&mut data, cbs, src.cast())) { + Ok(()) => Ok(Data::from_raw(data)), + Err(e) => Err(WrappedError(e, Box::from_raw(src).inner)), } } @@ -359,10 +357,7 @@ impl<'data> Data<'data> { /// [`gpgme_data_set_file_name`](https://www.gnupg.org/documentation/manuals/gpgme/Data-Buffer-Meta_002dData.html#index-gpgme_005fdata_005fset_005ffile_005fname) #[inline] pub fn clear_filename(&mut self) -> Result<()> { - unsafe { - return_err!(ffi::gpgme_data_set_file_name(self.as_raw(), ptr::null())); - } - Ok(()) + unsafe { convert_err(ffi::gpgme_data_set_file_name(self.as_raw(), ptr::null())) } } /// Upstream documentation: @@ -371,12 +366,11 @@ impl<'data> Data<'data> { pub fn set_filename(&mut self, name: impl CStrArgument) -> Result<()> { let name = name.into_cstr(); unsafe { - return_err!(ffi::gpgme_data_set_file_name( + convert_err(ffi::gpgme_data_set_file_name( self.as_raw(), name.as_ref().as_ptr(), - )); + )) } - Ok(()) } /// Upstream documentation: @@ -390,8 +384,7 @@ impl<'data> Data<'data> { /// [`gpgme_data_set_encoding`](https://www.gnupg.org/documentation/manuals/gpgme/Data-Buffer-Meta_002dData.html#index-gpgme_005fdata_005fset_005fencoding) #[inline] pub fn set_encoding(&mut self, enc: Encoding) -> Result<()> { - unsafe { return_err!(ffi::gpgme_data_set_encoding(self.as_raw(), enc.raw())) } - Ok(()) + unsafe { convert_err(ffi::gpgme_data_set_encoding(self.as_raw(), enc.raw())) } } /// Upstream documentation: @@ -401,13 +394,12 @@ impl<'data> Data<'data> { let name = name.into_cstr(); let value = value.into_cstr(); unsafe { - return_err!(ffi::gpgme_data_set_flag( + convert_err(ffi::gpgme_data_set_flag( self.as_raw(), name.as_ref().as_ptr(), value.as_ref().as_ptr(), - )); + )) } - Ok(()) } /// Upstream documentation: diff --git a/src/engine.rs b/src/engine.rs index c4277071..b49d3614 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -9,7 +9,7 @@ use std::{ use ffi; -use crate::{error::return_err, NonNull, Protocol, Result}; +use crate::{utils::convert_err, NonNull, Protocol, Result}; /// Upstream documentation: /// [`gpgme_engine_info_t`](https://www.gnupg.org/documentation/manuals/gpgme/Engine-Information.html#index-gpgme_005fengine_005finfo_005ft) @@ -123,7 +123,7 @@ impl EngineInfoGuard { let lock = lock.read().expect("engine info lock was poisoned"); unsafe { let mut info = ptr::null_mut(); - return_err!(ffi::gpgme_get_engine_info(&mut info)); + convert_err(ffi::gpgme_get_engine_info(&mut info))?; } Ok(EngineInfoGuard(lock)) } diff --git a/src/lib.rs b/src/lib.rs index 87c39270..9e9adc9a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,10 @@ use std::{ sync::{Mutex, OnceLock, RwLock}, }; -use self::{engine::EngineInfoGuard, error::return_err, utils::CStrArgument}; +use self::{ + engine::EngineInfoGuard, + utils::{convert_err, CStrArgument}, +}; #[doc(inline)] #[allow(deprecated)] @@ -259,13 +262,13 @@ impl Gpgme { } } - /// Checks that the engine implementing the specified protocol is supported by the library. + /// Checks that the engine implementing the specified protocol is available. /// /// Upstream documentation: /// [`gpgme_engine_check_version`](https://www.gnupg.org/documentation/manuals/gpgme/Engine-Version-Check.html#index-gpgme_005fengine_005fcheck_005fversion) pub fn check_engine_version(&self, proto: Protocol) -> Result<()> { unsafe { - return_err!(ffi::gpgme_engine_check_version(proto.raw())); + convert_err(ffi::gpgme_engine_check_version(proto.raw()))?; } Ok(()) } @@ -300,12 +303,12 @@ impl Gpgme { let home_dir = self .get_engine_info(proto) .as_ref() - .map_or(ptr::null(), |e| (*e).home_dir); - return_err!(ffi::gpgme_set_engine_info( + .map_or(ptr::null(), |x| x.home_dir); + convert_err(ffi::gpgme_set_engine_info( proto.raw(), path.as_ref().as_ptr(), home_dir, - )); + ))?; } Ok(()) } @@ -321,12 +324,12 @@ impl Gpgme { let path = self .get_engine_info(proto) .as_ref() - .map_or(ptr::null(), |e| (*e).file_name); - return_err!(ffi::gpgme_set_engine_info( + .map_or(ptr::null(), |x| x.file_name); + convert_err(ffi::gpgme_set_engine_info( proto.raw(), path, home_dir.as_ref().as_ptr(), - )); + ))?; } Ok(()) } @@ -351,7 +354,7 @@ impl Gpgme { .engine_lock .write() .expect("engine info lock was poisoned"); - return_err!(ffi::gpgme_set_engine_info(proto.raw(), path, home_dir)); + convert_err(ffi::gpgme_set_engine_info(proto.raw(), path, home_dir))?; } Ok(()) } diff --git a/src/results.rs b/src/results.rs index d9288b0d..90a95e93 100644 --- a/src/results.rs +++ b/src/results.rs @@ -12,8 +12,9 @@ use ffi; use libc; use crate::{ - error::return_err, notation::SignatureNotations, Context, Error, HashAlgorithm, ImportFlags, - KeyAlgorithm, NonNull, OpResult, Result, SignMode, SignatureSummary, Validity, + notation::SignatureNotations, utils::convert_err, Context, Error, HashAlgorithm, ImportFlags, + KeyAlgorithm, NonNull, OpResult, Result, SignMode, SignatureNotation, SignatureSummary, + Validity, }; macro_rules! impl_result { @@ -300,10 +301,7 @@ impl<'result> Import<'result> { #[inline] pub fn result(&self) -> Result<()> { - unsafe { - return_err!((*self.as_raw()).result); - Ok(()) - } + unsafe { convert_err((*self.as_raw()).result) } } #[inline] @@ -462,10 +460,7 @@ impl<'result> Recipient<'result> { #[inline] pub fn status(&self) -> Result<()> { - unsafe { - return_err!((*self.as_raw()).status); - Ok(()) - } + unsafe { convert_err((*self.as_raw()).status) } } } @@ -646,10 +641,7 @@ impl<'result> Signature<'result> { #[inline] pub fn status(&self) -> Result<()> { - unsafe { - return_err!((*self.as_raw()).status); - Ok(()) - } + unsafe { convert_err((*self.as_raw()).status) } } #[inline] diff --git a/src/utils.rs b/src/utils.rs index 48f07e54..c96a4b02 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -22,9 +22,7 @@ macro_rules! impl_wrapper { #[inline] pub fn into_raw(self) -> $T { - let raw = self.as_raw(); - ::std::mem::forget(self); - raw + ::std::mem::ManuallyDrop::new(self).as_raw() } }; } @@ -77,7 +75,7 @@ macro_rules! ffi_enum_wrapper { impl $Name { #[inline] - pub unsafe fn from_raw(raw: $T) -> $Name { + pub fn from_raw(raw: $T) -> $Name { $(if raw == $Value { $Name::$Item } else )+ { @@ -116,7 +114,7 @@ macro_rules! ffi_enum_wrapper { impl $Name { #[inline] - pub unsafe fn from_raw(raw: $T) -> $Name { + pub fn from_raw(raw: $T) -> $Name { $(if raw == $Value { $Name::$Item } else )+ { @@ -177,3 +175,11 @@ impl Ptr for *mut T { impl Ptr for *const T { type Inner = T; } + +#[inline(always)] +pub(crate) fn convert_err(err: ffi::gpgme_error_t) -> Result<(), Error> { + match err { + 0 => Ok(()), + _ => Err(Error::new(err)), + } +}