Repository: greyblake/nutype (language: rust)

Issue:
Compilation error with no-std + serde + validation
This macro `#[nutype(validate(less_or_equal = 100), derive(Deserialize))]` causes a compilation error when used in no_std environment. The error is due to the use of `format!()` macro (which depends on alloc) when reporting an error during serde de-serialization. See the minimal example in this repository: https://github.com/Indy2222/nutype-nostd-serde

Compilation error:

```
error: cannot find macro `format` in this scope
 --> src/lib.rs:4:1
  |
4 | #[nutype(validate(less_or_equal = 100), derive(Deserialize))]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `nutype-issue` (lib) due to 1 previous error
```

I think this should be fixed, as both Nutype and Serde support no_std environments.


Discussion / hints:
@Indy2222 Thank you for the report!

Fix patch:
diff --git a/nutype_macros/src/common/gen/traits.rs b/nutype_macros/src/common/gen/traits.rs
index abeff2c..f6d3bbd 100644
--- a/nutype_macros/src/common/gen/traits.rs
+++ b/nutype_macros/src/common/gen/traits.rs
@@ -294,8 +294,7 @@ pub fn gen_impl_trait_serde_deserialize(
         quote! {
             #type_name::try_new(raw_value).map_err(|validation_error| {
                 // Add a hint about which type is causing the error,
-                let err_msg = format!("{validation_error} Expected valid {}", #type_name_str);
-                <DE::Error as serde::de::Error>::custom(err_msg)
+                <DE::Error as serde::de::Error>::custom(core::format_args!("{validation_error} Expected valid {}", #type_name_str))
             })
         }
     } else {


Test patch: