Skip to main content

ice_age/
__.rs

1// License: see LICENSE file at root directory of `master` branch
2
3//! # Usage
4//!
5//! This module requires you to define a constant named `::TAG` at your root crate.
6//!
7//! It's recommended to include crate code name and version name in `::TAG`.
8
9/// # Unique universally identifier of this kit
10#[allow(dead_code)]
11pub const UUID: &str = "3ec2df31-b5fb-49a9-b91f-de63ec69a058";
12
13/// # Version
14#[allow(dead_code)]
15pub const VERSION: &str = "0.4.1";
16
17/// # Release date (year/month/day)
18#[allow(dead_code)]
19pub const RELEASE_DATE: (u16, u8, u8) = (2019, 6, 2);
20
21/// # Wrapper for format!(), which prefixes your message with: ::TAG, module_path!(), line!()
22macro_rules! __ { ($($arg: tt)+) => {
23    format!("[{}][{}-{}] {}", crate::TAG, module_path!(), line!(), format!($($arg)+))
24};}
25
26/// # Wrapper for format!(), which wraps your message inside 'bold' tag
27macro_rules! __b { ($($arg: tt)+) => {
28    format!("\x1b[1m{}\x1b[m", format!($($arg)+))
29};}
30
31/// # Wrapper for format!(), which wraps your message inside a warning 'red color' tag
32macro_rules! __w { ($($arg: tt)+) => {
33    format!("\x1b[38;5;1m{}\x1b[39m", format!($($arg)+))
34};}
35
36/// # Wrapper for println!() + __!()
37macro_rules! __p { ($($arg: tt)+) => {
38    println!("{}", __!($($arg)+));
39};}
40
41/// # Checks to see if given file descriptor is a char-device
42#[cfg(unix)]
43macro_rules! __is_char_device { ($fd: expr) => {{
44    use ::std::os::unix::fs::FileTypeExt;
45    use ::std::os::unix::io::RawFd;
46    use ::std::path::PathBuf;
47
48    let fd: RawFd = $fd;
49    match PathBuf::from(format!("/proc/{}/fd/{}", std::process::id(), fd)).metadata().map(|m| m.file_type()) {
50        Ok(ft) => ft.is_char_device(),
51        Err(_) => false,
52    }
53}};}
54
55/// # Wrapper for __p!() + __b!() + __!()
56macro_rules! __pp { ($($arg: tt)+) => {
57    match match cfg!(unix) {
58        true => {
59            use ::std::os::unix::io::AsRawFd;
60            __is_char_device!(::std::io::stdout().as_raw_fd())
61        },
62        false => true,
63    } {
64        true => __p!("{}", __b!("{}", __!($($arg)+))),
65        false => __p!($($arg)+),
66    };
67};}
68
69/// # Wrapper for eprintln!() + __!()
70macro_rules! __e { ($($arg: tt)+) => {
71    eprintln!("{}", __!($($arg)+));
72};}
73
74/// # Wrapper for __e!() + __w!() + __!()
75macro_rules! __ee { ($($arg: tt)+) => {
76    match match cfg!(unix) {
77        true => {
78            use ::std::os::unix::io::AsRawFd;
79            __is_char_device!(::std::io::stderr().as_raw_fd())
80        },
81        false => true,
82    } {
83        true => __e!("{}", __w!("{}", __!($($arg)+))),
84        false => __e!($($arg)+),
85    };
86};}