|
3 | 3 |
|
4 | 4 | //! Macros for the `app` module. |
5 | 5 |
|
6 | | -/// Return early with an [`AppError`](crate::AppError). |
7 | | -/// |
8 | | -/// The macro accepts: |
9 | | -/// - A string literal: `bail!("error message")` |
10 | | -/// - An error expression: `bail!(MyAppError::new())` |
11 | | -/// - A format string with arguments: `bail!("error: {value}")` |
12 | | -/// |
13 | | -/// # Examples |
14 | | -/// |
15 | | -/// ```rust |
16 | | -/// use ohno::{AppError, bail}; |
17 | | -/// |
18 | | -/// fn check_value(x: i32) -> Result<(), AppError> { |
19 | | -/// if x < 0 { |
20 | | -/// bail!("value must be non-negative, got {x}"); |
21 | | -/// } |
22 | | -/// Ok(()) |
23 | | -/// } |
24 | | -/// ``` |
25 | | -/// |
26 | | -/// ```rust |
27 | | -/// use ohno::{AppError, bail}; |
28 | | -/// |
29 | | -/// fn parse_config(data: &str) -> Result<(), AppError> { |
30 | | -/// if data.is_empty() { |
31 | | -/// bail!("config data cannot be empty"); |
32 | | -/// } |
33 | | -/// Ok(()) |
34 | | -/// } |
35 | | -/// ``` |
36 | | -/// |
37 | | -/// Bailing with an expression: |
38 | | -/// |
39 | | -/// ```rust |
40 | | -/// use ohno::{AppError, bail}; |
41 | | -/// |
42 | | -/// fn read_file(path: &str) -> Result<String, AppError> { |
43 | | -/// bail!(std::io::Error::from(std::io::ErrorKind::PermissionDenied)); |
44 | | -/// } |
45 | | -/// ``` |
46 | | -#[macro_export] |
47 | | -#[cfg_attr(docsrs, doc(cfg(feature = "app-err")))] |
48 | | -macro_rules! bail { |
49 | | - ($msg:literal $(,)?) => { |
50 | | - return Err($crate::AppError::new(format!($msg))) |
51 | | - }; |
52 | | - ($fmt:expr, $($arg:tt)*) => { |
53 | | - return Err($crate::AppError::new(format!($fmt, $($arg)*))) |
54 | | - }; |
55 | | - ($err:ident $(,)?) => { |
56 | | - return Err($crate::AppError::new($err)) |
57 | | - }; |
58 | | - ($err:expr $(,)?) => { |
59 | | - return Err($crate::AppError::new($err)) |
60 | | - }; |
61 | | -} |
62 | | - |
63 | 6 | /// Construct an [`AppError`](crate::AppError) in place. |
64 | 7 | /// |
65 | 8 | /// The macro accepts: |
@@ -113,3 +56,51 @@ macro_rules! app_err { |
113 | 56 | $crate::AppError::new($err) |
114 | 57 | }; |
115 | 58 | } |
| 59 | + |
| 60 | +/// Return early with an [`AppError`](crate::AppError). |
| 61 | +/// |
| 62 | +/// The macro accepts: |
| 63 | +/// - A string literal: `bail!("error message")` |
| 64 | +/// - An error expression: `bail!(MyAppError::new())` |
| 65 | +/// - A format string with arguments: `bail!("error: {value}")` |
| 66 | +/// |
| 67 | +/// # Examples |
| 68 | +/// |
| 69 | +/// ```rust |
| 70 | +/// use ohno::{AppError, bail}; |
| 71 | +/// |
| 72 | +/// fn check_value(x: i32) -> Result<(), AppError> { |
| 73 | +/// if x < 0 { |
| 74 | +/// bail!("value must be non-negative, got {x}"); |
| 75 | +/// } |
| 76 | +/// Ok(()) |
| 77 | +/// } |
| 78 | +/// ``` |
| 79 | +/// |
| 80 | +/// ```rust |
| 81 | +/// use ohno::{AppError, bail}; |
| 82 | +/// |
| 83 | +/// fn parse_config(data: &str) -> Result<(), AppError> { |
| 84 | +/// if data.is_empty() { |
| 85 | +/// bail!("config data cannot be empty"); |
| 86 | +/// } |
| 87 | +/// Ok(()) |
| 88 | +/// } |
| 89 | +/// ``` |
| 90 | +/// |
| 91 | +/// Bailing with an expression: |
| 92 | +/// |
| 93 | +/// ```rust |
| 94 | +/// use ohno::{AppError, bail}; |
| 95 | +/// |
| 96 | +/// fn read_file(path: &str) -> Result<String, AppError> { |
| 97 | +/// bail!(std::io::Error::from(std::io::ErrorKind::PermissionDenied)); |
| 98 | +/// } |
| 99 | +/// ``` |
| 100 | +#[macro_export] |
| 101 | +#[cfg_attr(docsrs, doc(cfg(feature = "app-err")))] |
| 102 | +macro_rules! bail{ |
| 103 | + ($($arg:tt)*) => ( |
| 104 | + return Err($crate::app_err!($($arg)*)) |
| 105 | + ); |
| 106 | +} |
0 commit comments