You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library is based on the struct [`ComparisonError`], which can be used to compare the partial ordering
11
11
of two to three variables of any type `T` which implements the [`PartialOrd`] trait. If the comparison evaluates
12
12
to false, [`ComparisonError`] can be formatted into a nice error message. To simplify the usage, the procedural
13
13
macro [`compare_variables`] is provided via the feature flag **proc_macro** (enabled by default).
14
14
15
-
The full API documentation is available at [https://docs.rs/compare_variables/0.1.0/compare_variables/](https://docs.rs/compare_variables/0.1.0/compare_variables/).
15
+
The full API documentation is available at [https://docs.rs/compare_variables/0.2.0/compare_variables/](https://docs.rs/compare_variables/0.2.0/compare_variables/).
16
16
17
17
# Examples
18
18
19
+
The type annotation of the error can be omitted and is only written out in these examples for clarity.
It is also possible to use named struct fields as inputs:
58
+
It is also possible to use named and anonymous struct fields as inputs:
59
59
60
60
```
61
61
use compare_variables::compare_variables;
62
62
63
63
struct NamedField {
64
64
x: f64
65
65
}
66
-
67
66
let n = NamedField {x: 1.0};
68
67
assert!(compare_variables!(n.x > -1.0).is_ok());
69
68
assert!(compare_variables!(n.x > 1.0).is_err());
69
+
70
+
struct AnonymousField(i32);
71
+
let a = AnonymousField(-5);
72
+
assert!(compare_variables!(a.0 > -6).is_ok());
73
+
assert!(compare_variables!(a.0 > 1).is_err());
70
74
```
71
75
72
76
# Error message
73
77
74
78
The error message is created via the struct [`ComparisonError`](https://docs.rs/compare_variables/0.1.0/compare_variables/struct.ComparisonError.html).
75
-
Please refer to its documentation for more details. The keywords `raw` and `as` allow to customize the treatment of variable names in the error message:
79
+
Please refer to its documentation for more details. The keywords `val` and `as` allow to customize the treatment of variable names in the error message:
76
80
77
81
```
78
82
use compare_variables::compare_variables;
79
83
80
84
// Error message with literals only
81
-
let err = compare_variables!(5i32 <= -1i32);
82
-
assert_eq!(err.unwrap_err().to_string(), "`5 <= -1` is false");
85
+
let err = compare_variables!(5i32 <= -1i32).unwrap_err();
86
+
assert_eq!(err.to_string(), "`5 <= -1` is false");
83
87
84
88
let x = 1;
85
89
let y = 2;
86
90
87
91
// Default error message
88
-
let err = compare_variables!(x > y);
89
-
assert_eq!(err.unwrap_err().to_string(), "`x (value: 1) > y (value: 2)` is false");
92
+
let err = compare_variables!(x > y).unwrap_err();
93
+
assert_eq!(err.to_string(), "`x (value: 1) > y (value: 2)` is false");
90
94
91
95
// Rename x in the error message
92
-
let err = compare_variables!(x as variable > y);
93
-
assert_eq!(err.unwrap_err().to_string(), "`variable (value: 1) > y (value: 2)` is false");
96
+
let err = compare_variables!(x as variable > y).unwrap_err();
97
+
assert_eq!(err.to_string(), "`variable (value: 1) > y (value: 2)` is false");
94
98
95
99
// Only display the underlying value, not the variable name:
96
-
let err = compare_variables!(raw x > y);
97
-
assert_eq!(err.unwrap_err().to_string(), "`1 > y (value: 2)` is false");
100
+
let err = compare_variables!(val x > y).unwrap_err();
101
+
assert_eq!(err.to_string(), "`1 > y (value: 2)` is false");
98
102
99
-
// `as` is ignored if used together with `raw`:
100
-
let err = compare_variables!(raw x as variable > y);
101
-
assert_eq!(err.unwrap_err().to_string(), "`1 > y (value: 2)` is false");
103
+
// `as` is ignored if used together with `val`:
104
+
let err = compare_variables!(val x as variable > y).unwrap_err();
105
+
assert_eq!(err.to_string(), "`1 > y (value: 2)` is false");
0 commit comments