@@ -9,109 +9,56 @@ pub(crate) fn derive_encryptable(input: DeriveInput) -> Result<TokenStream, syn:
99 . field_attributes ( & input) ?
1010 . build ( ) ?;
1111
12- let partition_key_field = settings. get_partition_key ( ) ;
13- let partition_key = format_ident ! ( "{partition_key_field}" ) ;
14- let type_name = settings. type_name . clone ( ) ;
15-
16- let sort_key_prefix = settings
17- . sort_key_prefix
18- . as_ref ( )
19- . map ( |x| quote ! { Some ( #x) } )
20- . unwrap_or_else ( || quote ! { None } ) ;
21-
2212 let protected_attributes = settings. protected_attributes ( ) ;
2313 let plaintext_attributes = settings. plaintext_attributes ( ) ;
24- let ident = settings. ident ( ) ;
2514
26- let is_partition_key_encrypted = protected_attributes. contains ( & partition_key_field. as_str ( ) ) ;
27- let is_sort_key_encrypted = settings
28- . sort_key_field
29- . as_ref ( )
30- . map ( |x| protected_attributes. contains ( & x. as_str ( ) ) )
31- . unwrap_or ( true ) ;
15+ let protected_attributes_cow = settings
16+ . protected_attributes ( )
17+ . into_iter ( )
18+ . map ( |x| quote ! { std:: borrow:: Cow :: Borrowed ( #x) } ) ;
19+
20+ let plaintext_attributes_cow = settings
21+ . plaintext_attributes ( )
22+ . into_iter ( )
23+ . map ( |x| quote ! { std:: borrow:: Cow :: Borrowed ( #x) } ) ;
24+
25+ let ident = settings. ident ( ) ;
3226
3327 let into_unsealed_impl = protected_attributes
3428 . iter ( )
3529 . map ( |attr| {
3630 let attr_ident = format_ident ! ( "{attr}" ) ;
3731
3832 quote ! {
39- . add_protected( #attr, |x| cipherstash_dynamodb:: traits:: Plaintext :: from( x . #attr_ident. to_owned( ) ) )
33+ unsealed . add_protected( #attr, cipherstash_dynamodb:: traits:: Plaintext :: from( self . #attr_ident. to_owned( ) ) ) ;
4034 }
4135 } )
4236 . chain ( plaintext_attributes. iter ( ) . map ( |attr| {
4337 let attr_ident = format_ident ! ( "{attr}" ) ;
4438
4539 quote ! {
46- . add_plaintext ( #attr, |x| cipherstash_dynamodb:: traits:: TableAttribute :: from( x . #attr_ident. clone( ) ) )
40+ unsealed . add_unprotected ( #attr, cipherstash_dynamodb:: traits:: TableAttribute :: from( self . #attr_ident. clone( ) ) ) ;
4741 }
4842 } ) ) ;
4943
50- let sort_key_impl = if let Some ( sort_key_field) = & settings. sort_key_field {
51- let sort_key_attr = format_ident ! ( "{sort_key_field}" ) ;
52-
53- quote ! {
54- if let Some ( prefix) = Self :: sort_key_prefix( ) {
55- format!( "{}#{}" , prefix, self . #sort_key_attr)
56- } else {
57- self . #sort_key_attr. to_string( )
58- }
59- }
60- } else {
61- quote ! { Self :: type_name( ) . into( ) }
62- } ;
63-
64- let primary_key_impl = if settings. sort_key_field . is_some ( ) {
65- quote ! { type PrimaryKey = cipherstash_dynamodb:: PkSk ; }
66- } else {
67- quote ! { type PrimaryKey = cipherstash_dynamodb:: Pk ; }
68- } ;
69-
7044 let expanded = quote ! {
7145 #[ automatically_derived]
7246 impl cipherstash_dynamodb:: traits:: Encryptable for #ident {
73- #primary_key_impl
74-
75- #[ inline]
76- fn type_name( ) -> & ' static str {
77- #type_name
78- }
79-
80- fn sort_key( & self ) -> String {
81- #sort_key_impl
82- }
83-
84- #[ inline]
85- fn sort_key_prefix( ) -> Option <& ' static str > {
86- #sort_key_prefix
87- }
88-
89- #[ inline]
90- fn is_partition_key_encrypted( ) -> bool {
91- #is_partition_key_encrypted
47+ fn protected_attributes( ) -> std:: borrow:: Cow <' static , [ std:: borrow:: Cow <' static , str >] > {
48+ std:: borrow:: Cow :: Borrowed ( & [ #( #protected_attributes_cow, ) * ] )
9249 }
9350
94- #[ inline]
95- fn is_sort_key_encrypted( ) -> bool {
96- #is_sort_key_encrypted
51+ fn plaintext_attributes( ) -> std:: borrow:: Cow <' static , [ std:: borrow:: Cow <' static , str >] > {
52+ std:: borrow:: Cow :: Borrowed ( & [ #( #plaintext_attributes_cow, ) * ] )
9753 }
9854
99- fn partition_key( & self ) -> String {
100- self . #partition_key. to_string( )
101- }
102-
103- fn protected_attributes( ) -> Vec <& ' static str > {
104- vec![ #( #protected_attributes, ) * ]
105- }
55+ #[ allow( clippy:: needless_question_mark) ]
56+ fn into_unsealed( self ) -> cipherstash_dynamodb:: crypto:: Unsealed {
57+ let mut unsealed = cipherstash_dynamodb:: crypto:: Unsealed :: new_with_descriptor( <Self as cipherstash_dynamodb:: traits:: Identifiable >:: type_name( ) ) ;
10658
107- fn plaintext_attributes( ) -> Vec <& ' static str > {
108- vec![ #( #plaintext_attributes, ) * ]
109- }
59+ #( #into_unsealed_impl) *
11060
111- #[ allow( clippy:: needless_question_mark) ]
112- fn into_sealer( self ) -> Result <cipherstash_dynamodb:: crypto:: Sealer <Self >, cipherstash_dynamodb:: crypto:: SealError > {
113- Ok ( cipherstash_dynamodb:: crypto:: Sealer :: new_with_descriptor( self , Self :: type_name( ) )
114- #( #into_unsealed_impl?) * )
61+ unsealed
11562 }
11663 }
11764 } ;
0 commit comments