Skip to content

Commit 345224f

Browse files
committed
update macros; add v2 variants for internal use
1 parent fa4bdb9 commit 345224f

File tree

2 files changed

+110
-12
lines changed

2 files changed

+110
-12
lines changed

src/auth/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
mod authentication_scheme;
2727
mod authorization;
2828
mod basic_auth;
29+
mod www_authenticate;
2930

3031
pub use authentication_scheme::AuthenticationScheme;
3132
pub use authorization::Authorization;
3233
pub use basic_auth::BasicAuth;
34+
pub use www_authenticate::WwwAuthenticate;

src/macros.rs

Lines changed: 108 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ macro_rules! bail {
44
($msg:literal $(,)?) => {
55
return $crate::private::Err($crate::format_err!($msg));
66
};
7-
($err:expr $(,)?) => {
8-
return $crate::private::Err($crate::format_err!($err));
7+
($msg:expr $(,)?) => {
8+
return $crate::private::Err($crate::format_err!($msg));
99
};
10-
($fmt:expr, $($arg:tt)*) => {
11-
return $crate::private::Err($crate::format_err!($fmt, $($arg)*));
10+
($msg:expr, $($arg:tt)*) => {
11+
return $crate::private::Err($crate::format_err!($msg, $($arg)*));
1212
};
1313
}
1414

@@ -26,14 +26,14 @@ macro_rules! ensure {
2626
return $crate::private::Err($crate::format_err!($msg));
2727
}
2828
};
29-
($cond:expr, $err:expr $(,)?) => {
29+
($cond:expr, $msg:expr $(,)?) => {
3030
if !$cond {
31-
return $crate::private::Err($crate::format_err!($err));
31+
return $crate::private::Err($crate::format_err!($msg));
3232
}
3333
};
34-
($cond:expr, $fmt:expr, $($arg:tt)*) => {
34+
($cond:expr, $msg:expr, $($arg:tt)*) => {
3535
if !$cond {
36-
return $crate::private::Err($crate::format_err!($fmt, $($arg)*));
36+
return $crate::private::Err($crate::format_err!($msg, $($arg)*));
3737
}
3838
};
3939
}
@@ -52,14 +52,14 @@ macro_rules! ensure_eq {
5252
return $crate::private::Err($crate::format_err!($msg));
5353
}
5454
};
55-
($left:expr, $right:expr, $err:expr $(,)?) => {
55+
($left:expr, $right:expr, $msg:expr $(,)?) => {
5656
if $left != $right {
57-
return $crate::private::Err($crate::format_err!($err));
57+
return $crate::private::Err($crate::format_err!($msg));
5858
}
5959
};
60-
($left:expr, $right:expr, $fmt:expr, $($arg:tt)*) => {
60+
($left:expr, $right:expr, $msg:expr, $($arg:tt)*) => {
6161
if $left != $right {
62-
return $crate::private::Err($crate::format_err!($fmt, $($arg)*));
62+
return $crate::private::Err($crate::format_err!($msg, $($arg)*));
6363
}
6464
};
6565
}
@@ -84,3 +84,99 @@ macro_rules! format_err {
8484
$crate::private::new_adhoc(format!($fmt, $($arg)*))
8585
};
8686
}
87+
88+
/// Return early with an error and a status code.
89+
#[doc(hidden)]
90+
#[macro_export]
91+
macro_rules! bail2 {
92+
($status:literal, $msg:literal $(,)?) => {{
93+
return $crate::private::Err($crate::format_err2!($status, $msg));
94+
}};
95+
($status:literal, $msg:expr $(,)?) => {
96+
return $crate::private::Err($crate::format_err2!($status, $msg));
97+
};
98+
($status:literal, $msg:expr, $($arg:tt)*) => {
99+
return $crate::private::Err($crate::format_err2!($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]
112+
macro_rules! ensure2 {
113+
($cond:expr, $status:literal, $msg:literal $(,)?) => {
114+
if !$cond {
115+
return $crate::private::Err($crate::format_err2!($status, $msg));
116+
}
117+
};
118+
($cond:expr, $status:literal, $msg:expr $(,)?) => {
119+
if !$cond {
120+
return $crate::private::Err($crate::format_err2!($status, $msg));
121+
}
122+
};
123+
($cond:expr, $status:literal, $msg:expr, $($arg:tt)*) => {
124+
if !$cond {
125+
return $crate::private::Err($crate::format_err2!($status, $fmt, $($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]
139+
macro_rules! ensure_eq2 {
140+
($left:expr, $right:expr, $status:literal, $msg:literal $(,)?) => {
141+
if $left != $right {
142+
return $crate::private::Err($crate::format_err2!($status, $msg));
143+
}
144+
};
145+
($left:expr, $right:expr, $status:literal, $msg:expr $(,)?) => {
146+
if $left != $right {
147+
return $crate::private::Err($crate::format_err2!($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_err2!($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]
164+
macro_rules! format_err2 {
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+
}

0 commit comments

Comments
 (0)