From f7c221774bb3be08cb54d68c8fea77421feb70ab Mon Sep 17 00:00:00 2001 From: Marco Mastropaolo Date: Tue, 19 Sep 2023 20:03:05 +0200 Subject: [PATCH] Added support for enums with negative values This adds parse and generation support for enums whose discriminant value is a negative number. It's responsibility of the library to have the appropriate "repr" type. --- csbindgen/src/parser.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/csbindgen/src/parser.rs b/csbindgen/src/parser.rs index 04ad215..8156c96 100644 --- a/csbindgen/src/parser.rs +++ b/csbindgen/src/parser.rs @@ -314,11 +314,20 @@ pub fn collect_enum(ast: &syn::File, result: &mut Vec) { for v in &t.variants { let name = v.ident.to_string(); let mut value = None; - if let Some((_, syn::Expr::Lit(x))) = &v.discriminant { - if let syn::Lit::Int(x) = &x.lit { + + match &v.discriminant { + Some((_, syn::Expr::Lit(x))) => if let syn::Lit::Int(x) = &x.lit { let digits = x.base10_digits().to_string(); value = Some(digits); } + Some((_, syn::Expr::Unary(u))) if matches!(u.op, syn::UnOp::Neg(_)) => { + if let syn::Expr::Lit(x) = &*u.expr { + if let syn::Lit::Int(x) = &x.lit { + value = Some(format!("-{}", x.base10_digits())); + } + } + } + _ => (), } fields.push((name, value));