Skip to content

Commit fd09132

Browse files
authored
Merge pull request #127 from greyblake/fix-serde-complex
Fix serde_complex example
2 parents ec3f1fa + c93ed19 commit fd09132

File tree

13 files changed

+45
-17
lines changed

13 files changed

+45
-17
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Support `Arbitrary` for any inner types
88
* Ability to specify boundaries (`greater`, `greater_or_equal`, `less`, `less_or_equal`, `len_char_min`, `len_char_max`) with expressions or named constants.
99
* Add `#[inline]` attribute to trivial functions
10+
* Improve error messages
1011

1112
### v0.4.0 - 2023-11-21
1213
* Support of arbitrary inner types with custom sanitizers and validators.

examples/serde_complex/src/main.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ fn main() {
4343
"#;
4444
let res: Result<Product, _> = serde_json::from_str(json);
4545
let err = res.unwrap_err();
46-
assert!(err.to_string().contains("empty, expected valid Name"));
46+
assert_eq!(
47+
err.to_string(),
48+
"Name is empty. Expected valid Name at line 3 column 27"
49+
);
4750
}
4851

4952
{
@@ -57,7 +60,10 @@ fn main() {
5760
"#;
5861
let res: Result<Product, _> = serde_json::from_str(json);
5962
let err = res.unwrap_err();
60-
assert!(err.to_string().contains("invalid, expected valid ImageUrl"));
63+
assert_eq!(
64+
err.to_string(),
65+
"ImageUrl failed the predicate test. Expected valid ImageUrl at line 4 column 60"
66+
);
6167
}
6268

6369
{
@@ -71,7 +77,10 @@ fn main() {
7177
"#;
7278
let res: Result<Product, _> = serde_json::from_str(json);
7379
let err = res.unwrap_err();
74-
assert!(err.to_string().contains("too small, expected valid Price"));
80+
assert_eq!(
81+
err.to_string(),
82+
"Price is too small. The value must be greater than 0.0. Expected valid Price at line 6 column 13"
83+
);
7584
}
7685

7786
{

nutype_macros/src/common/gen/traits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ pub fn gen_impl_trait_serde_deserialize(
241241
quote! {
242242
#type_name::new(raw_value).map_err(|validation_error| {
243243
// Add a hint about which type is causing the error,
244-
let err_msg = format!("{validation_error}, expected valid {}", #type_name_str);
244+
let err_msg = format!("{validation_error} Expected valid {}", #type_name_str);
245245
<DE::Error as serde::de::Error>::custom(err_msg)
246246
})
247247
}

test_suite/tests/any.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ mod traits {
200200

201201
{
202202
let err = "5,5".parse::<Position>().unwrap_err();
203-
assert_eq!(err.to_string(), "Failed to parse Position: invalid");
203+
assert_eq!(
204+
err.to_string(),
205+
"Failed to parse Position: Position failed the predicate test."
206+
);
204207
}
205208
}
206209
}
@@ -284,7 +287,10 @@ mod traits {
284287

285288
{
286289
let err = serde_json::from_str::<LinePoint>("{\"x\":7,\"y\":9}").unwrap_err();
287-
assert_eq!(err.to_string(), "invalid, expected valid LinePoint");
290+
assert_eq!(
291+
err.to_string(),
292+
"LinePoint failed the predicate test. Expected valid LinePoint"
293+
);
288294
}
289295

290296
{

test_suite/tests/float.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,10 @@ mod validators {
253253

254254
let err = Percentage::try_from(-0.1).unwrap_err();
255255

256-
assert_eq!(err.to_string(), "too small");
256+
assert_eq!(
257+
err.to_string(),
258+
"Percentage is too small. The value must be greater or equal to 0.0."
259+
);
257260
}
258261
}
259262
}
@@ -457,7 +460,10 @@ mod traits {
457460

458461
// Unhappy path: validation error
459462
let err: DistParseError = "12.35".parse::<Dist>().unwrap_err();
460-
assert_eq!(err.to_string(), "Failed to parse Dist: too big");
463+
assert_eq!(
464+
err.to_string(),
465+
"Failed to parse Dist: Dist is too big. The value must be less than 12.34."
466+
);
461467
}
462468

463469
#[test]

test_suite/tests/integer.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,10 @@ mod validators {
220220

221221
let err = Age::try_from(17).unwrap_err();
222222

223-
assert_eq!(err.to_string(), "too small");
223+
assert_eq!(
224+
err.to_string(),
225+
"Age is too small. The value must be greater or equal to 18."
226+
);
224227
}
225228
}
226229
}
@@ -608,7 +611,10 @@ mod traits {
608611

609612
// Unhappy path: validation error
610613
let err: AgeParseError = "101".parse::<Age>().unwrap_err();
611-
assert_eq!(err.to_string(), "Failed to parse Age: too big");
614+
assert_eq!(
615+
err.to_string(),
616+
"Failed to parse Age: Age is too big. The value must be less or equal to 99."
617+
);
612618
}
613619

614620
#[test]

test_suite/tests/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ mod validators {
223223
#[nutype(validate(not_empty))]
224224
pub struct Email(String);
225225

226-
assert_eq!(EmailError::NotEmptyViolated.to_string(), "empty");
226+
assert_eq!(EmailError::NotEmptyViolated.to_string(), "Email is empty.");
227227
}
228228

229229
mod when_boundaries_defined_as_constants {

test_suite/tests/ui/float/visibility/private.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ note: ...and refers to the struct `Percentage` which is defined here
1313
--> tests/ui/float/visibility/private.rs:4:5
1414
|
1515
4 | #[nutype(sanitize(with = |n| n.clamp(0.0, 100.0)))]
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly
1717
= note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info)

test_suite/tests/ui/float/visibility/private_error.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ note: ...and refers to the enum `PercentageError` which is defined here
1313
--> tests/ui/float/visibility/private_error.rs:4:5
1414
|
1515
4 | #[nutype(validate(greater_or_equal = 0.0, less_or_equal = 100.0))]
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly
1717
= note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info)

test_suite/tests/ui/integer/visibility/private.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ note: ...and refers to the struct `Percentage` which is defined here
1313
--> tests/ui/integer/visibility/private.rs:4:5
1414
|
1515
4 | #[nutype(sanitize(with = |n: i32| n.clamp(0, 100)))]
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly
1717
= note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info)

test_suite/tests/ui/integer/visibility/private_error.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ note: ...and refers to the enum `PercentageError` which is defined here
1313
--> tests/ui/integer/visibility/private_error.rs:4:5
1414
|
1515
4 | #[nutype(validate(greater_or_equal = 0, less_or_equal = 100))]
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly
1717
= note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info)

test_suite/tests/ui/string/visibility/private.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ note: ...and refers to the struct `Email` which is defined here
1313
--> tests/ui/string/visibility/private.rs:4:5
1414
|
1515
4 | #[nutype(sanitize(trim))]
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly
1717
= note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info)

test_suite/tests/ui/string/visibility/private_error.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ note: ...and refers to the enum `NameError` which is defined here
1313
--> tests/ui/string/visibility/private_error.rs:4:5
1414
|
1515
4 | #[nutype(validate(not_empty))]
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly
1717
= note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)