@@ -11,15 +11,17 @@ use std::ops::Range;
11
11
12
12
use crate :: utils_syn:: { get_expr_int_val, get_single_segment, tokens_to_string} ;
13
13
14
- pub fn parse_sub_attributes ( attributes : & Vec < syn:: Attribute > , main_attribute : & str ) -> syn:: Result < Vec < ( String , String ) > > {
14
+ pub fn parse_sub_attributes ( attributes : & Vec < syn:: Attribute > , main_attribute : & str , wrong_attribute : & str ) -> syn:: Result < Vec < ( String , String ) > > {
15
15
let mut r = vec ! [ ] ;
16
16
17
17
for attr in attributes {
18
18
let meta = attr. parse_meta ( ) ?;
19
-
20
19
match & meta {
21
20
& syn:: Meta :: List ( ref metalist) => {
22
21
if let Some ( path) = metalist. path . get_ident ( ) {
22
+ if path == wrong_attribute {
23
+ return Err ( syn:: Error :: new ( path. span ( ) , format ! ( "This attribute is not supported here, did you mean {:?}?" , main_attribute) ) ) ;
24
+ }
23
25
if path == main_attribute {
24
26
for nv in & metalist. nested {
25
27
match nv {
@@ -161,13 +163,13 @@ fn get_field_mid_positioning(field: &syn::Field) -> syn::Result<FieldMidPosition
161
163
_ => { return Err ( syn:: Error :: new ( field. ty . span ( ) , "Unsupported type" ) ) ; }
162
164
} ;
163
165
164
- let field_attributes = PackFieldAttribute :: parse_all ( & parse_sub_attributes ( & field. attrs , "packed_field" ) ?) ;
166
+ let field_attributes = PackFieldAttribute :: parse_all ( & parse_sub_attributes ( & field. attrs , "packed_field" , "packed_struct" ) ?) ;
165
167
166
168
let bits_position = field_attributes. iter ( ) . filter_map ( |a| match a {
167
169
& PackFieldAttribute :: BitPosition ( b) | & PackFieldAttribute :: BytePosition ( b) => Some ( b) ,
168
170
_ => None
169
171
} ) . next ( ) . unwrap_or ( BitsPositionParsed :: Next ) ;
170
-
172
+
171
173
let bit_width = if let Some ( bits) = field_attributes. iter ( ) . filter_map ( |a| if let & PackFieldAttribute :: SizeBits ( bits) = a { Some ( bits) } else { None } ) . next ( ) {
172
174
if array_size > 1 {
173
175
return Err ( syn:: Error :: new ( field. span ( ) , "Please use the 'element_size_bits' or 'element_size_bytes' for arrays." ) ) ;
@@ -192,7 +194,7 @@ fn get_field_mid_positioning(field: &syn::Field) -> syn::Result<FieldMidPosition
192
194
193
195
194
196
fn parse_field ( field : & syn:: Field , mp : & FieldMidPositioning , bit_range : & Range < usize > , default_endianness : Option < IntegerEndianness > ) -> syn:: Result < FieldKind > {
195
-
197
+
196
198
match & field. ty {
197
199
syn:: Type :: Path ( _) => {
198
200
return Ok (
@@ -237,8 +239,7 @@ fn parse_reg_field(field: &syn::Field, ty: &syn::Type, bit_range: &Range<usize>,
237
239
let bit_width = ( bit_range. end - bit_range. start ) + 1 ;
238
240
239
241
let ty_str = tokens_to_string ( ty) ;
240
-
241
- let field_attributes = PackFieldAttribute :: parse_all ( & parse_sub_attributes ( & field. attrs , "packed_field" ) ?) ;
242
+ let field_attributes = PackFieldAttribute :: parse_all ( & parse_sub_attributes ( & field. attrs , "packed_field" , "packed_struct" ) ?) ;
242
243
243
244
244
245
let is_enum_ty = field_attributes. iter ( ) . filter_map ( |a| match a {
@@ -348,7 +349,7 @@ pub fn parse_num(s: &str) -> usize {
348
349
349
350
350
351
pub fn parse_struct ( ast : & syn:: DeriveInput ) -> syn:: Result < PackStruct > {
351
- let attributes = PackStructAttribute :: parse_all ( & parse_sub_attributes ( & ast. attrs , "packed_struct" ) ?) ;
352
+ let attributes = PackStructAttribute :: parse_all ( & parse_sub_attributes ( & ast. attrs , "packed_struct" , "packed_field" ) ?) ;
352
353
353
354
let data_struct = match & ast. data {
354
355
syn:: Data :: Struct ( data) => data,
@@ -475,4 +476,4 @@ pub fn parse_struct(ast: &syn::DeriveInput) -> syn::Result<PackStruct> {
475
476
num_bytes,
476
477
num_bits
477
478
} )
478
- }
479
+ }
0 commit comments