Debian Codegraph @

src/exn/macros.rs

raw symbols SCIP
1 // Copyright 2025 FastLabs Developers
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /// Creates an [`Exn`] and returns it as [`Result`].
16 ///
17 /// Shorthand for `return Err(Exn::from(err))`.
18 ///
19 /// # Examples
20 ///
21 /// Create an [`Exn`] from [`Error`]:
22 ///
23 /// [`Exn`]: crate::Exn
24 /// [`Error`]: std::error::Error
25 ///
26 /// ```
27 /// use std::fs;
28 ///
29 /// use gix_error::bail;
30 /// # fn wrapper() -> Result<(), gix_error::Exn<std::io::Error>> {
31 /// match fs::read_to_string("/path/to/file") {
32 /// Ok(content) => println!("file contents: {content}"),
33 /// Err(err) => bail!(err),
34 /// }
35 /// # Ok(()) }
36 /// ```
37 #[macro_export]
38 macro_rules! bail {
39 ($err:expr) => {{
40 return ::std::result::Result::Err($crate::Exn::from($err));
41 }};
42 }
43
44 /// Ensures `$cond` is met; otherwise return an error.
45 ///
46 /// Shorthand for `if !$cond { bail!(...); }`.
47 ///
48 /// # Examples
49 ///
50 /// Create an [`Exn`] from an [`Error`]:
51 ///
52 /// [`Exn`]: crate::Exn
53 /// [`Error`]: std::error::Error
54 ///
55 /// ```
56 /// # fn has_permission(_: &u32, _: &u32) -> bool { true }
57 /// # type User = u32;
58 /// # let user = 0;
59 /// # type Resource = u32;
60 /// # let resource = 0;
61 /// use std::error::Error;
62 /// use std::fmt;
63 ///
64 /// use gix_error::ensure;
65 ///
66 /// #[derive(Debug)]
67 /// struct PermissionDenied(User, Resource);
68 ///
69 /// impl fmt::Display for PermissionDenied {
70 /// fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
71 /// write!(fmt, "permission denied")
72 /// }
73 /// }
74 ///
75 /// impl Error for PermissionDenied {}
76 ///
77 /// ensure!(
78 /// has_permission(&user, &resource),
79 /// PermissionDenied(user, resource),
80 /// );
81 /// # Ok(())
82 /// ```
83 #[macro_export]
84 macro_rules! ensure {
85 ($cond:expr, $err:expr $(,)?) => {{
86 if !bool::from($cond) {
87 $crate::bail!($err)
88 }
89 }};
90 }
91
92 /// Construct a [`Message`](crate::Message) from a string literal or format string.
93 /// Note that it always runs `format!()`, use the [`message()`](crate::message()) function for literals instead.
94 #[macro_export]
95 macro_rules! message {
96 ($message_with_format_args:literal $(,)?) => {
97 $crate::Message::new(format!($message_with_format_args))
98 };
99 ($fmt:expr, $($arg:tt)*) => {
100 $crate::Message::new(format!($fmt, $($arg)*))
101 };
102 }