From 45533a946c296a3a748a645fb80869f93ad7f09a Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 13 Jan 2023 21:14:03 +0100 Subject: refactor: put syn_path_to_string in a extension trait --- macros/src/util/syn_path.rs | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) (limited to 'macros/src/util') diff --git a/macros/src/util/syn_path.rs b/macros/src/util/syn_path.rs index 0e1b8f4..fc301ab 100644 --- a/macros/src/util/syn_path.rs +++ b/macros/src/util/syn_path.rs @@ -1,21 +1,30 @@ use quote::ToTokens; use syn::punctuated::Pair; -pub fn syn_path_to_string(path: &syn::Path) -> String +pub trait SynPathExt { - path.segments - .pairs() - .map(Pair::into_tuple) - .map(|(segment, opt_punct)| { - let segment_ident = &segment.ident; + /// Converts the [`syn::Path`] to a [`String`]. + fn to_string(&self) -> String; +} + +impl SynPathExt for syn::Path +{ + fn to_string(&self) -> String + { + self.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() + format!( + "{}{}", + segment_ident, + opt_punct.map_or_else(String::new, |punct| punct + .to_token_stream() + .to_string()) + ) + }) + .collect() + } } -- cgit v1.2.3-18-g5258