Skip to main content

zrx_diagnostic/diagnostic/
macros.rs

1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the DCO
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Macros for diagnostic creation.
27
28// ----------------------------------------------------------------------------
29// Macros
30// ----------------------------------------------------------------------------
31
32/// Creates a diagnostic with error severity.
33///
34/// This macro creates a diagnostic message with [`Severity::Error`][], using
35/// either a static string or a format string as an argument.
36///
37/// The diagnostic will include file, line and column information.
38///
39/// [`Severity::Error`]: crate::diagnostic::Severity::Error
40///
41/// # Examples
42///
43/// ```
44/// use zrx_diagnostic::error;
45///
46/// // Create diagnostic with static string
47/// let diagnostic = error!("Static");
48///
49/// // Create diagnostic with format string
50/// let diagnostic = error!("Format: {}", true);
51/// ```
52#[macro_export]
53macro_rules! error {
54    // Create and append diagnostic
55    (&mut $sink:expr, $($arg:tt)+) => {
56        $sink.push($crate::error!($($arg)+))
57    };
58    // Create and return diagnostic
59    ($($arg:tt)*) => {
60        $crate::__diagnostic!(
61            $crate::Severity::Error,
62            $($arg)*
63        )
64    };
65}
66
67/// Creates a diagnostic with warning severity.
68///
69/// This macro creates a diagnostic message with [`Severity::Warning`][], using
70/// either a static string or a format string as an argument.
71///
72/// The diagnostic will include file, line and column information.
73///
74/// [`Severity::Warning`]: crate::diagnostic::Severity::Warning
75///
76/// # Examples
77///
78/// ```
79/// use zrx_diagnostic::warning;
80///
81/// // Create diagnostic with static string
82/// let diagnostic = warning!("Static");
83///
84/// // Create diagnostic with format string
85/// let diagnostic = warning!("Format: {}", true);
86/// ```
87#[macro_export]
88macro_rules! warning {
89    // Create and append diagnostic
90    (&mut $sink:expr, $($arg:tt)+) => {
91        $sink.push($crate::warning!($($arg)+))
92    };
93    // Create and return diagnostic
94    ($($arg:tt)*) => {
95        $crate::__diagnostic!(
96            $crate::Severity::Warning,
97            $($arg)*
98        )
99    };
100}
101
102/// Creates a diagnostic with info severity.
103///
104/// This macro creates a diagnostic message with [`Severity::Info`][], using
105/// either a static string or a format string as an argument.
106///
107/// The diagnostic will include file, line and column information.
108///
109/// [`Severity::Info`]: crate::diagnostic::Severity::Info
110///
111/// # Examples
112///
113/// ```
114/// use zrx_diagnostic::info;
115///
116/// // Create diagnostic with static string
117/// let diagnostic = info!("Static");
118///
119/// // Create diagnostic with format string
120/// let diagnostic = info!("Format: {}", true);
121/// ```
122#[macro_export]
123macro_rules! info {
124    // Create and append diagnostic
125    (&mut $sink:expr, $($arg:tt)+) => {
126        $sink.push($crate::info!($($arg)+))
127    };
128    // Create and return diagnostic
129    ($($arg:tt)*) => {
130        $crate::__diagnostic!(
131            $crate::Severity::Info,
132            $($arg)*
133        )
134    };
135}
136
137/// Creates a diagnostic with hint severity.
138///
139/// This macro creates a diagnostic message with [`Severity::Hint`][], using
140/// either a static string or a format string as an argument.
141///
142/// The diagnostic will include file, line and column information.
143///
144/// [`Severity::Hint`]: crate::diagnostic::Severity::Hint
145///
146/// # Examples
147///
148/// ```
149/// use zrx_diagnostic::hint;
150///
151/// // Create diagnostic with static string
152/// let diagnostic = hint!("Static");
153///
154/// // Create diagnostic with format string
155/// let diagnostic = hint!("Format: {}", true);
156/// ```
157#[macro_export]
158macro_rules! hint {
159    // Create and append diagnostic
160    (&mut $sink:expr, $($arg:tt)+) => {
161        $sink.push($crate::hint!($($arg)+))
162    };
163    // Create and return diagnostic
164    ($($arg:tt)*) => {
165        $crate::__diagnostic!(
166            $crate::Severity::Hint,
167            $($arg)*
168        )
169    };
170}
171
172/// Creates a diagnostic with debug severity.
173///
174/// This macro creates a diagnostic message with [`Severity::Debug`][], using
175/// either a static string or a format string as an argument.
176///
177/// The diagnostic will include file, line and column information.
178///
179/// [`Severity::Debug`]: crate::diagnostic::Severity::Debug
180///
181/// # Examples
182///
183/// ```
184/// use zrx_diagnostic::debug;
185///
186/// // Create diagnostic with static string
187/// let diagnostic = debug!("Static");
188///
189/// // Create diagnostic with format string
190/// let diagnostic = debug!("Format: {}", true);
191/// ```
192#[macro_export]
193macro_rules! debug {
194    // Create and append diagnostic
195    (&mut $sink:expr, $($arg:tt)+) => {
196        $sink.push($crate::debug!($($arg)+))
197    };
198    // Create and return diagnostic
199    ($($arg:tt)*) => {
200        $crate::__diagnostic!(
201            $crate::Severity::Debug,
202            $($arg)*
203        )
204    };
205}
206
207// ----------------------------------------------------------------------------
208
209/// Creates a diagnostic with the given severity.
210///
211/// Note that this is an internal helper macro to create a diagnostics either
212/// from a static string or format string, as well as the given severity. It's
213/// not intended to be used directly. Use the severity-specific implementations
214/// like [`error!`], [`warning!`] and friends instead.
215#[doc(hidden)]
216#[macro_export]
217macro_rules! __diagnostic {
218    // Create diagnostic with severity and static string
219    ($severity:expr, $message:expr) => {
220        $crate::Diagnostic::new($severity, $message)
221            .location($crate::location!())
222    };
223    // Create diagnostic with severity and format string
224    ($severity:expr, $fmt:expr, $($arg:tt)*) => {
225        $crate::Diagnostic::new($severity, format!($fmt, $($arg)*))
226            .location($crate::location!())
227    };
228}