Skip to content

Commit f263dba

Browse files
Update parser.rs
1 parent 46c14d8 commit f263dba

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

macros/src/parser.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,16 @@ impl Parse for FieldDef {
4949
Ok(FieldDef { name, size })
5050
}
5151
}
52+
53+
pub fn parse_bit_width(ty: &syn::Ident) -> Result<u8> {
54+
let ty_str = ty.to_string();
55+
if !ty_str.starts_with("u") {
56+
return Err(syn::Error::new_spanned(ty, format!("Invalid type {ty_str}, expected u{{1..128}}")));
57+
}
58+
let size = ty_str[1..].parse::<u8>()
59+
.map_err(|e| syn::Error::new_spanned(ty, format!("Could not parse type size: {e}")))?;
60+
if size == 0 || size > 128 {
61+
return Err(syn::Error::new_spanned(ty, format!("Invalid size for {ty_str}, expected u{{1..128}}")));
62+
}
63+
Ok(size)
64+
}

0 commit comments

Comments
 (0)