1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
//! # Submodule for Eiffel Inspired Invariant Checking Macros
//!
//! This submodule is part of a larger crate that provides features inspired by the Eiffel programming language's
//! invariant checking. These features include checks for loops, entry, exit, and more.
//!
//! This submodule specifically provides the standard macros, as the main crate cannot mix standard macros with
//! procedural macros due to Cargo's restrictions.
//!
//! The Eiffel language's options for invariant checking serve as the basis for the design
//! and functionality of the macros in this submodule.
//!
//! Please note that this submodule, like the main crate, is still a work in progress. As such, some features may not be fully
//! implemented or may undergo significant changes in future updates.
//!
//! Contributions and feedback are always welcome.
#![deny(warnings)]
#![deny(missing_docs)]
use thiserror::Error;
/// A simple error type to represent a precondition failed error.
#[derive(Error, Debug, Clone)]
#[error("{message}")]
pub struct PreconditionFailedError {
message: &'static str,
}
#[macro_export]
/// `require` is a macro that checks if a given condition is met.
/// If the condition is not met, the macro will cause the program to panic with a specified message.
///
/// # Arguments
///
/// * `$condition`: An expression that should evaluate to a boolean. This is the precondition that needs to be checked.
/// * `$msg`: A message that will be printed if the precondition is not met.
///
/// # Panics
///
/// The macro panics if the precondition `$condition` is not met, with a panic message of the form: "Precondition failed: $msg".
macro_rules! require {
($condition:expr, $msg:expr) => {
if !$condition {
panic!("Precondition failed: {}", $msg);
}
};
}
#[macro_export]
/// `require_or_err` is a macro that checks if a given condition is met.
/// If the condition is not met, the macro will return an error of type `PreconditionFailedError` with a specified message.
///
/// # Arguments
///
/// * `$condition`: An expression that should evaluate to a boolean. This is the precondition that needs to be checked.
/// * `$msg`: A message that will be included in the `PreconditionFailedError` if the precondition is not met.
///
/// # Errors
///
/// The macro returns an error of type `PreconditionFailedError` if the precondition `$condition` is not met, with an error message of the form: "Precondition failed: $msg".
macro_rules! require_or_err {
($condition:expr, $msg:expr) => {
if !$condition {
return Err($crate::PreconditionFailedError { message: $msg }.into());
}
};
}
#[cfg(test)]
mod tests {
///! Basic usage of the `require!` and `require_or_err!` macros.
use super::*;
fn example_function_with_result(x: i32) -> Result<(), PreconditionFailedError> {
require_or_err!(x > 0, "x must be greater than 0");
// Proceed with the function logic
println!("x is a valid argument: {}", x);
Ok(())
}
fn example_function_without_result(x: i32) {
require!(x > 0, "x must be greater than 0");
// Proceed with the function logic
println!("x is a valid argument: {}", x);
}
#[test]
fn it_works() {
assert!(example_function_with_result(1).is_ok());
assert!(example_function_with_result(0).is_err());
assert!(example_function_with_result(-1).is_err());
}
#[test]
fn it_works_without_result() {
example_function_without_result(1);
}
#[test]
#[should_panic]
fn it_works_without_result_and_panics() {
example_function_without_result(0);
}
}