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
use crate::grid_builder::*;
use std::fmt::*;
use std::marker::PhantomData;
/// A builder used to create plain-text table from row values.
///
/// Generate a table using the columns defined by [`CellsFormatter`].
///
/// # Examples
///
/// ```
/// use text_grid::*;
/// struct RowData {
///     a: u32,
///     b: u32,
/// }
/// impl CellsSource for RowData {
///     fn fmt(f: &mut CellsFormatter<&Self>) {
///         f.column("a", |s| s.a);
///         f.column("b", |s| s.b);
///     }
/// }
///
/// let mut g = Grid::new();
/// g.push(&RowData { a: 300, b: 1 });
/// g.push(&RowData { a: 2, b: 200 });
///
/// assert_eq!(format!("\n{g}"), r#"
///   a  |  b  |
/// -----|-----|
///  300 |   1 |
///    2 | 200 |
/// "#);
/// ```
pub struct Grid<R: ?Sized, S = DefaultGridSchema<R>> {
    b: GridBuilder,
    schema: S,
    _phantom: PhantomData<fn(&R)>,
}

impl<R: CellsSource + ?Sized> Default for Grid<R, DefaultGridSchema<R>> {
    fn default() -> Self {
        Self::new()
    }
}

impl<R: CellsSource + ?Sized> Grid<R, DefaultGridSchema<R>> {
    /// Create a new `Grid` with [`DefaultGridSchema`] and prepare header rows.
    pub fn new() -> Self {
        Self::new_with_schema(DefaultGridSchema::default())
    }
}

impl<R: ?Sized, S: GridSchema<Source = R>> Grid<S::Source, S> {
    /// Create a new `Grid` with specified schema and prepare header rows.
    pub fn new_with_schema(schema: S) -> Self {
        let b = GridBuilder::new_with_header(&schema);
        Grid {
            b,
            schema,
            _phantom: PhantomData::default(),
        }
    }
}
impl<R: ?Sized, S: GridSchema<Source = R>> Grid<R, S> {
    /// Append a row to the bottom of the grid.
    pub fn push(&mut self, source: &R) {
        self.b.push(|b| b.extend_with_schema(source, &self.schema));
    }

    /// Append a row separator to the bottom of the grid.
    pub fn push_separator(&mut self) {
        self.b.push_separator();
    }
}
impl<R, S: GridSchema<Source = R>> Extend<R> for Grid<R, S> {
    fn extend<T: IntoIterator<Item = R>>(&mut self, iter: T) {
        for i in iter {
            self.push(&i);
        }
    }
}
impl<'a, R, S: GridSchema<Source = R>> Extend<&'a R> for Grid<R, S> {
    fn extend<T: IntoIterator<Item = &'a R>>(&mut self, iter: T) {
        for i in iter {
            self.push(i);
        }
    }
}
impl<R: CellsSource> FromIterator<R> for Grid<R> {
    fn from_iter<T: IntoIterator<Item = R>>(iter: T) -> Self {
        let mut g = Self::new();
        g.extend(iter);
        g
    }
}
impl<'a, R: CellsSource> FromIterator<&'a R> for Grid<R> {
    fn from_iter<T: IntoIterator<Item = &'a R>>(iter: T) -> Self {
        let mut g = Self::new();
        g.extend(iter);
        g
    }
}

impl<R: ?Sized, S> Display for Grid<R, S> {
    fn fmt(&self, f: &mut Formatter) -> Result {
        Display::fmt(&self.b, f)
    }
}
impl<R: ?Sized, S> Debug for Grid<R, S> {
    fn fmt(&self, f: &mut Formatter) -> Result {
        Debug::fmt(&self.b, f)
    }
}