http_types/
macros.rs

1/// Return early with an error.
2#[macro_export]
3macro_rules! bail {
4    ($msg:literal $(,)?) => {
5        return $crate::private::Err($crate::format_err!($msg))
6    };
7    ($msg:expr $(,)?) => {
8        return $crate::private::Err($crate::format_err!($msg))
9    };
10    ($msg:expr, $($arg:tt)*) => {
11        return $crate::private::Err($crate::format_err!($msg, $($arg)*))
12    };
13}
14
15/// 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 $(,)?) => {
25        if !$cond {
26            return $crate::private::Err($crate::format_err!($msg))
27        }
28    };
29    ($cond:expr, $msg:expr $(,)?) => {
30        if !$cond {
31            return $crate::private::Err($crate::format_err!($msg))
32        }
33    };
34    ($cond:expr, $msg:expr, $($arg:tt)*) => {
35        if !$cond {
36            return $crate::private::Err($crate::format_err!($msg, $($arg)*))
37        }
38    };
39}
40
41/// 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 $(,)?) => {
51        if $left != $right {
52            return $crate::private::Err($crate::format_err!($msg))
53        }
54    };
55    ($left:expr, $right:expr, $msg:expr $(,)?) => {
56        if $left != $right {
57            return $crate::private::Err($crate::format_err!($msg))
58        }
59    };
60    ($left:expr, $right:expr, $msg:expr, $($arg:tt)*) => {
61        if $left != $right {
62            return $crate::private::Err($crate::format_err!($msg, $($arg)*))
63        }
64    };
65}
66
67/// 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 $(,)?) => ({
80        let error = $err;
81        Error::new_adhoc(error)
82    });
83    ($fmt:expr, $($arg:tt)*) => {
84        $crate::private::new_adhoc(format!($fmt, $($arg)*))
85    };
86}
87
88/// Return early with an error and a status code.
89#[doc(hidden)]
90#[macro_export]
91macro_rules! bail_status {
92    ($status:literal, $msg:literal $(,)?) => {{
93        return $crate::private::Err($crate::format_err_status!($status, $msg))
94    }};
95    ($status:literal, $msg:expr $(,)?) => {
96        return $crate::private::Err($crate::format_err_status!($status, $msg))
97    };
98    ($status:literal, $msg:expr, $($arg:tt)*) => {
99        return $crate::private::Err($crate::format_err_status!($status, $msg, $($arg)*))
100    };
101}
102
103/// 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 $(,)?) => {
114        if !$cond {
115            return $crate::private::Err($crate::format_err_status!($status, $msg))
116        }
117    };
118    ($cond:expr, $status:literal, $msg:expr $(,)?) => {
119        if !$cond {
120            return $crate::private::Err($crate::format_err_status!($status, $msg))
121        }
122    };
123    ($cond:expr, $status:literal, $msg:expr, $($arg:tt)*) => {
124        if !$cond {
125            return $crate::private::Err($crate::format_err_status!($status, $msg, $($arg)*))
126        }
127    };
128}
129
130/// 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 $(,)?) => {
141        if $left != $right {
142            return $crate::private::Err($crate::format_err_status!($status, $msg))
143        }
144    };
145    ($left:expr, $right:expr, $status:literal, $msg:expr $(,)?) => {
146        if $left != $right {
147            return $crate::private::Err($crate::format_err_status!($status, $msg))
148        }
149    };
150    ($left:expr, $right:expr, $status:literal, $msg:expr, $($arg:tt)*) => {
151        if $left != $right {
152            return $crate::private::Err($crate::format_err_status!($status, $msg, $($arg)*))
153        }
154    };
155}
156
157/// 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.
168        let mut err = $crate::private::new_adhoc($msg);
169        err.set_status($status);
170        err
171    }};
172    ($status:literal, $msg:expr $(,)?) => {{
173        let mut err = $crate::private::new_adhoc($msg);
174        err.set_status($status);
175        err
176    }};
177    ($status:literal, $msg:expr, $($arg:tt)*) => {{
178        let mut err = $crate::private::new_adhoc(format!($msg, $($arg)*));
179        err.set_status($status);
180        err
181    }};
182}