Skip to main content

cba/baccarat/
mod.rs

1mod define;
2// pub use define::*;
3mod types;
4// pub use types::*;
5
6// -------------------------------
7
8#[macro_export]
9/// Map a function over the elements of a vec![].
10/// By default, .to_string() is applied.
11/// To specify the mapping, a function or type followed by `|` can precede the elements.
12/// The pure prefix `:` is shorthand for calling .into().
13macro_rules! vec_ {
14    ($($elem:expr),* $(,)?) => {
15        vec![$($elem.to_string()),*]
16    };
17    (: $($elem:expr),*) => {
18        vec![$($elem.into()),*]
19    };
20    ($t:ty: $($elem:expr),*) => {
21        vec![$(
22            < $t as ::std::convert::From<_> >::from($elem)
23        ),*]
24    };
25    ($f:ident : $($elem:expr),*) => {
26        vec![$($f($elem)),*]
27    };
28}
29
30#[macro_export]
31/// Map a function over the elements of a [].
32/// By default, .to_string() is applied.
33/// To specify the mapping, a function or type followed by `|` can precede the elements.
34/// The pure prefix `:` is shorthand for calling .into().
35macro_rules! slice_ {
36    ($($elem:expr),* $(,)?) => {
37        [$($elem.to_string()),*]
38    };
39    (: $($elem:expr),*) => {
40        [$($elem.into()),*]
41    };
42    ($t:ty : $($elem:expr),*) => {
43        [$(
44            < $t as ::std::convert::From<_> >::from($elem)
45        ),*]
46    };
47    ($f:ident : $($elem:expr),*) => {
48        [$($f($elem)),*]
49    };
50}
51
52#[macro_export]
53/// Write newline-delimited strings directly to stdout without dynamic dispatch.
54///
55/// # Note
56/// The `;` delimiter inserts new-lines, while `,` does not.
57/// A trailing newline is always added.
58macro_rules! prints {
59    // write to custom buffer
60    ($( $( $s:expr ),+ );* => $buf:expr) => {{
61        use std::io::Write;
62        $(
63            $(
64                let _ = $buf.write_all($s.as_bytes());
65            )+
66            let _ = $buf.write_all(b"\n");
67        )*
68    }};
69
70    // default: stdout
71    ( $( $( $s:expr ),+ );* ) => {{
72        use std::io::{self, Write};
73        let mut out = io::stdout().lock();
74
75        $(
76            $(
77                let _ = out.write_all($s.as_bytes());
78            )+
79            let _ = out.write_all(b"\n");
80        )*
81    }};
82}
83
84#[macro_export]
85// Easier than format! for concatenating strings
86macro_rules! concat_ {
87    ( $( $x:expr ),* $(,)? ) => {{
88        use std::fmt::Write;
89        let mut s = String::new();
90        $(
91            write!(&mut s, "{}", $x).unwrap();
92        )*
93        s
94    }};
95}
96
97#[cfg(test)]
98mod tests {
99    #[test]
100    fn writes_to_buffer() {
101        let mut buf = Vec::new();
102
103        prints!(
104            "hello", " world";
105            "line 2";
106            "a", "b", "c" => &mut buf
107        );
108
109        assert_eq!(
110            std::str::from_utf8(&buf).unwrap(),
111            "hello world\nline 2\nabc\n"
112        );
113
114        let mut buf = Vec::new();
115
116        prints!(
117            "hello", " world" => &mut buf
118        );
119
120        assert_eq!(std::str::from_utf8(&buf).unwrap(), "hello world\n");
121    }
122}
123
124// ------------- DEBUG -------------
125
126/// dbg!/log::debug! and return the value.
127/// - expr: dbg, but only in debug builds.
128/// - prefix, expr: log::trace!("{prefix}: {:?}")
129#[macro_export]
130macro_rules! _dbg {
131    ($($expr:expr),+ $(,)?) => {{
132        $(
133            #[cfg(debug_assertions)]
134            let __val = ::std::dbg!($expr);
135            #[cfg(not(debug_assertions))]
136            let __val = $expr;
137        )+
138        __val
139    }};
140
141    ($prefix:expr; $s:expr) => {{
142        let val = $s;
143        ::log::trace!(concat!("{}: {:?}"), $prefix, &val);
144        val
145    }};
146}
147
148/// Prints to stderr like `eprintln!` but only in debug builds
149#[macro_export]
150macro_rules! _eprint {
151    ($($args:tt)*) => {
152        #[cfg(debug_assertions)]
153        {
154            eprintln!($($args)*);
155        }
156    };
157}
158
159#[macro_export]
160/// Info log in debug
161macro_rules! _info {
162    ($($arg:tt)*) => {
163        #[cfg(debug_assertions)]
164        {
165            log::info!($($arg)*);
166        }
167    };
168}