expect_elements

Macro expect_elements 

Source
macro_rules! expect_elements {
    (($($tokens:tt)+) , $($fmt:tt)+) => { ... };
    ($($tokens:tt)+) => { ... };
}
Expand description

Macro for creating Error::UnexpectedElements errors.

The macro accepts a comparison expression between two values (expected and actual) and an optional message. The message itself can accept formatting arguments. It will return an Err with an Error::UnexpectedElements error so the caller of the macro should have a return type of Result<_, E> s.t. E implements From for the [Error] type.

Because of how the macro is implemented, when passign a custom message is necessary to surround the comparison with parenthesis.

ยงExamples

use mdnt_support::error::Error;
use mdnt_support::expect_elements;

fn foo(c: usize) -> Result<(), Error> {
    let a = 6;
    let b = 7;
    // Default message
    expect_elements!(a == b);
    // With a custom message
    expect_elements!((a <= b), "During call to foo()");
    // With a custom formatted message
    expect_elements!((a > c), "During call to foo({c})");
    Ok(())
}