Repository: greyblake/nutype (language: rust)

Issue:
Refactor: move `gen_impl_into_inner` into common



Discussion / hints:

Fix patch:
diff --git a/WORKLOG.md b/WORKLOG.md
index e7f0d98..fcb9118 100644
--- a/WORKLOG.md
+++ b/WORKLOG.md
@@ -1,6 +1,4 @@
 ## Roadmap
-* new_unchecked: review the PR: https://github.com/greyblake/nutype/pull/16
-* Merge code examples in README and lib.rs into bigger chunks, so doc tests can run
 
 ### TODO Refactor:
 * Refactor: Use newtype for errors (e.g. error_type_name, etc)
@@ -112,3 +110,6 @@
 * new_unchecked: add test coverage
 * new_unchecked: add docs to README
 * new_unchecked: add docs to lib.rs
+* new_unchecked: review the PR: https://github.com/greyblake/nutype/pull/16
+* Merge code examples in README and lib.rs into bigger chunks, so doc tests can run
+* Reuse gen_impl_into_inner
diff --git a/nutype_macros/src/common/gen/mod.rs b/nutype_macros/src/common/gen/mod.rs
index 0788054..049706e 100644
--- a/nutype_macros/src/common/gen/mod.rs
+++ b/nutype_macros/src/common/gen/mod.rs
@@ -87,3 +87,13 @@ pub fn gen_reimports(
         #reimport_parse_error_type_if_needed
     }
 }
+
+pub fn gen_impl_into_inner(type_name: &TypeName, inner_type: impl ToTokens) -> TokenStream {
+    quote! {
+        impl #type_name {
+            pub fn into_inner(self) -> #inner_type {
+                self.0
+            }
+        }
+    }
+}
diff --git a/nutype_macros/src/float/gen/mod.rs b/nutype_macros/src/float/gen/mod.rs
index d370e87..f370eab 100644
--- a/nutype_macros/src/float/gen/mod.rs
+++ b/nutype_macros/src/float/gen/mod.rs
@@ -15,7 +15,10 @@ use crate::{
         new_unchecked::gen_new_unchecked, parse_error::gen_parse_error_name,
         traits::GeneratedTraits, type_custom_closure,
     },
-    common::models::{FloatType, NewUnchecked, TypeName},
+    common::{
+        gen::gen_impl_into_inner,
+        models::{FloatType, NewUnchecked, TypeName},
+    },
 };
 use traits::gen_traits;
 
@@ -102,16 +105,6 @@ where
     }
 }
 
-fn gen_impl_into_inner(type_name: &TypeName, inner_type: FloatType) -> TokenStream {
-    quote! {
-        impl #type_name {
-            pub fn into_inner(self) -> #inner_type {
-                self.0
-            }
-        }
-    }
-}
-
 fn gen_new_without_validation<T>(
     type_name: &TypeName,
     inner_type: FloatType,
diff --git a/nutype_macros/src/integer/gen/mod.rs b/nutype_macros/src/integer/gen/mod.rs
index cdf2b51..25f37fc 100644
--- a/nutype_macros/src/integer/gen/mod.rs
+++ b/nutype_macros/src/integer/gen/mod.rs
@@ -15,7 +15,10 @@ use crate::{
         new_unchecked::gen_new_unchecked, parse_error::gen_parse_error_name,
         traits::GeneratedTraits, type_custom_closure,
     },
-    common::models::{IntegerType, NewUnchecked, TypeName},
+    common::{
+        gen::gen_impl_into_inner,
+        models::{IntegerType, NewUnchecked, TypeName},
+    },
 };
 
 pub fn gen_nutype_for_integer<T>(
@@ -102,16 +105,6 @@ where
     }
 }
 
-fn gen_impl_into_inner(type_name: &TypeName, inner_type: IntegerType) -> TokenStream {
-    quote! {
-        impl #type_name {
-            pub fn into_inner(self) -> #inner_type {
-                self.0
-            }
-        }
-    }
-}
-
 fn gen_new_without_validation<T>(
     type_name: &TypeName,
     inner_type: IntegerType,
diff --git a/nutype_macros/src/string/gen/mod.rs b/nutype_macros/src/string/gen/mod.rs
index 3446202..9a4a3e0 100644
--- a/nutype_macros/src/string/gen/mod.rs
+++ b/nutype_macros/src/string/gen/mod.rs
@@ -10,10 +10,11 @@ use syn::Attribute;
 use crate::{
     common::{
         gen::{
-            error::gen_error_type_name, gen_module_name_for_type, gen_reimports,
-            new_unchecked::gen_new_unchecked, traits::GeneratedTraits, type_custom_closure,
+            error::gen_error_type_name, gen_impl_into_inner, gen_module_name_for_type,
+            gen_reimports, new_unchecked::gen_new_unchecked, traits::GeneratedTraits,
+            type_custom_closure,
         },
-        models::{NewUnchecked, TypeName},
+        models::{InnerType, NewUnchecked, TypeName},
     },
     string::models::{StringSanitizer, StringValidator},
 };
@@ -81,9 +82,8 @@ pub fn gen_string_implementation(
             validators,
         } => gen_new_and_with_validation(type_name, sanitizers, validators),
     };
-    let impl_into_inner = gen_impl_into_inner(type_name);
-
-    let inner_type = quote!(String);
+    let inner_type = InnerType::String;
+    let impl_into_inner = gen_impl_into_inner(type_name, inner_type);
     let impl_new_unchecked = gen_new_unchecked(type_name, inner_type, new_unchecked);
 
     quote! {
@@ -93,16 +93,6 @@ pub fn gen_string_implementation(
     }
 }
 
-fn gen_impl_into_inner(type_name: &TypeName) -> TokenStream {
-    quote! {
-        impl #type_name {
-            pub fn into_inner(self) -> String {
-                self.0
-            }
-        }
-    }
-}
-
 fn gen_new_without_validation(type_name: &TypeName, sanitizers: &[StringSanitizer]) -> TokenStream {
     let sanitize = gen_string_sanitize_fn(sanitizers);
 


Test patch: