From 45533a946c296a3a748a645fb80869f93ad7f09a Mon Sep 17 00:00:00 2001
From: HampusM <hampus@hampusmat.com>
Date: Fri, 13 Jan 2023 21:14:03 +0100
Subject: refactor: put syn_path_to_string in a extension trait

---
 macros/src/injectable/dependency.rs     |  4 ++--
 macros/src/injectable/implementation.rs |  6 ++---
 macros/src/util/syn_path.rs             | 39 ++++++++++++++++++++-------------
 3 files changed, 29 insertions(+), 20 deletions(-)

(limited to 'macros/src')

diff --git a/macros/src/injectable/dependency.rs b/macros/src/injectable/dependency.rs
index 314a369..d9d904e 100644
--- a/macros/src/injectable/dependency.rs
+++ b/macros/src/injectable/dependency.rs
@@ -2,7 +2,7 @@ use proc_macro2::Ident;
 use syn::{parse2, FnArg, GenericArgument, LitStr, PathArguments, Type};
 
 use crate::injectable::named_attr_input::NamedAttrInput;
-use crate::util::syn_path::syn_path_to_string;
+use crate::util::syn_path::SynPathExt;
 
 /// Interface for a representation of a dependency of a injectable type.
 ///
@@ -79,7 +79,7 @@ impl IDependency for Dependency
             attr.path.get_ident().map_or_else(
                 || false,
                 |attr_ident| attr_ident.to_string().as_str() == "named",
-            ) || syn_path_to_string(&attr.path) == "syrette::named"
+            ) || &attr.path.to_string() == "syrette::named"
         });
 
         let opt_named_attr_tokens = opt_named_attr.map(|attr| &attr.tokens);
diff --git a/macros/src/injectable/implementation.rs b/macros/src/injectable/implementation.rs
index cfbc620..9b7236c 100644
--- a/macros/src/injectable/implementation.rs
+++ b/macros/src/injectable/implementation.rs
@@ -8,7 +8,7 @@ use syn::{parse_str, ExprMethodCall, FnArg, Generics, ImplItemMethod, ItemImpl,
 use crate::injectable::dependency::IDependency;
 use crate::util::item_impl::find_impl_method_by_name_mut;
 use crate::util::string::camelcase_to_snakecase;
-use crate::util::syn_path::syn_path_to_string;
+use crate::util::syn_path::SynPathExt;
 
 const DI_CONTAINER_VAR_NAME: &str = "di_container";
 const DEPENDENCY_HISTORY_VAR_NAME: &str = "dependency_history";
@@ -245,7 +245,7 @@ impl<Dep: IDependency> InjectableImpl<Dep>
             Type::TraitObject(interface_trait) => {
                 Ok(interface_trait.to_token_stream().to_string())
             }
-            Type::Path(path_interface) => Ok(syn_path_to_string(&path_interface.path)),
+            Type::Path(path_interface) => Ok(path_interface.path.to_string()),
             &_ => Err("Invalid type. Expected trait type or path type"),
         }?;
 
@@ -319,7 +319,7 @@ impl<Dep: IDependency> InjectableImpl<Dep>
                 .iter()
                 .enumerate()
                 .filter_map(|(index, attr)| {
-                    if syn_path_to_string(&attr.path).as_str() == "syrette::named" {
+                    if &attr.path.to_string() == "syrette::named" {
                         return Some(index);
                     }
 
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