File tree Expand file tree Collapse file tree 6 files changed +77
-2
lines changed
Expand file tree Collapse file tree 6 files changed +77
-2
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ name = "helium_proto"
1010path = " src/lib.rs"
1111
1212[workspace ]
13- members = [" beacon" ]
13+ members = [" beacon" , " msg-signature " , " msg-signature-macro " ]
1414
1515[workspace .dependencies ]
1616tonic = " 0.14"
@@ -30,9 +30,9 @@ tonic-prost = { workspace = true, optional = true }
3030bytes = { workspace = true }
3131prost = { workspace = true }
3232serde = { workspace = true }
33- serde_json = { workspace = true }
3433strum = { version = " 0.27" , features = [" derive" ] }
3534strum_macros = " 0.27"
35+ msg-signature = { path = " msg-signature" }
3636
3737[build-dependencies ]
3838tonic-prost-build = " 0.14"
Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ const MESSAGES: &[&str] = &[
3737macro_rules! config {
3838 ( $config: expr) => {
3939 $config
40+ . type_attribute( "." , "#[derive(msg_signature::MsgHasSignature)]" )
4041 . type_attribute( "." , "#[derive(serde::Serialize, serde::Deserialize)]" )
4142 . enum_attribute(
4243 ".helium.service_provider" ,
Original file line number Diff line number Diff line change 1+ [package ]
2+ name = " msg-signature-macro"
3+ version = " 0.1.0"
4+ edition = " 2024"
5+
6+ [lib ]
7+ proc-macro = true
8+
9+ [dependencies ]
10+ syn = { version = " 2" }
11+ quote = " 1"
Original file line number Diff line number Diff line change 1+ use proc_macro:: TokenStream ;
2+ use quote:: quote;
3+ use syn:: { DeriveInput , parse_macro_input} ;
4+
5+ #[ proc_macro_derive( MsgHasSignature ) ]
6+ pub fn msg_signature_derive ( input : TokenStream ) -> TokenStream {
7+ let input = parse_macro_input ! ( input as DeriveInput ) ;
8+ let name = input. ident ;
9+
10+ let has_signature = match & input. data {
11+ syn:: Data :: Struct ( data_struct) => match & data_struct. fields {
12+ syn:: Fields :: Named ( fields_named) => fields_named
13+ . named
14+ . iter ( )
15+ . any ( |field| field. ident . as_ref ( ) . unwrap ( ) == "signature" ) ,
16+ _ => false ,
17+ } ,
18+ _ => false ,
19+ } ;
20+
21+ if has_signature {
22+ let expanded = quote ! {
23+ impl msg_signature:: MsgHasSignature for #name {
24+ fn signature( & self ) -> & [ u8 ] {
25+ & self . signature
26+ }
27+
28+ fn without_signature( & self ) -> Self {
29+ let mut clone = self . clone( ) ;
30+ clone. signature = vec![ ] ;
31+ clone
32+ }
33+
34+ fn set_signature( & mut self , signature: Vec <u8 >) {
35+ self . signature = signature;
36+ }
37+
38+ fn clear_signature( & mut self ) {
39+ self . signature = vec![ ] ;
40+ }
41+ }
42+ } ;
43+
44+ return TokenStream :: from ( expanded) ;
45+ }
46+
47+ TokenStream :: new ( )
48+ }
Original file line number Diff line number Diff line change 1+ [package ]
2+ name = " msg-signature"
3+ version = " 0.1.0"
4+ edition = " 2024"
5+
6+ [dependencies ]
7+ msg-signature-macro = { path = " ../msg-signature-macro" }
Original file line number Diff line number Diff line change 1+ pub use msg_signature_macro:: MsgHasSignature ;
2+
3+ pub trait MsgHasSignature {
4+ fn signature ( & self ) -> & [ u8 ] ;
5+ fn without_signature ( & self ) -> Self ;
6+ fn set_signature ( & mut self , signature : Vec < u8 > ) ;
7+ fn clear_signature ( & mut self ) ;
8+ }
You can’t perform that action at this time.
0 commit comments