From af88321fc14a95b3613ec11a7f665db3c468c944 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 1 Oct 2023 21:15:00 +0200 Subject: refactor: remove impossible unwrap in injectable macro This unwrap couldn't possibly be Err and can be removed by creating method call expressions by hand --- macros/src/util/syn_ext.rs | 114 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 macros/src/util/syn_ext.rs (limited to 'macros/src/util/syn_ext.rs') diff --git a/macros/src/util/syn_ext.rs b/macros/src/util/syn_ext.rs new file mode 100644 index 0000000..6201670 --- /dev/null +++ b/macros/src/util/syn_ext.rs @@ -0,0 +1,114 @@ +use proc_macro2::Ident; +use syn::punctuated::Punctuated; +use syn::token::Paren; +use syn::{ + Expr, + ExprCall, + ExprLit, + ExprMethodCall, + ExprPath, + GenericMethodArgument, + Lit, + MethodTurbofish, + Path, + Token, +}; + +pub trait ExprMethodCallExt +{ + fn new(receiver: Expr, method: Ident, args: impl IntoIterator) -> Self; + + fn with_turbofish(self, turbofish: MethodTurbofish) -> Self; +} + +impl ExprMethodCallExt for ExprMethodCall +{ + fn new(receiver: Expr, method: Ident, args: impl IntoIterator) -> Self + { + Self { + attrs: Vec::new(), + receiver: Box::new(receiver), + dot_token: ::default(), + method, + turbofish: None, + paren_token: Paren::default(), + args: Punctuated::from_iter(args), + } + } + + fn with_turbofish(mut self, turbofish: MethodTurbofish) -> Self + { + self.turbofish = Some(turbofish); + + self + } +} + +pub trait ExprPathExt +{ + fn new(path: Path) -> Self; +} + +impl ExprPathExt for ExprPath +{ + fn new(path: Path) -> Self + { + Self { + attrs: Vec::new(), + qself: None, + path, + } + } +} + +pub trait MethodTurbofishExt +{ + fn new(args: impl IntoIterator) -> Self; +} + +impl MethodTurbofishExt for MethodTurbofish +{ + fn new(args: impl IntoIterator) -> Self + { + Self { + colon2_token: ::default(), + lt_token: ::default(), + args: Punctuated::from_iter(args), + gt_token: ]>::default(), + } + } +} + +pub trait ExprCallExt +{ + fn new(function: Expr, args: impl IntoIterator) -> Self; +} + +impl ExprCallExt for ExprCall +{ + fn new(function: Expr, args: impl IntoIterator) -> Self + { + Self { + attrs: Vec::new(), + func: Box::new(function), + paren_token: Paren::default(), + args: Punctuated::from_iter(args), + } + } +} + +pub trait ExprLitExt +{ + fn new(lit: impl Into) -> Self; +} + +impl ExprLitExt for ExprLit +{ + fn new(lit: impl Into) -> Self + { + Self { + attrs: Vec::new(), + lit: lit.into(), + } + } +} -- cgit v1.2.3-18-g5258