audb_core/tools/
macros.rs1use colored::Colorize;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum OutputLevel {
10 Info,
12 Success,
14 Warning,
16 Error,
18 State,
20}
21
22impl OutputLevel {
23 fn color(&self) -> colored::Color {
25 match self {
26 OutputLevel::Info => colored::Color::Blue,
27 OutputLevel::Success => colored::Color::Green,
28 OutputLevel::Warning => colored::Color::Yellow,
29 OutputLevel::Error => colored::Color::Red,
30 OutputLevel::State => colored::Color::Cyan,
31 }
32 }
33
34 fn label(&self) -> &'static str {
36 match self {
37 OutputLevel::Info => "info",
38 OutputLevel::Success => "success",
39 OutputLevel::Warning => "warning",
40 OutputLevel::Error => "error",
41 OutputLevel::State => "state",
42 }
43 }
44
45 fn should_error_exit(&self) -> bool {
47 matches!(self, OutputLevel::Error)
48 }
49}
50
51pub fn print_msg(level: OutputLevel, message: impl AsRef<str>) {
61 let label = level.label().color(level.color()).bold();
62 println!("{}: {}", label, message.as_ref());
63}
64
65pub fn print_error(message: impl AsRef<str>) {
67 print_msg(OutputLevel::Error, message);
68}
69
70pub fn print_info(message: impl AsRef<str>) {
72 print_msg(OutputLevel::Info, message);
73}
74
75pub fn print_warning(message: impl AsRef<str>) {
77 print_msg(OutputLevel::Warning, message);
78}
79
80pub fn print_success(message: impl AsRef<str>) {
82 print_msg(OutputLevel::Success, message);
83}
84
85pub fn print_state(message: impl AsRef<str>) {
87 print_msg(OutputLevel::State, message);
88}
89
90pub fn exit_with_msg(level: OutputLevel, message: impl AsRef<str>, lowercase: bool) -> ! {
101 let msg = if lowercase {
102 message.as_ref().to_lowercase()
103 } else {
104 message.as_ref().to_string()
105 };
106
107 let label = level.label().color(level.color()).bold();
108 println!("{}: {}", label, msg);
109
110 let exit_code = if level.should_error_exit() { 1 } else { 0 };
111 std::process::exit(exit_code);
112}
113
114pub fn exit_info(message: impl AsRef<str>) -> ! {
118 exit_with_msg(OutputLevel::Info, message, true);
119}
120
121pub fn exit_error(message: impl AsRef<str>) -> ! {
125 exit_with_msg(OutputLevel::Error, message, true);
126}
127
128#[deprecated(since = "0.1.0", note = "Use print_error function instead")]
133#[allow(unused_macros)]
134#[macro_export]
135macro_rules! print_error {
136 ($arg:tt) => {
137 $crate::tools::macros::print_error($arg)
138 };
139 ($($arg:tt)*) => {
140 $crate::tools::macros::print_error(&format!($($arg)*))
141 };
142}
143
144#[deprecated(since = "0.1.0", note = "Use print_info function instead")]
146#[allow(unused_macros)]
147#[macro_export]
148macro_rules! print_info {
149 ($arg:tt) => {
150 $crate::tools::macros::print_info($arg)
151 };
152 ($($arg:tt)*) => {
153 $crate::tools::macros::print_info(&format!($($arg)*))
154 };
155}
156
157#[deprecated(since = "0.1.0", note = "Use print_warning function instead")]
159#[allow(unused_macros)]
160#[macro_export]
161macro_rules! print_warning {
162 ($arg:tt) => {
163 $crate::tools::macros::print_warning($arg)
164 };
165 ($($arg:tt)*) => {
166 $crate::tools::macros::print_warning(&format!($($arg)*))
167 };
168}
169
170#[deprecated(since = "0.1.0", note = "Use print_success function instead")]
172#[allow(unused_macros)]
173#[macro_export]
174macro_rules! print_success {
175 ($arg:tt) => {
176 $crate::tools::macros::print_success($arg)
177 };
178 ($($arg:tt)*) => {
179 $crate::tools::macros::print_success(&format!($($arg)*))
180 };
181}
182
183#[deprecated(since = "0.1.0", note = "Use print_state function instead")]
185#[allow(unused_macros)]
186#[macro_export]
187macro_rules! print_state {
188 ($arg:tt) => {
189 $crate::tools::macros::print_state($arg)
190 };
191 ($($arg:tt)*) => {
192 $crate::tools::macros::print_state(&format!($($arg)*))
193 };
194}
195
196#[deprecated(since = "0.1.0", note = "Use exit_info function instead")]
198#[allow(unused_macros)]
199#[macro_export]
200macro_rules! exit_info {
201 ($arg:tt) => {
202 $crate::tools::macros::exit_info($arg)
203 };
204 ($($arg:tt)*) => {
205 $crate::tools::macros::exit_info(&format!($($arg)*))
206 };
207}
208
209#[deprecated(since = "0.1.0", note = "Use exit_error function instead")]
211#[allow(unused_macros)]
212#[macro_export]
213macro_rules! exit_error {
214 ($arg:tt) => {
215 $crate::tools::macros::exit_error($arg)
216 };
217 ($($arg:tt)*) => {
218 $crate::tools::macros::exit_error(&format!($($arg)*))
219 };
220}