| 1 | use quote::{quote, ToTokens}; |
| 2 | use syn::{spanned::Spanned, Expr, Lit, Meta, Type}; |
| 3 | |
| 4 | use super::path::path_to_string; |
| 5 | |
| 6 | const INT_TYPES: [&str; 12] = |
| 7 | ["u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128", "isize"]; |
| 8 | |
| 9 | const FLOAT_TYPES: [&str; 2] = ["f32", "f64"]; |
| 10 | |
| 11 | #[inline] |
| 12 | pub(crate) fn meta_2_expr(meta: &Meta) -> syn::Result<Expr> { |
| 13 | match &meta { |
| 14 | Meta::NameValue(name_value) => Ok(name_value.value.clone()), |
| 15 | Meta::List(list) => list.parse_args::<Expr>(), |
| 16 | Meta::Path(path) => Err(syn::Error::new( |
| 17 | path.span(), |
| 18 | format!("expected `{path} = Expr` or `{path}(Expr)`", path = path_to_string(path)), |
| 19 | )), |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | #[inline] |
| 24 | pub(crate) fn auto_adjust_expr(expr: Expr, ty: Option<&Type>) -> Expr { |
| 25 | match &expr { |
| 26 | Expr::Lit(lit) => { |
| 27 | match &lit.lit { |
| 28 | Lit::Int(lit) => { |
| 29 | if let Some(Type::Path(ty)) = ty { |
| 30 | let ty_string = ty.into_token_stream().to_string(); |
| 31 | |
| 32 | if lit.suffix() == ty_string || INT_TYPES.contains(&ty_string.as_str()) { |
| 33 | // don't call into |
| 34 | return expr; |
| 35 | } |
| 36 | } |
| 37 | }, |
| 38 | Lit::Float(lit) => { |
| 39 | if let Some(Type::Path(ty)) = ty { |
| 40 | let ty_string = ty.into_token_stream().to_string(); |
| 41 | |
| 42 | if lit.suffix() == ty_string || FLOAT_TYPES.contains(&ty_string.as_str()) { |
| 43 | // don't call into |
| 44 | return expr; |
| 45 | } |
| 46 | } |
| 47 | }, |
| 48 | Lit::Str(_) => { |
| 49 | if let Some(Type::Reference(ty)) = ty { |
| 50 | let ty_string = ty.elem.clone().into_token_stream().to_string(); |
| 51 | |
| 52 | if ty_string == "str" { |
| 53 | // don't call into |
| 54 | return expr; |
| 55 | } |
| 56 | } |
| 57 | }, |
| 58 | Lit::Bool(_) => { |
| 59 | if let Some(Type::Path(ty)) = ty { |
| 60 | let ty_string = ty.into_token_stream().to_string(); |
| 61 | |
| 62 | if ty_string == "bool" { |
| 63 | // don't call into |
| 64 | return expr; |
| 65 | } |
| 66 | } |
| 67 | }, |
| 68 | Lit::Char(_) => { |
| 69 | if let Some(Type::Path(ty)) = ty { |
| 70 | let ty_string = ty.into_token_stream().to_string(); |
| 71 | |
| 72 | if ty_string == "char" { |
| 73 | // don't call into |
| 74 | return expr; |
| 75 | } |
| 76 | } |
| 77 | }, |
| 78 | Lit::Byte(_) => { |
| 79 | if let Some(Type::Path(ty)) = ty { |
| 80 | let ty_string = ty.into_token_stream().to_string(); |
| 81 | |
| 82 | if ty_string == "u8" { |
| 83 | // don't call into |
| 84 | return expr; |
| 85 | } |
| 86 | } |
| 87 | }, |
| 88 | Lit::ByteStr(_) => { |
| 89 | if let Some(Type::Reference(ty)) = ty { |
| 90 | if let Type::Array(ty) = ty.elem.as_ref() { |
| 91 | if let Type::Path(ty) = ty.elem.as_ref() { |
| 92 | let ty_string = ty.into_token_stream().to_string(); |
| 93 | |
| 94 | if ty_string == "u8" { |
| 95 | // don't call into |
| 96 | return expr; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | }, |
| 102 | _ => (), |
| 103 | } |
| 104 | |
| 105 | syn::parse2(quote!(::core::convert::Into::into(#expr))).unwrap() |
| 106 | }, |
| 107 | _ => expr, |
| 108 | } |
| 109 | } |