@@ -12,102 +12,96 @@ extern crate proc_macro;
12
12
13
13
use proc_macro2:: TokenStream ;
14
14
use quote:: quote;
15
- use syn:: { Attribute , Meta , NestedMeta } ;
16
15
use synstructure:: { decl_derive, AddBounds } ;
17
16
18
- /// Name of the digest attribute
19
- const DIGEST_ATTRIBUTE_NAME : & str = "digest" ;
20
-
21
17
/// Derive the `Signer` trait for `DigestSigner` types
22
18
fn derive_signer ( mut s : synstructure:: Structure ) -> TokenStream {
23
- let digest_path = DigestAttribute :: parse ( & s) . into_meta ( "Signer" ) ;
24
-
25
19
s. add_bounds ( AddBounds :: None ) ;
26
20
s. gen_impl ( quote ! {
27
21
gen impl <S > signature:: Signer <S > for @Self
28
22
where
29
- S : Signature ,
30
- Self : signature:: DigestSigner <#digest_path , S >
23
+ S : signature :: DigestSignature ,
24
+ Self : signature:: DigestSigner <S :: Digest , S >
31
25
{
32
26
fn try_sign( & self , msg: & [ u8 ] ) -> Result <S , signature:: Error > {
33
- self . try_sign_digest( #digest_path :: new( ) . chain( msg) )
27
+ self . try_sign_digest( S :: Digest :: new( ) . chain( msg) )
34
28
}
35
29
}
36
30
} )
37
31
}
38
- decl_derive ! ( [ Signer , attributes ( digest ) ] => derive_signer) ;
32
+ decl_derive ! ( [ Signer ] => derive_signer) ;
39
33
40
34
/// Derive the `Verifier` trait for `DigestVerifier` types
41
35
fn derive_verifier ( mut s : synstructure:: Structure ) -> TokenStream {
42
- let digest_path = DigestAttribute :: parse ( & s) . into_meta ( "Verifier" ) ;
43
-
44
36
s. add_bounds ( AddBounds :: None ) ;
45
37
s. gen_impl ( quote ! {
46
38
gen impl <S > signature:: Verifier <S > for @Self
47
39
where
48
- S : Signature ,
49
- Self : signature:: DigestVerifier <#digest_path , S >
40
+ S : signature :: DigestSignature ,
41
+ Self : signature:: DigestVerifier <S :: Digest , S >
50
42
{
51
43
fn verify( & self , msg: & [ u8 ] , signature: & S ) -> Result <( ) , signature:: Error > {
52
- self . verify_digest( #digest_path :: new( ) . chain( msg) , signature)
44
+ self . verify_digest( S :: Digest :: new( ) . chain( msg) , signature)
53
45
}
54
46
}
55
47
} )
56
48
}
57
- decl_derive ! ( [ Verifier , attributes( digest) ] => derive_verifier) ;
58
-
59
- /// The `#[digest(...)]` attribute passed to the proc macro
60
- #[ derive( Default ) ]
61
- struct DigestAttribute {
62
- digest : Option < Meta > ,
63
- }
64
-
65
- impl DigestAttribute {
66
- /// Parse attributes from the incoming AST
67
- fn parse ( s : & synstructure:: Structure < ' _ > ) -> Self {
68
- let mut result = Self :: default ( ) ;
69
-
70
- for v in s. variants ( ) . iter ( ) {
71
- for attr in v. ast ( ) . attrs . iter ( ) {
72
- result. parse_attr ( attr) ;
49
+ decl_derive ! ( [ Verifier ] => derive_verifier) ;
50
+
51
+ #[ cfg( test) ]
52
+ mod tests {
53
+ use super :: * ;
54
+ use synstructure:: test_derive;
55
+
56
+ #[ test]
57
+ fn signer ( ) {
58
+ test_derive ! {
59
+ derive_signer {
60
+ struct MySigner <C : EllipticCurve > {
61
+ scalar: Scalar <C :: ScalarSize >
62
+ }
73
63
}
64
+ expands to {
65
+ #[ allow( non_upper_case_globals) ]
66
+ const _DERIVE_signature_Signer_S_FOR_MySigner: ( ) = {
67
+ impl <S , C : EllipticCurve > signature:: Signer <S > for MySigner <C >
68
+ where
69
+ S : signature:: DigestSignature ,
70
+ Self : signature:: DigestSigner <S :: Digest , S >
71
+ {
72
+ fn try_sign( & self , msg: & [ u8 ] ) -> Result <S , signature:: Error > {
73
+ self . try_sign_digest( S :: Digest :: new( ) . chain( msg) )
74
+ }
75
+ }
76
+ } ;
77
+ }
78
+ no_build // tests in `signature-crate/tests`
74
79
}
75
-
76
- result
77
80
}
78
81
79
- /// Parse attribute and handle `#[digest(...)]` attribute
80
- fn parse_attr ( & mut self , attr : & Attribute ) {
81
- let meta = attr
82
- . parse_meta ( )
83
- . unwrap_or_else ( |e| panic ! ( "error parsing digest attribute: {:?} ({})" , attr, e) ) ;
84
-
85
- if let Meta :: List ( list) = meta {
86
- if !list. path . is_ident ( DIGEST_ATTRIBUTE_NAME ) {
87
- return ;
82
+ #[ test]
83
+ fn verifier ( ) {
84
+ test_derive ! {
85
+ derive_verifier {
86
+ struct MyVerifier <C : EllipticCurve > {
87
+ point: UncompressedPoint <C >
88
+ }
88
89
}
89
-
90
- for nested_meta in & list. nested {
91
- if let NestedMeta :: Meta ( meta) = nested_meta {
92
- if self . digest . is_none ( ) {
93
- self . digest = Some ( meta. to_owned ( ) ) ;
94
- } else {
95
- panic ! ( "multiple digest attributes in custom derive" ) ;
90
+ expands to {
91
+ #[ allow( non_upper_case_globals) ]
92
+ const _DERIVE_signature_Verifier_S_FOR_MyVerifier: ( ) = {
93
+ impl <S , C : EllipticCurve > signature:: Verifier <S > for MyVerifier <C >
94
+ where
95
+ S : signature:: DigestSignature ,
96
+ Self : signature:: DigestVerifier <S :: Digest , S >
97
+ {
98
+ fn verify( & self , msg: & [ u8 ] , signature: & S ) -> Result <( ) , signature:: Error > {
99
+ self . verify_digest( S :: Digest :: new( ) . chain( msg) , signature)
100
+ }
96
101
}
97
- } else {
98
- panic ! ( "malformed digest attribute: {:?}" , nested_meta) ;
99
- }
102
+ } ;
100
103
}
104
+ no_build // tests in `signature-crate/tests`
101
105
}
102
106
}
103
-
104
- /// Convert parsed attributes into the recovered `Meta`
105
- fn into_meta ( self , trait_name : & str ) -> Meta {
106
- self . digest . unwrap_or_else ( || {
107
- panic ! (
108
- "#[digest(...)] attribute is mandatory when deriving {}" ,
109
- trait_name
110
- )
111
- } )
112
- }
113
107
}
0 commit comments