@@ -182,13 +182,16 @@ impl AsRef<[u8]> for Fingerprint {
182182
183183/// Models the interactions that can be done on a pgp key
184184pub trait Key {
185- /// returns a list of names associated with the key
185+ /// Returns a list of names associated with the key.
186186 fn user_id_names ( & self ) -> Vec < String > ;
187187
188- /// returns the keys fingerprint
188+ /// Returns the keys fingerprint.
189+ ///
190+ /// # Errors
191+ /// If a gpg context can't be created, and the gpg backend is chosen.
189192 fn fingerprint ( & self ) -> Result < Fingerprint > ;
190193
191- /// returns if the key isn't usable
194+ /// Returns if the key isn't usable.
192195 fn is_not_usable ( & self ) -> bool ;
193196}
194197
@@ -228,6 +231,7 @@ pub trait Crypto {
228231 /// Will return `Err` if decryption fails, for example if the current user isn't the
229232 /// recipient of the message.
230233 fn decrypt_string ( & self , ciphertext : & [ u8 ] ) -> Result < String > ;
234+
231235 /// Encrypts a string
232236 /// # Errors
233237 /// Will return `Err` if encryption fails, for example if the current users key
@@ -256,6 +260,9 @@ pub trait Crypto {
256260 ) -> std:: result:: Result < SignatureStatus , VerificationError > ;
257261
258262 /// Returns true if a recipient is in the user's keyring.
263+ ///
264+ /// # Errors
265+ /// If a gpg context can't be created, and the gpg backend is chosen.
259266 fn is_key_in_keyring ( & self , recipient : & Recipient ) -> Result < bool > ;
260267
261268 /// Pull keys from the keyserver for those recipients.
@@ -273,7 +280,7 @@ pub trait Crypto {
273280 /// Will return `Err` if `key_id` didn't correspond to a key.
274281 fn get_key ( & self , key_id : & str ) -> Result < Box < dyn Key > > ;
275282
276- /// Returns a map from key fingerprints to OwnerTrustLevel's
283+ /// Returns a map from key fingerprints to ` OwnerTrustLevel` 's
277284 /// # Errors
278285 /// Will return `Err` on failure to obtain trust levels.
279286 fn get_all_trust_items ( & self ) -> Result < HashMap < Fingerprint , OwnerTrustLevel > > ;
@@ -294,7 +301,7 @@ impl Crypto for GpgMe {
294301 let mut ctx = gpgme:: Context :: from_protocol ( gpgme:: Protocol :: OpenPgp ) ?;
295302 let mut output = Vec :: new ( ) ;
296303 ctx. decrypt ( ciphertext, & mut output) ?;
297- let result = String :: from_utf8 ( output. to_vec ( ) ) ?;
304+ let result = String :: from_utf8 ( output. clone ( ) ) ?;
298305 output. zeroize ( ) ;
299306 Ok ( result)
300307 }
@@ -514,7 +521,7 @@ struct Helper<'a> {
514521 key_ring : & ' a HashMap < Fingerprint , Arc < Cert > > ,
515522 /// This is all the certificates that are allowed to sign something
516523 public_keys : Vec < Arc < Cert > > ,
517- /// context if talking to gpg_agent for example
524+ /// context if talking to ` gpg_agent` for example
518525 ctx : Option < sequoia_gpg_agent:: gnupg:: Context > ,
519526 /// to do verification or not
520527 do_signature_verification : bool ,
@@ -555,7 +562,7 @@ impl VerificationHelper for Helper<'_> {
555562
556563fn find (
557564 key_ring : & HashMap < Fingerprint , Arc < Cert > > ,
558- recipient : & Option < KeyHandle > ,
565+ recipient : Option < & KeyHandle > ,
559566) -> Result < Arc < Cert > > {
560567 let recipient = recipient. as_ref ( ) . ok_or ( Error :: Generic ( "No recipient" ) ) ?;
561568
@@ -576,7 +583,7 @@ fn find(
576583 return Err ( Error :: Generic ( "unknown fingerprint version" ) ) ;
577584 }
578585 _ => { }
579- } ;
586+ }
580587 }
581588 KeyHandle :: KeyID ( key_id) => match key_id {
582589 KeyID :: Long ( bytes) => {
@@ -608,7 +615,7 @@ impl DecryptionHelper for Helper<'_> {
608615 // we don't know which key is the users own key, so lets try them all
609616 let mut selected_fingerprint: Option < Arc < Cert > > = None ;
610617 for pkesk in pkesks {
611- if let Ok ( cert) = find ( self . key_ring , & pkesk. recipient ( ) ) {
618+ if let Ok ( cert) = find ( self . key_ring , pkesk. recipient ( ) . as_ref ( ) ) {
612619 let key = cert. primary_key ( ) . key ( ) ;
613620 let mut pair = sequoia_gpg_agent:: KeyPair :: new_for_gnupg_context (
614621 self . ctx
@@ -648,8 +655,7 @@ impl DecryptionHelper for Helper<'_> {
648655 for pkesk in pkesks {
649656 if pkesk
650657 . decrypt ( & mut pair, sym_algo)
651- . map ( |( algo, sk) | decrypt ( algo, & sk) )
652- . unwrap_or ( false )
658+ . is_some_and ( |( algo, sk) | decrypt ( algo, & sk) )
653659 {
654660 return Ok ( Some (
655661 ( * self
@@ -688,10 +694,7 @@ impl Key for SequoiaKey {
688694 fn is_not_usable ( & self ) -> bool {
689695 let p = sequoia_openpgp:: policy:: StandardPolicy :: new ( ) ;
690696
691- let policy = match self . cert . with_policy ( & p, None ) {
692- Err ( _) => return true ,
693- Ok ( p) => p,
694- } ;
697+ let Ok ( policy) = self . cert . with_policy ( & p, None ) else { return true } ;
695698
696699 self . cert . revocation_status ( & p, None ) != RevocationStatus :: NotAsFarAsWeKnow
697700 || policy. alive ( ) . is_err ( )
@@ -737,6 +740,7 @@ impl Sequoia {
737740 } )
738741 }
739742
743+ #[ must_use]
740744 pub fn from_values (
741745 user_key_id : Fingerprint ,
742746 key_ring : HashMap < Fingerprint , Arc < Cert > > ,
@@ -756,26 +760,25 @@ impl Sequoia {
756760 let mut result = vec ! [ ] ;
757761
758762 for recipient in input {
759- match & recipient. fingerprint {
760- Some ( fp ) => match self . key_ring . get ( fp) {
763+ if let Some ( fp ) = & recipient. fingerprint {
764+ match self . key_ring . get ( fp) {
761765 Some ( cert) => result. push ( cert. clone ( ) ) ,
762766 None => {
763767 return Err ( Error :: GenericDyn ( format ! (
764768 "Recipient with key id {} not found" ,
765769 recipient. key_id
766770 ) ) ) ;
767771 }
768- } ,
769- None => {
770- let kh: KeyHandle = recipient. key_id . parse ( ) ?;
771-
772- for cert in self . key_ring . values ( ) {
773- if cert. key_handle ( ) . aliases ( & kh) {
774- result. push ( cert. clone ( ) ) ;
775- }
772+ }
773+ } else {
774+ let kh: KeyHandle = recipient. key_id . parse ( ) ?;
775+
776+ for cert in self . key_ring . values ( ) {
777+ if cert. key_handle ( ) . aliases ( & kh) {
778+ result. push ( cert. clone ( ) ) ;
776779 }
777780 }
778- } ;
781+ }
779782 }
780783
781784 Ok ( result)
0 commit comments