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
/// Exits a function early with an `Error`.
#[macro_export]
macro_rules! bail
{
	($ctx:expr) => {
		return Err($crate::err_msg($ctx).into());
	};

	($fmt:expr, $($arg:tt)*) => {
		return Err($crate::err_msg(format!($fmt, $($arg)*)).into());
	};
}

/// Exits a function early with an `Error` if the condition is not satisfied.
#[macro_export]
macro_rules! ensure
{
	($cond:expr, $ctx:expr) => {
		if !($cond) {
			return Err($crate::err_msg($ctx).into());
		}
	};

	($cond:expr, $fmt:expr, $($arg:tt)*) => {
		if !($cond) {
			return Err($crate::err_msg(format!($fmt, $($arg)*)).into());
		}
	};
}

/// Creates an `Error` using the standard string interpolation syntax.
#[macro_export]
macro_rules! format_err
{
	($($arg:tt)*) => { $crate::err_msg(format!($($arg)*)) };
}