Skip to content

Commit d996e7c

Browse files
jaseemabidemilio
authored andcommitted
Refactor pattern match for Syn::Expr::Lit
Moved similar patterns into a nested block, 1. The non exhaustive pattern match warnings are way more clearer and explicit now because it is on a specific smaller type than all of syn::Expr. This refactor showed that cbindgen doesn't handle byte strings and verbatim nodes. 2. Slightly shorter without sacrificing readability even though I added some newlines and comments.
1 parent 9b7bb8f commit d996e7c

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

src/bindgen/ir/constant.rs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::borrow::Cow;
66
use std::collections::HashMap;
77
use std::io::Write;
88

9-
use syn;
9+
use syn::{self, UnOp};
1010

1111
use bindgen::config::{Config, Language};
1212
use bindgen::declarationtyperesolver::DeclarationTypeResolver;
@@ -18,7 +18,6 @@ use bindgen::ir::{
1818
use bindgen::library::Library;
1919
use bindgen::writer::{Source, SourceWriter};
2020
use bindgen::Bindings;
21-
use syn::UnOp;
2221

2322
#[derive(Debug, Clone)]
2423
pub enum Literal {
@@ -100,8 +99,10 @@ impl Literal {
10099
}
101100
}
102101

102+
// Translate from full blown `syn::Expr` into a simpler `Literal` type
103103
pub fn load(expr: &syn::Expr) -> Result<Literal, String> {
104104
match *expr {
105+
// Match binary expressions of the form `a * b`
105106
syn::Expr::Binary(ref bin_expr) => {
106107
let l = Self::load(&bin_expr.left)?;
107108
let r = Self::load(&bin_expr.right)?;
@@ -121,33 +122,29 @@ impl Literal {
121122
right: Box::new(r),
122123
})
123124
}
124-
syn::Expr::Lit(syn::ExprLit {
125-
lit: syn::Lit::Str(ref value),
126-
..
127-
}) => Ok(Literal::Expr(format!("u8\"{}\"", value.value()))),
128-
syn::Expr::Lit(syn::ExprLit {
129-
lit: syn::Lit::Byte(ref value),
130-
..
131-
}) => Ok(Literal::Expr(format!("{}", value.value()))),
132-
syn::Expr::Lit(syn::ExprLit {
133-
lit: syn::Lit::Char(ref value),
134-
..
135-
}) => Ok(Literal::Expr(match value.value() as u32 {
136-
0..=255 => format!("'{}'", value.value().escape_default()),
137-
other_code => format!(r"L'\u{:X}'", other_code),
138-
})),
139-
syn::Expr::Lit(syn::ExprLit {
140-
lit: syn::Lit::Int(ref value),
141-
..
142-
}) => Ok(Literal::Expr(value.base10_digits().to_string())),
143-
syn::Expr::Lit(syn::ExprLit {
144-
lit: syn::Lit::Float(ref value),
145-
..
146-
}) => Ok(Literal::Expr(value.base10_digits().to_string())),
147-
syn::Expr::Lit(syn::ExprLit {
148-
lit: syn::Lit::Bool(ref value),
149-
..
150-
}) => Ok(Literal::Expr(format!("{}", value.value))),
125+
126+
// Match literals like "one", 'a', 32 etc
127+
syn::Expr::Lit(syn::ExprLit { ref lit, .. }) => {
128+
match lit {
129+
syn::Lit::Str(ref value) => {
130+
Ok(Literal::Expr(format!("u8\"{}\"", value.value())))
131+
}
132+
syn::Lit::Byte(ref value) => Ok(Literal::Expr(format!("{}", value.value()))),
133+
syn::Lit::Char(ref value) => Ok(Literal::Expr(match value.value() as u32 {
134+
0..=255 => format!("'{}'", value.value().escape_default()),
135+
other_code => format!(r"L'\u{:X}'", other_code),
136+
})),
137+
syn::Lit::Int(ref value) => {
138+
Ok(Literal::Expr(value.base10_digits().to_string()))
139+
}
140+
syn::Lit::Float(ref value) => {
141+
Ok(Literal::Expr(value.base10_digits().to_string()))
142+
}
143+
syn::Lit::Bool(ref value) => Ok(Literal::Expr(format!("{}", value.value))),
144+
// TODO: Add support for byte string and Verbatim
145+
_ => Err(format!("Unsupported literal expression. {:?}", *lit)),
146+
}
147+
}
151148

152149
syn::Expr::Struct(syn::ExprStruct {
153150
ref path,
@@ -171,6 +168,7 @@ impl Literal {
171168
fields: field_map,
172169
})
173170
}
171+
174172
syn::Expr::Unary(syn::ExprUnary {
175173
ref op, ref expr, ..
176174
}) => match *op {
@@ -183,7 +181,8 @@ impl Literal {
183181
}
184182
_ => Err(format!("Unsupported Unary expression. {:?}", *op)),
185183
},
186-
_ => Err(format!("Unsupported literal expression. {:?}", *expr)),
184+
185+
_ => Err(format!("Unsupported expression. {:?}", *expr)),
187186
}
188187
}
189188

0 commit comments

Comments
 (0)