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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//! Macros for the `app` module.
/// Construct an [`AppError`](crate::AppError) in place.
///
/// The macro accepts:
/// - A string literal: `app_err!("error message")`
/// - An error expression: `app_err!(MyError::new())`
/// - A format string with arguments: `app_err!("error: {value}")`
///
/// # Examples
///
/// ```rust
/// use ohno::{AppError, app_err};
///
/// // Create an error from a string literal
/// let error = app_err!("something went wrong");
/// ```
///
/// ```rust
/// use ohno::{AppError, app_err};
///
/// fn validate(x: i32) -> Result<(), AppError> {
/// if x < 0 {
/// return Err(app_err!("value must be non-negative, got {x}"));
/// }
/// Ok(())
/// }
/// ```
///
/// Creating an error from another error type:
///
/// ```rust
/// use ohno::app_err;
///
/// fn read_file() {
/// let result = std::fs::read_to_string("file.txt").map_err(|e| app_err!(e));
/// }
/// ```
/// Return early with an [`AppError`](crate::AppError).
///
/// The macro accepts:
/// - A string literal: `bail!("error message")`
/// - An error expression: `bail!(MyAppError::new())`
/// - A format string with arguments: `bail!("error: {value}")`
///
/// # Examples
///
/// ```rust
/// use ohno::{AppError, bail};
///
/// fn check_value(x: i32) -> Result<(), AppError> {
/// if x < 0 {
/// bail!("value must be non-negative, got {x}");
/// }
/// Ok(())
/// }
/// ```
///
/// ```rust
/// use ohno::{AppError, bail};
///
/// fn parse_config(data: &str) -> Result<(), AppError> {
/// if data.is_empty() {
/// bail!("config data cannot be empty");
/// }
/// Ok(())
/// }
/// ```
///
/// Bailing with an expression:
///
/// ```rust
/// use ohno::{AppError, bail};
///
/// fn read_file(path: &str) -> Result<String, AppError> {
/// bail!(std::io::Error::from(std::io::ErrorKind::PermissionDenied));
/// }
/// ```