From e0f90a8e384615c79d7d51c66d19294d75e79391 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 27 Aug 2022 23:41:41 +0200 Subject: feat: implement named bindings --- macros/src/util/item_impl.rs | 12 ++++++------ macros/src/util/mod.rs | 1 + macros/src/util/syn_path.rs | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 macros/src/util/syn_path.rs (limited to 'macros/src/util') diff --git a/macros/src/util/item_impl.rs b/macros/src/util/item_impl.rs index 271ae2f..4bd7492 100644 --- a/macros/src/util/item_impl.rs +++ b/macros/src/util/item_impl.rs @@ -1,13 +1,13 @@ use syn::{ImplItem, ImplItemMethod, ItemImpl}; -pub fn find_impl_method_by_name<'item_impl>( - item_impl: &'item_impl ItemImpl, +pub fn find_impl_method_by_name_mut<'item_impl>( + item_impl: &'item_impl mut ItemImpl, method_name: &'static str, -) -> Option<&'item_impl ImplItemMethod> +) -> Option<&'item_impl mut ImplItemMethod> { - let impl_items = &item_impl.items; + let impl_items = &mut item_impl.items; - impl_items.iter().find_map(|impl_item| match impl_item { + impl_items.iter_mut().find_map(|impl_item| match impl_item { ImplItem::Method(method_item) => { if method_item.sig.ident == method_name { Some(method_item) @@ -15,6 +15,6 @@ pub fn find_impl_method_by_name<'item_impl>( None } } - &_ => None, + &mut _ => None, }) } diff --git a/macros/src/util/mod.rs b/macros/src/util/mod.rs index fc7b2c6..4f2a594 100644 --- a/macros/src/util/mod.rs +++ b/macros/src/util/mod.rs @@ -1,2 +1,3 @@ pub mod item_impl; pub mod iterator_ext; +pub mod syn_path; diff --git a/macros/src/util/syn_path.rs b/macros/src/util/syn_path.rs new file mode 100644 index 0000000..15653bf --- /dev/null +++ b/macros/src/util/syn_path.rs @@ -0,0 +1,22 @@ +#![allow(clippy::module_name_repetitions)] +use quote::ToTokens; +use syn::punctuated::Pair; + +pub fn syn_path_to_string(path: &syn::Path) -> String +{ + path.segments + .pairs() + .map(Pair::into_tuple) + .map(|(segment, opt_punct)| { + let segment_ident = &segment.ident; + + format!( + "{}{}", + segment_ident, + opt_punct.map_or_else(String::new, |punct| punct + .to_token_stream() + .to_string()) + ) + }) + .collect() +} -- cgit v1.2.3-18-g5258