carlog 0.1.0

Simple, lightweight crate that provides Cargo logging style messages
Documentation
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//! `carlog` is a simple, lightweight crate that provides Cargo logging style messages via the
//! `Status` struct or via multiple macros that recreate common cargo message formats:
//! * Cargo ok: `carlog_ok!`
//! * Cargo info: `carlog_info!`
//! * Cargo warning: `carlog_warning!`
//! * Cargo error: `carlog_error!`
//!
//! The crate provides support for logging to both stdout and stderr and to any stream that implements
//! the `Write` trait.
//!
//! ## Example
//! ```ignore
//! #[macro_use] extern crate carlog;
//!
//! use carlog::prelude::*;
//!
//! let status = Status::new().bold().justify().color(CargoColor::Green).status("Compiled");
//! status.print_stdout("carlog v0.1.0");
//!
//! carlog_ok!("Compiled", "carlog v0.1.0");
//! ```
//! Output:
//! <div style="padding: 1%; padding-left: 50px; background-color: black;">
//!     <span style="color: #16C60C;"><b>Compiled</b></span><span> carlog v0.1.0</span>
//! </div>

use colored::*;
use std::io;
use std::io::{stderr, stdout, Write};

/// Module to import required structs and enums to use this crate.
///
/// ## Example
/// ```
/// use carlog::prelude::*;
/// ```
pub mod prelude {
    pub use crate::CargoColor;
    pub use crate::CarlogStream;
    pub use crate::Status;
}

/// Cargo terminal colors.
#[derive(Copy, Clone)]
pub enum CargoColor {
    Green,
    Cyan,
    Yellow,
    Red,
    White,
    Black,
}

impl Default for CargoColor {
    fn default() -> Self {
        Self::White
    }
}

/// Carlog library streams.
///
/// This enum contains the two output standard streams:
/// * stdout
/// * stderr
/// It also has a custom variant which holds a mutable reference to a writeable stream (socket, vec, slice...).
///
/// The default variant is the stdout.
///
/// ## Example
/// ```
/// use carlog::prelude::*;
///
/// let stdout = CarlogStream::Stdout;
/// let stderr = CarlogStream::Stderr;
/// let mut output = Vec::<u8>::new();
/// let custom = CarlogStream::Custom(&mut output);
/// ```
pub enum CarlogStream<'a> {
    Stdout,
    Stderr,
    Custom(&'a mut dyn Write),
}

impl Default for CarlogStream<'_> {
    fn default() -> Self {
        Self::Stdout
    }
}

/// Simple cargo status log.
///
/// This is the part displayed before the actual message to be logged i.e. 'Compiled'.
///
/// ## Example
/// ```
/// use carlog::prelude::*;
///
/// let status = Status::new().bold().justify().color(CargoColor::Green).status("Compiled");
/// status.print_stdout("carlog v0.1.0");
/// ```
#[derive(Default)]
pub struct Status {
    /// If the status must be padded to 12 characters to the right using spaces.
    justify: bool,

    /// If the status must be bold.
    bold: bool,

    /// The color of the status.
    color: CargoColor,

    /// The string of the status.
    status: String,
}

impl Status {
    /// Creates a new empty status.
    ///
    /// The status has the default values:
    /// * <b>NO</b> justification.
    /// * <b>NO</b> boldness.
    /// * White color.
    /// * Empty status string.
    /// ## Example
    /// ```
    /// use carlog::prelude::*;
    ///
    /// let status = Status::new();
    /// ```
    pub fn new() -> Self {
        Self::default()
    }

    /// Justify the status.
    ///
    /// Sets the status to be padded to 12 characters to the right using spaces.
    /// ## Example
    /// ```
    /// use carlog::prelude::*;
    ///
    /// let status = Status::new().justify();
    /// ```
    pub fn justify(mut self) -> Self {
        self.justify = true;
        self
    }

    /// Set the status to be bold.
    ///
    /// ## Example
    /// ```
    /// use carlog::prelude::*;
    ///
    /// let status = Status::new().bold();
    /// ```
    pub fn bold(mut self) -> Self {
        self.bold = true;
        self
    }

    /// Set the color of the status.
    ///
    /// * `color`: The cargo color of the status.
    ///
    /// ## Example
    /// ```
    /// use carlog::prelude::*;
    ///
    /// let status = Status::new().color(CargoColor::Cyan);
    /// ```
    pub fn color(mut self, color: CargoColor) -> Self {
        self.color = color;
        self
    }

    /// Set the string status.
    ///
    /// * `str`: The status text from a type that can be converted to a string reference.
    ///
    /// ## Example
    /// ```
    /// use carlog::prelude::*;
    ///
    /// let status = Status::new().status("Text");
    /// ```
    pub fn status<S>(mut self, str: S) -> Self
    where
        S: AsRef<str>,
    {
        self.status = str.as_ref().to_string();
        self
    }

    /// Print the status to stdout.
    ///
    /// `msg`: The message to be printed alongside the status.
    ///
    /// ## Example
    /// ```
    /// use carlog::prelude::*;
    ///
    /// let status = Status::new().bold().justify().color(CargoColor::Green).status("Compiled");
    /// status.print_stdout("carlog v0.1.0");
    /// ```
    pub fn print_stdout<S>(self, msg: S) -> io::Result<()>
    where
        S: AsRef<str>,
    {
        self.print(stdout().lock(), msg)
    }

    /// Print the status to stderr.
    ///
    /// `msg`: The message to be printed alongside the status.
    ///
    /// ## Example
    /// ```
    /// use carlog::prelude::*;
    ///
    /// let status = Status::new().bold().justify().color(CargoColor::Green).status("Compiled");
    /// status.print_stderr("carlog v0.1.0");
    /// ```
    pub fn print_stderr<S>(self, msg: S) -> io::Result<()>
    where
        S: AsRef<str>,
    {
        self.print(stderr().lock(), msg)
    }

    /// Print the status to the specified stream.
    ///
    /// `stream`: The stream where the status and message will be written.
    /// `msg`: The message to be printed alongside the status.
    ///
    /// ## Example
    /// ```
    /// use carlog::prelude::*;
    ///
    /// let status = Status::new().bold().justify().color(CargoColor::Green).status("Compiled");
    /// let mut output = Vec::<u8>::new();
    /// status.print(output, "carlog v0.1.0");
    /// ```
    pub fn print<W, S>(self, mut stream: W, msg: S) -> io::Result<()>
    where
        W: Write,
        S: AsRef<str>,
    {
        let status = Self::color_str(self.color, self.bold, &self.status);
        if self.justify {
            let padding = " ".repeat(usize::saturating_sub(12, self.status.len()));
            write!(stream, "{}{}", padding, status)?;
        } else {
            write!(stream, "{}", status)?;
        }
        writeln!(stream, "{}", msg.as_ref())?;
        stream.flush()?;
        Ok(())
    }

    fn color_str<S>(color: CargoColor, bold: bool, str: S) -> String
    where
        S: AsRef<str>,
    {
        let mut colored = match color {
            CargoColor::Green => str.as_ref().green(),
            CargoColor::Cyan => str.as_ref().cyan(),
            CargoColor::Yellow => str.as_ref().bright_yellow(),
            CargoColor::Red => str.as_ref().bright_red(),
            CargoColor::White => str.as_ref().white(),
            CargoColor::Black => str.as_ref().black(),
        };
        if bold {
            colored = colored.bold()
        }
        colored.to_string()
    }
}

/// Print a cargo like message.
///
/// ## Example
/// ```ignore
/// #[macro_use] extern crate carlog;
///
/// use carlog::prelude::*;
///
/// carlog!("Compiling", "carlog v0.1.0"); // Not justified, not bold, stdout, white.
/// carlog!("Compiling", "carlog v0.1.0", CargoColor::Cyan); // Not justified, not bold, stdout.
///
/// let mut output = Vec::<u8>::new();
/// carlog!(
///     "Compiling",                      // Status text
///     "carlog v0.1.0",                  // Message
///     true,                             // Bold
///     false,                            // Justified
///     CargoColor::Cyan,                 // Color
///     CarlogStream::Custom(&mut output) // Stream
/// );
/// println!("{}", String::from_utf8(output).unwrap());
/// ```
#[macro_export]
macro_rules! carlog {
    ($status:expr, $message:expr) => {
        carlog!($status, $message, crate::CargoColor::default());
    };
    ($status:expr, $message:expr, $color:expr) => {
        carlog!(
            $status,
            $message,
            false,
            false,
            $color,
            crate::CarlogStream::default()
        )
    };
    ($status:expr, $message:expr, $bold:expr, $justify:expr, $color:expr, $stream:expr) => {
        let mut status = crate::Status::new().color($color).status($status);
        if $bold {
            status = status.bold();
        }
        if $justify {
            status = status.justify();
        }
        match $stream {
            crate::CarlogStream::Stdout => status
                .print_stdout($message)
                .expect("Failed to print to stdout!"),
            crate::CarlogStream::Stderr => status
                .print_stderr($message)
                .expect("Failed to print to stderr!"),
            crate::CarlogStream::Custom(stream) => status
                .print(stream, $message)
                .expect("Failed to print to custom stream!"),
        }
    };
}

/// Print an info-like cargo message.
///
/// The status is justified, bold and in cyan.
///
/// ## Example
/// ```ignore
/// #[macro_use] extern crate carlog;
///
/// use carlog::prelude::*;
///
/// carlog_info!("Compiling", "carlog v0.1.0");
/// let mut output = Vec::<u8>::new();
/// carlog_info!("Compiling", "carlog v0.1.0", CarlogStream::Custom(&mut output));
/// println!("{}", String::from_utf8(output).unwrap());
/// ```
#[macro_export]
macro_rules! carlog_info {
    ($status:expr, $message:expr) => {
        carlog_info!($status, $message, crate::CarlogStream::default());
    };
    ($status:expr, $message:expr, $stream:expr) => {
        carlog!(
            $status,
            format!(" {}", $message),
            true,
            true,
            crate::CargoColor::Cyan,
            $stream
        );
    };
}

/// Print an ok-like cargo message.
///
/// The status is justified, bold and in green.
///
/// ## Example
/// ```ignore
/// #[macro_use] extern crate carlog;
///
/// use carlog::prelude::*;
///
/// carlog_ok!("Compiled", "carlog v0.1.0");
/// let mut output = Vec::<u8>::new();
/// carlog_ok!("Compiled", "carlog v0.1.0", CarlogStream::Custom(&mut output));
/// println!("{}", String::from_utf8(output).unwrap());
/// ```
#[macro_export]
macro_rules! carlog_ok {
    ($status:expr, $message:expr) => {
        carlog_ok!($status, $message, crate::CarlogStream::default());
    };
    ($status:expr, $message:expr, $stream:expr) => {
        carlog!(
            $status,
            format!(" {}", $message),
            true,
            true,
            crate::CargoColor::Green,
            $stream
        );
    };
}

/// Print an warning like cargo message.
///
/// The status is not justified, not bold and light yellow with the status text 'warning'.
///
/// ## Example
/// ```ignore
/// #[macro_use] extern crate carlog;
///
/// use carlog::prelude::*;
///
/// carlog_warning!("carlog (v0.1.0) generated a warning!");
/// let mut output = Vec::<u8>::new();
/// carlog_warning!("carlog (v0.1.0) generated a warning!", CarlogStream::Custom(&mut output));
/// println!("{}", String::from_utf8(output).unwrap());
/// ```
#[macro_export]
macro_rules! carlog_warning {
    ($message:expr) => {
        carlog_warning!($message, crate::CarlogStream::default());
    };
    ($message:expr, $stream:expr) => {
        carlog!(
            "warning",
            format!(": {}", $message),
            false,
            false,
            crate::CargoColor::Yellow,
            $stream
        );
    };
}

/// Print an error like cargo message.
///
/// The status is not justified, not bold and light red with the status text 'error'.
///
/// ## Example
/// ```ignore
/// #[macro_use] extern crate carlog;
///
/// use carlog::prelude::*;
///
/// carlog_error!("carlog (v0.1.0) generated an error!");
/// let mut output = Vec::<u8>::new();
/// carlog_error!("carlog (v0.1.0) generated an error!", CarlogStream::Custom(&mut output));
/// println!("{}", String::from_utf8(output).unwrap());
/// ```
#[macro_export]
macro_rules! carlog_error {
    ($message:expr) => {
        carlog_error!($message, crate::CarlogStream::default());
    };
    ($message:expr, $stream:expr) => {
        carlog!(
            "error",
            format!(": {}", $message),
            false,
            false,
            crate::CargoColor::Red,
            $stream
        );
    };
}

#[cfg(test)]
mod test {
    use crate::CarlogStream;

    #[test]
    fn test_carlog_info() {
        let mut output = Vec::<u8>::new();
        carlog_info!(
            "Compiling",
            "carlog v0.1.0",
            CarlogStream::Custom(&mut output)
        );
        let output = String::from_utf8(output);
        assert!(output.is_ok());
        assert_eq!(
            output.unwrap(),
            "   \u{1b}[1;36mCompiling\u{1b}[0m carlog v0.1.0\n"
        );
    }

    #[test]
    fn test_carlog_ok() {
        let mut output = Vec::<u8>::new();
        carlog_ok!(
            "Compiled",
            "carlog v0.1.0",
            CarlogStream::Custom(&mut output)
        );
        let output = String::from_utf8(output);
        assert!(output.is_ok());
        assert_eq!(
            output.unwrap(),
            "    \u{1b}[1;32mCompiled\u{1b}[0m carlog v0.1.0\n"
        );
    }

    #[test]
    fn test_carlog_warning() {
        let mut output = Vec::<u8>::new();
        carlog_warning!(
            "carlog (v0.1.0) generated a warning!",
            CarlogStream::Custom(&mut output)
        );
        let output = String::from_utf8(output);
        assert!(output.is_ok());
        assert_eq!(
            output.unwrap(),
            "\u{1b}[93mwarning\u{1b}[0m: carlog (v0.1.0) generated a warning!\n"
        );
    }

    #[test]
    fn test_carlog_error() {
        let mut output = Vec::<u8>::new();
        carlog_error!(
            "carlog (v0.1.0) generated an error!",
            CarlogStream::Custom(&mut output)
        );
        let output = String::from_utf8(output);
        assert!(output.is_ok());
        assert_eq!(
            output.unwrap(),
            "\u{1b}[91merror\u{1b}[0m: carlog (v0.1.0) generated an error!\n"
        );
    }
}