@@ -19,7 +19,7 @@ use tokio::{
1919
2020use crate :: {
2121 AsAttributes , Key , Secret ,
22- file:: { Error , InvalidItemError , Item , LockedItem , LockedKeyring , UnlockedItem , api} ,
22+ file:: { Error , InvalidItemError , LockedItem , LockedKeyring , UnlockedItem , api} ,
2323} ;
2424
2525/// Definition for batch item creation: (label, attributes, secret, replace)
@@ -243,45 +243,55 @@ impl UnlockedKeyring {
243243 self . keyring . read ( ) . await . items . len ( )
244244 }
245245
246- /// Retrieve the list of available [`UnlockedItem`]s.
246+ /// Retrieve all items including those that cannot be decrypted.
247+ ///
248+ /// Returns a [`Vec`] where each element is either an [`UnlockedItem`] or an
249+ /// [`InvalidItemError`] for items that failed to decrypt.
247250 ///
248- /// If items cannot be decrypted, [`InvalidItemError`]s are returned for
249- /// them instead of [`UnlockedItem`]s.
251+ /// Use this method when you need to know about or handle decryption
252+ /// failures. For most use cases, [`items()`](Self::items) is more
253+ /// convenient as it only returns successfully decrypted items.
250254 #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip( self ) ) ) ]
251- pub async fn items ( & self ) -> Result < Vec < Result < Item , InvalidItemError > > , Error > {
255+ pub async fn all_items ( & self ) -> Result < Vec < Result < UnlockedItem , InvalidItemError > > , Error > {
252256 let key = self . derive_key ( ) . await ?;
253257 let keyring = self . keyring . read ( ) . await ;
254258
255259 #[ cfg( feature = "tracing" ) ]
256- let _span = tracing:: debug_span!( "decrypt " , total_items = keyring. items. len( ) ) ;
260+ let _span = tracing:: debug_span!( "decrypt_all " , total_items = keyring. items. len( ) ) ;
257261
258262 Ok ( keyring
259263 . items
260264 . iter ( )
261265 . map ( |e| {
262- ( * e) . clone ( )
263- . decrypt ( & key)
264- . map_err ( |err| {
265- InvalidItemError :: new (
266- err,
267- e. hashed_attributes . keys ( ) . map ( |x| x. to_string ( ) ) . collect ( ) ,
268- )
269- } )
270- . map ( Item :: Unlocked )
266+ ( * e) . clone ( ) . decrypt ( & key) . map_err ( |err| {
267+ InvalidItemError :: new (
268+ err,
269+ e. hashed_attributes . keys ( ) . map ( |x| x. to_string ( ) ) . collect ( ) ,
270+ )
271+ } )
271272 } )
272273 . collect ( ) )
273274 }
274275
276+ /// Retrieve the list of available [`UnlockedItem`]s.
277+ ///
278+ /// Items that cannot be decrypted are silently skipped. Use
279+ /// [`all_items()`](Self::all_items) if you need access to decryption
280+ /// errors.
281+ #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip( self ) ) ) ]
282+ pub async fn items ( & self ) -> Result < Vec < UnlockedItem > , Error > {
283+ Ok ( self . all_items ( ) . await ?. into_iter ( ) . flatten ( ) . collect ( ) )
284+ }
285+
275286 /// Search items matching the attributes.
276287 #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip( self , attributes) ) ) ]
277- pub async fn search_items ( & self , attributes : & impl AsAttributes ) -> Result < Vec < Item > , Error > {
288+ pub async fn search_items (
289+ & self ,
290+ attributes : & impl AsAttributes ,
291+ ) -> Result < Vec < UnlockedItem > , Error > {
278292 let key = self . derive_key ( ) . await ?;
279293 let keyring = self . keyring . read ( ) . await ;
280- let results = keyring
281- . search_items ( attributes, & key) ?
282- . into_iter ( )
283- . map ( Item :: Unlocked )
284- . collect :: < Vec < Item > > ( ) ;
294+ let results = keyring. search_items ( attributes, & key) ?;
285295
286296 #[ cfg( feature = "tracing" ) ]
287297 tracing:: debug!( "Found {} matching items" , results. len( ) ) ;
@@ -291,13 +301,14 @@ impl UnlockedKeyring {
291301
292302 /// Find the first item matching the attributes.
293303 #[ cfg_attr( feature = "tracing" , tracing:: instrument( skip( self , attributes) ) ) ]
294- pub async fn lookup_item ( & self , attributes : & impl AsAttributes ) -> Result < Option < Item > , Error > {
304+ pub async fn lookup_item (
305+ & self ,
306+ attributes : & impl AsAttributes ,
307+ ) -> Result < Option < UnlockedItem > , Error > {
295308 let key = self . derive_key ( ) . await ?;
296309 let keyring = self . keyring . read ( ) . await ;
297310
298- keyring
299- . lookup_item ( attributes, & key)
300- . map ( |maybe_item| maybe_item. map ( Item :: Unlocked ) )
311+ keyring. lookup_item ( attributes, & key)
301312 }
302313
303314 /// Find the index in the list of items of the first item matching the
@@ -354,7 +365,7 @@ impl UnlockedKeyring {
354365 attributes : & impl AsAttributes ,
355366 secret : impl Into < Secret > ,
356367 replace : bool ,
357- ) -> Result < Item , Error > {
368+ ) -> Result < UnlockedItem , Error > {
358369 let item = {
359370 let key = self . derive_key ( ) . await ?;
360371 let mut keyring = self . keyring . write ( ) . await ;
@@ -375,7 +386,7 @@ impl UnlockedKeyring {
375386 Ok ( _) => {
376387 #[ cfg( feature = "tracing" ) ]
377388 tracing:: info!( "Successfully created item" ) ;
378- Ok ( Item :: Unlocked ( item) )
389+ Ok ( item)
379390 }
380391 }
381392 }
0 commit comments