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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2025 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Creates an [`Exn`] and converts it to the function's result error type.
///
/// Shorthand for `return Err(Exn::from(err).into())`.
/// Works with both [`crate::Result`] and typed [`crate::ExnResult`].
/// Prefer to import the macro with `use gix_error::bail;` (or `use gix::error::bail;` in applications)
/// and invoke it as `bail!(...)` instead of using a qualified path for readability.
///
/// String literals and format arguments implicitly construct a formatted [`Message`](crate::Message),
/// including captured arguments like `bail!("invalid input: {input}")`.
/// Like [`message!`](crate::message!), this always formats the message; use [`message()`](crate::message())
/// inside `bail!` for a static message without formatting.
/// A string literal can be followed by method calls on the formatted [`Message`](crate::Message),
/// such as `bail!("invalid record at {offset}".corrupted().with("offset", offset))`.
/// Explicit format arguments follow the method chain: `bail!("invalid record at {}".corrupted(), offset)`.
/// These methods apply to the message after formatting, not to the string literal itself.
/// Parenthesize an ordinary error expression starting with a literal to avoid this shorthand,
/// for example `bail!(("bad".parse::<u32>().expect_err("invalid number")))`.
/// Without a classification builder, the shorthand leaves the error unclassified.
/// Classified constructors also work, such as `bail!(gix_error::validation("invalid input"))`.
///
/// # Examples
///
/// Create an [`Exn`] from [`Error`]:
///
/// [`Exn`]: crate::Exn
/// [`Error`]: std::error::Error
///
/// ```
/// use std::fs;
///
/// use gix_error::{bail, ExnResult};
/// # fn wrapper() -> ExnResult<(), std::io::Error> {
/// match fs::read_to_string("/path/to/file") {
/// Ok(content) => println!("file contents: {content}"),
/// Err(err) => bail!(err),
/// }
/// # Ok(()) }
/// ```
///
/// ```
/// use gix_error::{bail, Result};
///
/// fn public_api() -> Result {
/// bail!(gix_error::validation("invalid input"));
/// }
/// assert!(public_api().expect_err("the input is invalid").is_validation());
/// ```
///
/// Return a formatted message with captured or explicit arguments:
///
/// ```
/// use gix_error::{bail, ExnMessageResult, Result};
///
/// fn captured(name: &str) -> Result {
/// bail!("unknown executable '{name}'");
/// }
///
/// fn explicit(name: &str) -> ExnMessageResult {
/// bail!("unknown executable '{}'", name);
/// }
/// # assert!(captured("other").is_err());
/// # assert!(explicit("other").is_err());
/// ```
///
/// Chain classification and metadata builders on a formatted message:
///
/// ```
/// use std::path::Path;
/// use gix_error::{bail, ExnMessageResult, MetadataValue, Result};
///
/// fn captured(path: &Path) -> Result {
/// bail!("Missing reference".not_found().with("path", path));
/// }
///
/// fn explicit(path: &Path) -> ExnMessageResult {
/// bail!("Missing reference at '{}'".not_found().with("path", path), path.display());
/// }
///
/// let path = Path::new("refs/heads/main");
/// let error = captured(path).expect_err("the reference is missing");
/// assert!(error.is_not_found());
/// assert_eq!(error.metadata().next().expect("reference details")["path"], MetadataValue::Path(path.into()));
/// let error = explicit(path).expect_err("the reference is missing");
/// assert_eq!(error.error().message, "Missing reference at 'refs/heads/main'");
/// assert!(error.is_not_found());
/// ```
;
=> ;
// Strip the grouping used to opt out of builder shorthand before it can trigger `unused_parens`.
=> ;
=> ;
=> ;
}
/// Ensures `$cond` is met; otherwise return an error.
///
/// Shorthand for `if !$cond { bail!(...); }`.
/// Accepts the same error expressions, format arguments, and message builder chains as [`bail!`].
/// The condition is evaluated once; error, format, and builder arguments are evaluated only on failure.
///
/// # Examples
///
/// Create an [`Exn`] from an [`Error`]:
///
/// [`Exn`]: crate::Exn
/// [`Error`]: std::error::Error
///
/// ```
/// # fn has_permission(_: &u32, _: &u32) -> bool { true }
/// # type User = u32;
/// # let user = 0;
/// # type Resource = u32;
/// # let resource = 0;
/// use std::error::Error;
/// use std::fmt;
///
/// use gix_error::ensure;
///
/// #[derive(Debug)]
/// struct PermissionDenied(User, Resource);
///
/// impl fmt::Display for PermissionDenied {
/// fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(fmt, "permission denied")
/// }
/// }
///
/// impl Error for PermissionDenied {}
///
/// ensure!(
/// has_permission(&user, &resource),
/// PermissionDenied(user, resource),
/// );
/// # Ok::<(), gix_error::Error>(())
/// ```
///
/// Format a classified failure and attach metadata:
///
/// ```
/// use gix_error::{ensure, ExnMessageResult, MetadataValue, Result};
///
/// fn captured(count: usize) -> Result {
/// ensure!(count > 0, "Count must be positive, got {count}".validation().with_input(count));
/// Ok(())
/// }
///
/// fn explicit(count: usize) -> ExnMessageResult {
/// ensure!(count > 0, "Count must be positive, got {}".validation().with_input(count), count);
/// Ok(())
/// }
///
/// captured(1)?;
/// let error = captured(0).expect_err("zero violates the input constraint");
/// assert!(error.is_validation());
/// assert_eq!(error.metadata().next().expect("input details")["input"], MetadataValue::U64(0));
/// let error = explicit(0).expect_err("zero violates the input constraint");
/// assert_eq!(error.error().message, "Count must be positive, got 0");
/// assert!(error.is_validation());
/// # Ok::<(), gix_error::Error>(())
/// ```
/// Construct a [`Message`](crate::Message) from a string literal or format string.
/// Note that it always runs `format!()`, use the [`message()`](crate::message()) function for literals instead.