11use crate :: {
2+ async_map_somes:: async_map_somes,
23 encrypted_table:: { TableAttribute , TableEntry } ,
34 traits:: { ReadConversionError , WriteConversionError } ,
45 Decryptable ,
@@ -62,7 +63,8 @@ impl SealedTableEntry {
6263 plaintext_attributes,
6364 } = spec;
6465
65- let mut plaintext_items: Vec < Vec < & TableAttribute > > = Vec :: with_capacity ( items. len ( ) ) ;
66+ let mut plaintext_items: Vec < Vec < Option < & TableAttribute > > > =
67+ Vec :: with_capacity ( items. len ( ) ) ;
6668 let mut decryptable_items = Vec :: with_capacity ( items. len ( ) * protected_attributes. len ( ) ) ;
6769
6870 for item in items. iter ( ) {
@@ -77,9 +79,11 @@ impl SealedTableEntry {
7779 } ) ;
7880
7981 attribute
80- . ok_or_else ( || SealError :: MissingAttribute ( name. to_string ( ) ) ) ?
81- . as_encrypted_record ( )
82- . ok_or_else ( || SealError :: InvalidCiphertext ( name. to_string ( ) ) )
82+ . map ( |x| {
83+ x. as_encrypted_record ( )
84+ . ok_or_else ( || SealError :: InvalidCiphertext ( name. to_string ( ) ) )
85+ } )
86+ . transpose ( )
8387 } )
8488 . collect :: < Result < Vec < _ > , _ > > ( ) ?;
8589
@@ -97,23 +101,21 @@ impl SealedTableEntry {
97101 _ => name,
98102 } ;
99103
100- item. inner ( )
101- . attributes
102- . get ( attr)
103- . ok_or ( SealError :: MissingAttribute ( attr. to_string ( ) ) )
104+ item. inner ( ) . attributes . get ( attr)
104105 } )
105- . collect :: < Result < Vec < & TableAttribute > , SealError > > ( ) ? ;
106+ . collect :: < Vec < Option < & TableAttribute > > > ( ) ;
106107
107108 plaintext_items. push ( unprotected) ;
108109 }
109110
110- let decrypted = cipher. decrypt ( decryptable_items ) . await ?;
111+ let decrypted = async_map_somes ( decryptable_items , |items| cipher. decrypt ( items ) ) . await ?;
111112
112- let decrypted_iter: & mut dyn Iterator < Item = & [ Plaintext ] > =
113+ let decrypted_iter: & mut dyn Iterator < Item = & [ Option < Plaintext > ] > =
113114 if protected_attributes. len ( ) > 0 {
114115 & mut decrypted. chunks_exact ( protected_attributes. len ( ) )
115116 } else {
116- & mut std:: iter:: repeat_with :: < & [ Plaintext ] , _ > ( || & [ ] ) . take ( plaintext_items. len ( ) )
117+ & mut std:: iter:: repeat_with :: < & [ Option < Plaintext > ] , _ > ( || & [ ] )
118+ . take ( plaintext_items. len ( ) )
117119 } ;
118120
119121 let unsealed = decrypted_iter
@@ -122,13 +124,17 @@ impl SealedTableEntry {
122124 let mut unsealed = Unsealed :: new ( ) ;
123125
124126 for ( name, plaintext) in protected_attributes. iter ( ) . zip ( decrypted_plaintext) {
125- unsealed. add_protected ( name. to_string ( ) , plaintext. clone ( ) ) ;
127+ if let Some ( plaintext) = plaintext {
128+ unsealed. add_protected ( name. to_string ( ) , plaintext. clone ( ) ) ;
129+ }
126130 }
127131
128132 for ( name, plaintext) in
129133 plaintext_attributes. iter ( ) . zip ( plaintext_items. into_iter ( ) )
130134 {
131- unsealed. add_unprotected ( name. to_string ( ) , plaintext. clone ( ) ) ;
135+ if let Some ( plaintext) = plaintext {
136+ unsealed. add_unprotected ( name. to_string ( ) , plaintext. clone ( ) ) ;
137+ }
132138 }
133139
134140 unsealed
0 commit comments