compare_variables
A library for comparing the ordering of variables and producing useful messages.
The full API documentation is available at https://docs.rs/compare_variables/0.3.0/compare_variables.
Feedback welcome!
Found a bug, missing docs, or have a feature request?
Please open an issue on GitHub.
This library can be used to compare the order of two to three variables of any
type T implementing PartialOrd via either the procedural macro
compare_variables or by directly constructing the underlying Comparison
struct. Comparison implements std::fmt::Display to create nice messages
with the result of the comparison:
use ;
let x = 1.0;
let ok: = compare_variables!.unwrap;
assert_eq!;
let err: = compare_variables!.unwrap_err;
assert_eq!;
As shown in the example, compare_variables returns a
Result<Comparison, Comparison> so it can be used together with the ?
operator:
use ;
assert!;
assert_eq!;
If the underlying Comparison should be "unpacked", the simple
.unwrap_or_else(|x| x) pattern can be used:
use ;
assert_eq!;
assert_eq!;
compare_variables and Comparison allow evaluating for inequality and
equality as well as (strictly) greater or lesser than, as shown in the examples
below. It is also possible to use named or unnamed struct fields which are then
reported correspondingly in the resulting message.
use ;
// Check for equality and inequality
let x = 1.0;
assert!;
assert!;
// Named and unnamed struct fields:
let n = NamedField ;
assert!;
let err: = compare_variables!.unwrap_err;
assert_eq!;
;
let a = AnonymousField;
let err: = compare_variables!.unwrap_err;
assert_eq!;
// It is also possible to customize the error message via `as` (providing an alias) and `val` (omit the variable name):
let x: u16 = 1;
let y: u16 = 2;
let z: u16 = 3;
let err: = compare_variables!.unwrap_err;
assert_eq!;
Usage without the procedural macro
In order to minimize dependencies, it is possible to use this crate without the
proc_macro feature flag. Please see the docstring of Comparison for
details.
use ;
let x = 1;
let msg_macro = compare_variables!.unwrap_or_else;
let msg_no_macro = new;
assert_eq!;