Assure: macros for Rust runtime checking
This Rust crate provides the macro assure! and related macros.
These are intentionally similar to the macro assert! and related macros.
Available via https://crates.io/crates/assure
Introduction
The assure macros work like this:
-
assure!(x)returnsResultwithOk(x)orErr("assure"). -
assure_eq(x, y)returnsResultwithOk(x)orErr("assure_eq left:1 right:2")).
For comparison assert macros work like this:
-
assert!(x)returns successfully or callspanic!. -
assert_eq!(x, y)returns successfully or callspanic!.
Return Ok or Err
The assure macros return Result with either:
-
Ok(…)with the leftmost macro argument. -
Err(…)with a generated error message intended for diagnostics.
Example of Ok:
let a = 1;
let b = 1;
assure_eq!;
//-> Ok(a)
Example of Err:
let a = 1;
let b = 2;
assure_eq!;
//-> Err("assure_eq left:1 right:2")
Usage
The assure macros can useful for checking with the ? operator.
This example function uses the assure_gt! macro, which means assure greater than:
Example of Ok:
sum_positive_numbers;
//-> Ok(3)
Example of Err:
sum_positive_numbers;
//-> Err("assure_gt left:-2 right:0)
Custom error messages
The assure macros generate a defult diagnostic error message such as:
assure_eq!(1, 2)returnsErr("assure_eq left:1 right:2").
The macros have a second form, where a custom error message can be provided as the last argument:
assure_eq!(1, 2, "message")returnsErr("message").
Example error message:
Macros for simple values
Macro for truth checking:
assure!(a): assureais true.
Macros for value comparison:
-
assure_eq!(a, b): assureais equal tob. -
assure_ne!(a, b): assureais not equal tob. -
assure_lt!(a, b): assureais less thanb. -
assure_le!(a, b): assureais less than or equal tob. -
assure_gt!(a, b): assureais greater thanb. -
assure_ge!(a, b): assureais greater than or equal tob.
Macros for set checking
The assure_set…! macros help with comparison of set parameters, such as two arrays or two vectors. where the item order does not matter, and the item count does not matter.
-
assure_set_eq(a, b): assure the setais equal to the setb. -
assure_set_ne(a, b): assure the setais not equal to the setb.
Example of Ok:
let a = ;
let b = ;
assure_set_eq!;
//-> Ok(&a)
Example of Err:
let a = ;
let b = ;
assure_set_eq!;
//-> Err("assure_set_eq left:{1, 2} right:{3, 4}")
Macros for bag checking
The assure_bag…! macros help with comparison of bag parameters, such as comparison of two arrays or two vectors, where the item order does not matter, and the item count does matter.
-
assure_bag_eq(a, b): assure the bagais equal to the bagb. -
assure_bag_ne(a, b): assure the bagais not equal to the bagb.
Example of Ok:
let a = ;
let b = ;
assure_set_eq!;
//-> Ok(&a)
Example of Err:
let a = ;
let b = ;
assure_set_eq!;
//-> Err("assure_bag_eq left:{1: 2} right:{1: 3}")
Macros for IO-related checking
The assure_io…! macros help with IO-related checking, such as comparison of files, streams, etc. These macros return a Result with Ok(true) or Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, message)).
Macro for truth checking:
assure_io!(a): assureais true.
Macros for value comparison:
-
assure_io_eq!(a, b): assureais equal tob. -
assure_io_ne!(a, b): assureais not equal tob. -
assure_io_lt!(a, b): assureais less thanb. -
assure_io_le!(a, b): assureais less than or equal tob. -
assure_io_gt!(a, b): assureais greater thanb. -
assure_io_ge!(a, b): assureais greater than or equal tob.
Example of Ok:
let a = 1;
let b = 1;
assure_io_eq!;
//-> Ok(a)
Example of Err:
let a = 1;
let b = 2;
assure_io_eq!;
//-> Err(
// std::io::Error::new(
// std::io::ErrorKind::InvalidInput,
// "assure_io_eq left:1 right:2"
// )
// )