Skip to content

Commit 7cf40ed

Browse files
authored
Merge pull request #74 from rhn/warning
Error out when using the wrong attribute
2 parents 51f779d + 46679a1 commit 7cf40ed

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

packed_struct_codegen/src/pack_parse.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ use std::ops::Range;
1111

1212
use crate::utils_syn::{get_expr_int_val, get_single_segment, tokens_to_string};
1313

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)>> {
1515
let mut r = vec![];
1616

1717
for attr in attributes {
1818
let meta = attr.parse_meta()?;
19-
2019
match &meta {
2120
&syn::Meta::List(ref metalist) => {
2221
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+
}
2325
if path == main_attribute {
2426
for nv in &metalist.nested {
2527
match nv {
@@ -161,13 +163,13 @@ fn get_field_mid_positioning(field: &syn::Field) -> syn::Result<FieldMidPosition
161163
_ => { return Err(syn::Error::new(field.ty.span(), "Unsupported type")); }
162164
};
163165

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")?);
165167

166168
let bits_position = field_attributes.iter().filter_map(|a| match a {
167169
&PackFieldAttribute::BitPosition(b) | &PackFieldAttribute::BytePosition(b) => Some(b),
168170
_ => None
169171
}).next().unwrap_or(BitsPositionParsed::Next);
170-
172+
171173
let bit_width = if let Some(bits) = field_attributes.iter().filter_map(|a| if let &PackFieldAttribute::SizeBits(bits) = a { Some(bits) } else { None }).next() {
172174
if array_size > 1 {
173175
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
192194

193195

194196
fn parse_field(field: &syn::Field, mp: &FieldMidPositioning, bit_range: &Range<usize>, default_endianness: Option<IntegerEndianness>) -> syn::Result<FieldKind> {
195-
197+
196198
match &field.ty {
197199
syn::Type::Path(_) => {
198200
return Ok(
@@ -237,8 +239,7 @@ fn parse_reg_field(field: &syn::Field, ty: &syn::Type, bit_range: &Range<usize>,
237239
let bit_width = (bit_range.end - bit_range.start) + 1;
238240

239241
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")?);
242243

243244

244245
let is_enum_ty = field_attributes.iter().filter_map(|a| match a {
@@ -348,7 +349,7 @@ pub fn parse_num(s: &str) -> usize {
348349

349350

350351
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")?);
352353

353354
let data_struct = match &ast.data {
354355
syn::Data::Struct(data) => data,
@@ -475,4 +476,4 @@ pub fn parse_struct(ast: &syn::DeriveInput) -> syn::Result<PackStruct> {
475476
num_bytes,
476477
num_bits
477478
})
478-
}
479+
}

0 commit comments

Comments
 (0)