1/// Return early with an error.
2#[macro_export]
3macro_rules! bail {
4 ($msg:literal $(,)?) => {
5return $crate::private::Err($crate::format_err!($msg))
6 };
7 ($msg:expr $(,)?) => {
8return $crate::private::Err($crate::format_err!($msg))
9 };
10 ($msg:expr, $($arg:tt)*) => {
11return $crate::private::Err($crate::format_err!($msg, $($arg)*))
12 };
13}
1415/// Return early with an error if a condition is not satisfied.
16///
17/// This macro is equivalent to `if !$cond { return Err(From::from($err)); }`.
18///
19/// Analogously to `assert!`, `ensure!` takes a condition and exits the function
20/// if the condition fails. Unlike `assert!`, `ensure!` returns an `Error`
21/// rather than panicking.
22#[macro_export]
23macro_rules! ensure {
24 ($cond:expr, $msg:literal $(,)?) => {
25if !$cond {
26return $crate::private::Err($crate::format_err!($msg))
27 }
28 };
29 ($cond:expr, $msg:expr $(,)?) => {
30if !$cond {
31return $crate::private::Err($crate::format_err!($msg))
32 }
33 };
34 ($cond:expr, $msg:expr, $($arg:tt)*) => {
35if !$cond {
36return $crate::private::Err($crate::format_err!($msg, $($arg)*))
37 }
38 };
39}
4041/// Return early with an error if two expressions are not equal to each other.
42///
43/// This macro is equivalent to `if $left != $right { return Err(From::from($err)); }`.
44///
45/// Analogously to `assert_eq!`, `ensure_eq!` takes two expressions and exits the function
46/// if the expressions are not equal. Unlike `assert_eq!`, `ensure_eq!` returns an `Error`
47/// rather than panicking.
48#[macro_export]
49macro_rules! ensure_eq {
50 ($left:expr, $right:expr, $msg:literal $(,)?) => {
51if $left != $right {
52return $crate::private::Err($crate::format_err!($msg))
53 }
54 };
55 ($left:expr, $right:expr, $msg:expr $(,)?) => {
56if $left != $right {
57return $crate::private::Err($crate::format_err!($msg))
58 }
59 };
60 ($left:expr, $right:expr, $msg:expr, $($arg:tt)*) => {
61if $left != $right {
62return $crate::private::Err($crate::format_err!($msg, $($arg)*))
63 }
64 };
65}
6667/// Construct an ad-hoc error from a string.
68///
69/// This evaluates to an `Error`. It can take either just a string, or a format
70/// string with arguments. It also can take any custom type which implements
71/// `Debug` and `Display`.
72#[macro_export]
73macro_rules! format_err {
74 ($msg:literal $(,)?) => {
75// Handle $:literal as a special case to make cargo-expanded code more
76 // concise in the common case.
77$crate::private::new_adhoc($msg)
78 };
79 ($err:expr $(,)?) => ({
80let error = $err;
81 Error::new_adhoc(error)
82 });
83 ($fmt:expr, $($arg:tt)*) => {
84$crate::private::new_adhoc(format!($fmt, $($arg)*))
85 };
86}
8788/// Return early with an error and a status code.
89#[doc(hidden)]
90#[macro_export]
91macro_rules! bail_status {
92 ($status:literal, $msg:literal $(,)?) => {{
93return $crate::private::Err($crate::format_err_status!($status, $msg))
94 }};
95 ($status:literal, $msg:expr $(,)?) => {
96return $crate::private::Err($crate::format_err_status!($status, $msg))
97 };
98 ($status:literal, $msg:expr, $($arg:tt)*) => {
99return $crate::private::Err($crate::format_err_status!($status, $msg, $($arg)*))
100 };
101}
102103/// Return early with an error if a condition is not satisfied.
104///
105/// This macro is equivalent to `if !$cond { return Err(From::from($err)); }`.
106///
107/// Analogously to `assert!`, `ensure!` takes a condition and exits the function
108/// if the condition fails. Unlike `assert!`, `ensure!` returns an `Error`
109/// rather than panicking.
110#[doc(hidden)]
111#[macro_export]
112macro_rules! ensure_status {
113 ($cond:expr, $status:literal, $msg:literal $(,)?) => {
114if !$cond {
115return $crate::private::Err($crate::format_err_status!($status, $msg))
116 }
117 };
118 ($cond:expr, $status:literal, $msg:expr $(,)?) => {
119if !$cond {
120return $crate::private::Err($crate::format_err_status!($status, $msg))
121 }
122 };
123 ($cond:expr, $status:literal, $msg:expr, $($arg:tt)*) => {
124if !$cond {
125return $crate::private::Err($crate::format_err_status!($status, $msg, $($arg)*))
126 }
127 };
128}
129130/// Return early with an error if two expressions are not equal to each other.
131///
132/// This macro is equivalent to `if $left != $right { return Err(From::from($err)); }`.
133///
134/// Analogously to `assert_eq!`, `ensure_eq!` takes two expressions and exits the function
135/// if the expressions are not equal. Unlike `assert_eq!`, `ensure_eq!` returns an `Error`
136/// rather than panicking.
137#[doc(hidden)]
138#[macro_export]
139macro_rules! ensure_eq_status {
140 ($left:expr, $right:expr, $status:literal, $msg:literal $(,)?) => {
141if $left != $right {
142return $crate::private::Err($crate::format_err_status!($status, $msg))
143 }
144 };
145 ($left:expr, $right:expr, $status:literal, $msg:expr $(,)?) => {
146if $left != $right {
147return $crate::private::Err($crate::format_err_status!($status, $msg))
148 }
149 };
150 ($left:expr, $right:expr, $status:literal, $msg:expr, $($arg:tt)*) => {
151if $left != $right {
152return $crate::private::Err($crate::format_err_status!($status, $msg, $($arg)*))
153 }
154 };
155}
156157/// Construct an ad-hoc error from a string.
158///
159/// This evaluates to an `Error`. It can take either just a string, or a format
160/// string with arguments. It also can take any custom type which implements
161/// `Debug` and `Display`.
162#[doc(hidden)]
163#[macro_export]
164macro_rules! format_err_status {
165 ($status:literal, $msg:literal $(,)?) => {{
166// Handle $:literal as a special case to make cargo-expanded code more
167 // concise in the common case.
168let mut err = $crate::private::new_adhoc($msg);
169 err.set_status($status);
170 err
171 }};
172 ($status:literal, $msg:expr $(,)?) => {{
173let mut err = $crate::private::new_adhoc($msg);
174 err.set_status($status);
175 err
176 }};
177 ($status:literal, $msg:expr, $($arg:tt)*) => {{
178let mut err = $crate::private::new_adhoc(format!($msg, $($arg)*));
179 err.set_status($status);
180 err
181 }};
182}