From 56f6f34d1f69c30350b71a9296b1d4c94f68f062 Mon Sep 17 00:00:00 2001 From: Serhii Potapov Date: Sun, 25 Feb 2024 11:38:59 +0100 Subject: [PATCH 1/4] Fix serde_complex example --- examples/serde_complex/src/main.rs | 15 ++++++++++++--- nutype_macros/src/common/gen/traits.rs | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/examples/serde_complex/src/main.rs b/examples/serde_complex/src/main.rs index 48997f7..667eaf1 100644 --- a/examples/serde_complex/src/main.rs +++ b/examples/serde_complex/src/main.rs @@ -43,7 +43,10 @@ fn main() { "#; let res: Result = serde_json::from_str(json); let err = res.unwrap_err(); - assert!(err.to_string().contains("empty, expected valid Name")); + assert_eq!( + err.to_string(), + "Name is empty. Expected valid Name at line 3 column 27" + ); } { @@ -57,7 +60,10 @@ fn main() { "#; let res: Result = serde_json::from_str(json); let err = res.unwrap_err(); - assert!(err.to_string().contains("invalid, expected valid ImageUrl")); + assert_eq!( + err.to_string(), + "ImageUrl failed the predicate test. Expected valid ImageUrl at line 4 column 60" + ); } { @@ -71,7 +77,10 @@ fn main() { "#; let res: Result = serde_json::from_str(json); let err = res.unwrap_err(); - assert!(err.to_string().contains("too small, expected valid Price")); + assert_eq!( + err.to_string(), + "Price is too small. The value must be greater than 0.0. Expected valid Price at line 6 column 13" + ); } { diff --git a/nutype_macros/src/common/gen/traits.rs b/nutype_macros/src/common/gen/traits.rs index 836066a..14ec3d7 100644 --- a/nutype_macros/src/common/gen/traits.rs +++ b/nutype_macros/src/common/gen/traits.rs @@ -241,7 +241,7 @@ pub fn gen_impl_trait_serde_deserialize( quote! { #type_name::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); + let err_msg = format!("{validation_error} Expected valid {}", #type_name_str); ::custom(err_msg) }) } From 1eed81d16fbddf589d9a4ade18dddcf881bfb040 Mon Sep 17 00:00:00 2001 From: Serhii Potapov Date: Sun, 25 Feb 2024 12:01:25 +0100 Subject: [PATCH 2/4] Fix tests --- test_suite/tests/any.rs | 10 ++++++++-- test_suite/tests/float.rs | 10 ++++++++-- test_suite/tests/integer.rs | 10 ++++++++-- test_suite/tests/string.rs | 2 +- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/test_suite/tests/any.rs b/test_suite/tests/any.rs index 471a56b..c588ed9 100644 --- a/test_suite/tests/any.rs +++ b/test_suite/tests/any.rs @@ -200,7 +200,10 @@ mod traits { { let err = "5,5".parse::().unwrap_err(); - assert_eq!(err.to_string(), "Failed to parse Position: invalid"); + assert_eq!( + err.to_string(), + "Failed to parse Position: Position failed the predicate test." + ); } } } @@ -284,7 +287,10 @@ mod traits { { let err = serde_json::from_str::("{\"x\":7,\"y\":9}").unwrap_err(); - assert_eq!(err.to_string(), "invalid, expected valid LinePoint"); + assert_eq!( + err.to_string(), + "LinePoint failed the predicate test. Expected valid LinePoint" + ); } { diff --git a/test_suite/tests/float.rs b/test_suite/tests/float.rs index a42112b..6ebc2bc 100644 --- a/test_suite/tests/float.rs +++ b/test_suite/tests/float.rs @@ -253,7 +253,10 @@ mod validators { let err = Percentage::try_from(-0.1).unwrap_err(); - assert_eq!(err.to_string(), "too small"); + assert_eq!( + err.to_string(), + "Percentage is too small. The value must be greater or equal to 0.0." + ); } } } @@ -457,7 +460,10 @@ mod traits { // Unhappy path: validation error let err: DistParseError = "12.35".parse::().unwrap_err(); - assert_eq!(err.to_string(), "Failed to parse Dist: too big"); + assert_eq!( + err.to_string(), + "Failed to parse Dist: Dist is too big. The value must be less than 12.34." + ); } #[test] diff --git a/test_suite/tests/integer.rs b/test_suite/tests/integer.rs index 4c8d5bb..0052622 100644 --- a/test_suite/tests/integer.rs +++ b/test_suite/tests/integer.rs @@ -220,7 +220,10 @@ mod validators { let err = Age::try_from(17).unwrap_err(); - assert_eq!(err.to_string(), "too small"); + assert_eq!( + err.to_string(), + "Age is too small. The value must be greater or equal to 18." + ); } } } @@ -608,7 +611,10 @@ mod traits { // Unhappy path: validation error let err: AgeParseError = "101".parse::().unwrap_err(); - assert_eq!(err.to_string(), "Failed to parse Age: too big"); + assert_eq!( + err.to_string(), + "Failed to parse Age: Age is too big. The value must be less or equal to 99." + ); } #[test] diff --git a/test_suite/tests/string.rs b/test_suite/tests/string.rs index 6a1cf9e..43a67bd 100644 --- a/test_suite/tests/string.rs +++ b/test_suite/tests/string.rs @@ -223,7 +223,7 @@ mod validators { #[nutype(validate(not_empty))] pub struct Email(String); - assert_eq!(EmailError::NotEmptyViolated.to_string(), "empty"); + assert_eq!(EmailError::NotEmptyViolated.to_string(), "Email is empty."); } mod when_boundaries_defined_as_constants { From 88a2dc412831927a94cb96d482ff40ddca816c26 Mon Sep 17 00:00:00 2001 From: Serhii Potapov Date: Sun, 25 Feb 2024 12:04:47 +0100 Subject: [PATCH 3/4] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fa73d0..389c4f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Support `Arbitrary` for any inner types * Ability to specify boundaries (`greater`, `greater_or_equal`, `less`, `less_or_equal`, `len_char_min`, `len_char_max`) with expressions or named constants. * Add `#[inline]` attribute to trivial functions +* Improve error messages ### v0.4.0 - 2023-11-21 * Support of arbitrary inner types with custom sanitizers and validators. From c93ed19c1abfa493695d79df09a04672eefa08cb Mon Sep 17 00:00:00 2001 From: Serhii Potapov Date: Sun, 25 Feb 2024 12:11:29 +0100 Subject: [PATCH 4/4] Fix UI test for Rust 1.76.0 --- test_suite/tests/ui/float/visibility/private.stderr | 2 +- test_suite/tests/ui/float/visibility/private_error.stderr | 2 +- test_suite/tests/ui/integer/visibility/private.stderr | 2 +- test_suite/tests/ui/integer/visibility/private_error.stderr | 2 +- test_suite/tests/ui/string/visibility/private.stderr | 2 +- test_suite/tests/ui/string/visibility/private_error.stderr | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test_suite/tests/ui/float/visibility/private.stderr b/test_suite/tests/ui/float/visibility/private.stderr index d4abf86..877f2f4 100644 --- a/test_suite/tests/ui/float/visibility/private.stderr +++ b/test_suite/tests/ui/float/visibility/private.stderr @@ -13,5 +13,5 @@ note: ...and refers to the struct `Percentage` which is defined here --> tests/ui/float/visibility/private.rs:4:5 | 4 | #[nutype(sanitize(with = |n| n.clamp(0.0, 100.0)))] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly = note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/test_suite/tests/ui/float/visibility/private_error.stderr b/test_suite/tests/ui/float/visibility/private_error.stderr index 10ac6a6..38dbbb5 100644 --- a/test_suite/tests/ui/float/visibility/private_error.stderr +++ b/test_suite/tests/ui/float/visibility/private_error.stderr @@ -13,5 +13,5 @@ note: ...and refers to the enum `PercentageError` which is defined here --> tests/ui/float/visibility/private_error.rs:4:5 | 4 | #[nutype(validate(greater_or_equal = 0.0, less_or_equal = 100.0))] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly = note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/test_suite/tests/ui/integer/visibility/private.stderr b/test_suite/tests/ui/integer/visibility/private.stderr index 6c23fcf..f8f6dfa 100644 --- a/test_suite/tests/ui/integer/visibility/private.stderr +++ b/test_suite/tests/ui/integer/visibility/private.stderr @@ -13,5 +13,5 @@ note: ...and refers to the struct `Percentage` which is defined here --> tests/ui/integer/visibility/private.rs:4:5 | 4 | #[nutype(sanitize(with = |n: i32| n.clamp(0, 100)))] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly = note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/test_suite/tests/ui/integer/visibility/private_error.stderr b/test_suite/tests/ui/integer/visibility/private_error.stderr index 37d6957..849c91d 100644 --- a/test_suite/tests/ui/integer/visibility/private_error.stderr +++ b/test_suite/tests/ui/integer/visibility/private_error.stderr @@ -13,5 +13,5 @@ note: ...and refers to the enum `PercentageError` which is defined here --> tests/ui/integer/visibility/private_error.rs:4:5 | 4 | #[nutype(validate(greater_or_equal = 0, less_or_equal = 100))] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly = note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/test_suite/tests/ui/string/visibility/private.stderr b/test_suite/tests/ui/string/visibility/private.stderr index 391ab8e..4cc2985 100644 --- a/test_suite/tests/ui/string/visibility/private.stderr +++ b/test_suite/tests/ui/string/visibility/private.stderr @@ -13,5 +13,5 @@ note: ...and refers to the struct `Email` which is defined here --> tests/ui/string/visibility/private.rs:4:5 | 4 | #[nutype(sanitize(trim))] - | ^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly + | ^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly = note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/test_suite/tests/ui/string/visibility/private_error.stderr b/test_suite/tests/ui/string/visibility/private_error.stderr index 3152551..afbdf46 100644 --- a/test_suite/tests/ui/string/visibility/private_error.stderr +++ b/test_suite/tests/ui/string/visibility/private_error.stderr @@ -13,5 +13,5 @@ note: ...and refers to the enum `NameError` which is defined here --> tests/ui/string/visibility/private_error.rs:4:5 | 4 | #[nutype(validate(not_empty))] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly = note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info)