@@ -6,7 +6,10 @@ pub use self::{
66} ;
77use crate :: {
88 crypto:: * ,
9- traits:: { Decryptable , ReadConversionError , Searchable , WriteConversionError } ,
9+ traits:: {
10+ Decryptable , PrimaryKey , PrimaryKeyParts , ReadConversionError , Searchable ,
11+ WriteConversionError ,
12+ } ,
1013} ;
1114use aws_sdk_dynamodb:: {
1215 types:: { AttributeValue , Delete , Put , TransactWriteItem } ,
@@ -112,14 +115,13 @@ impl EncryptedTable {
112115 QueryBuilder :: new ( self )
113116 }
114117
115- pub async fn get < T > ( & self , pk : & str , sk : Option < & str > ) -> Result < Option < T > , GetError >
118+ pub async fn get < T > ( & self , k : impl Into < T :: PrimaryKey > ) -> Result < Option < T > , GetError >
116119 where
117120 T : Decryptable ,
118121 {
119- let pk = encrypt_partition_key ( pk, & self . cipher ) ?;
120- let sk = sk
121- . map ( |sk| format ! ( "{}#{}" , T :: type_name( ) , sk) )
122- . unwrap_or_else ( || T :: type_name ( ) . to_string ( ) ) ;
122+ let PrimaryKeyParts { pk, sk } = k. into ( ) . into_parts ( T :: type_name ( ) ) ;
123+
124+ let pk = encrypt_partition_key ( & pk, & self . cipher ) ?;
123125
124126 let result = self
125127 . db
@@ -141,15 +143,10 @@ impl EncryptedTable {
141143 }
142144
143145 // TODO: create PrimaryKey abstraction
144- pub async fn delete < E : Searchable > (
145- & self ,
146- pk : & str ,
147- sk : Option < & str > ,
148- ) -> Result < ( ) , DeleteError > {
149- let pk = AttributeValue :: S ( encrypt_partition_key ( pk, & self . cipher ) ?) ;
150- let sk = sk
151- . map ( |sk| format ! ( "{}#{}" , E :: type_name( ) , sk) )
152- . unwrap_or_else ( || E :: type_name ( ) . to_string ( ) ) ;
146+ pub async fn delete < E : Searchable > ( & self , k : impl Into < E :: PrimaryKey > ) -> Result < ( ) , DeleteError > {
147+ let PrimaryKeyParts { pk, sk } = k. into ( ) . into_parts ( E :: type_name ( ) ) ;
148+
149+ let pk = AttributeValue :: S ( encrypt_partition_key ( & pk, & self . cipher ) ?) ;
153150
154151 let sk_to_delete = all_index_keys :: < E > ( & sk) . into_iter ( ) . into_iter ( ) . chain ( [ sk] ) ;
155152
0 commit comments