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
// Copyright (c) tabular-rs Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

/// A macro for building a [`Row`].
///
/// `row!(A, B, C)` is equivalent to
/// `Row::new().with_cell(A).with_cell(B).with_cell(C)`.
///
/// # Examples
///
/// ```
/// use tabular::row;
///
/// # fn main() {
/// let table = tabular::Table::new("{:>}  {:<}  {:<}")
///     .with_row(row!(34, "hello", true))
///     .with_row(row!(567, "goodbye", false));
///
/// assert_eq!( format!("\n{}", table),
///             r#"
///  34  hello    true
/// 567  goodbye  false
/// "# );
/// # }
/// ```
///
/// [`Row`]: struct.Row.html
#[macro_export]
macro_rules! row {
    ( $( $cell:expr ),* ) => {
        {
            let mut result = $crate::Row::new();
            $(
                result.add_cell($cell);
            )*
            result
        }
    };

    ( $( $cell:expr, )* ) => {
        row!( $( $cell ),* )
    };
}

/// A macro for building a [`Table`].
///
/// `table!(S, A, B, C)` is equivalent to
/// `Table::new(S).with_row(A).with_row(B).with_row(B)`.
///
/// # Examples
///
/// ```
/// use tabular::{row, table};
///
/// # fn main() {
/// let table = table!("{:>}  {:<}  {:<}",
///                    row!(34, "hello", true),
///                    row!(567, "goodbye", false));
///
/// assert_eq!( format!("\n{}", table),
///             r#"
///  34  hello    true
/// 567  goodbye  false
/// "# );
/// # }
/// ```
///
/// [`Table`]: struct.Row.html
#[macro_export]
macro_rules! table {
    ( $row_spec:expr, $( $row:expr ),* ) => {
        {
            let mut result = $crate::Table::new($row_spec);
            $(
                result.add_row($row);
            )*
            result
        }
    };

    ( $row_spec:expr, $( $row:expr, )* ) => {
        table!($row_spec, $( row ),* )
    };
}