derive/derive.rs
1//! This example shows how you can quickly set up a table printer
2//! for your datatype.
3
4use kitty_table::{DebugTableRow, DefaultTableStyle, TableFormatter};
5
6#[derive(DefaultTableStyle, DebugTableRow)]
7pub struct MyDictionaryStruct<'src> {
8 name: &'src str,
9 age: u32,
10 adult: bool,
11}
12
13#[derive(DefaultTableStyle, DebugTableRow)]
14pub struct MyTupleStruct(i32, i32, i32);
15
16pub fn main() {
17 dictionary_struct();
18 tuple_struct();
19}
20
21fn dictionary_struct() {
22 // Create a table using the default formatter for MyDictionaryStruct.
23 // This default formatter was created by deriving DefaultTableStyle above.
24 let table = TableFormatter::from_style();
25
26 // Don't format this into a million lines please it's fine like this
27 #[rustfmt::skip]
28 let data = [
29 MyDictionaryStruct { name: "Susan", age: 42, adult: true },
30 MyDictionaryStruct { name: "Ashli", age: 19, adult: true },
31 MyDictionaryStruct { name: "Groot", age: 5, adult: false },
32 MyDictionaryStruct { name: "Henry", age: 53, adult: true },
33 MyDictionaryStruct { name: "Whatever", age: 17, adult: false },
34 ];
35
36 // Print our results. We are able to use MyDictionaryStruct here
37 // because we derived DebugTableRow earlier.
38 let _ = table.debug_print(data);
39}
40
41fn tuple_struct() {
42 // Create a table using the default formatter for MyTupleStruct. This
43 // default formatter was created by deriving DefaultTableStyle above.
44 let table = TableFormatter::from_style();
45
46 let data = [
47 MyTupleStruct(7, -10, -3),
48 MyTupleStruct(6, -7, -1),
49 MyTupleStruct(5, -4, 1),
50 MyTupleStruct(4, -1, 3),
51 MyTupleStruct(3, 2, 5),
52 MyTupleStruct(2, 5, 7),
53 MyTupleStruct(1, 8, 9),
54 MyTupleStruct(0, 11, 11),
55 MyTupleStruct(-1, 14, 13),
56 MyTupleStruct(-2, 17, 15),
57 ];
58
59 // Print our results. We are able to use MyTupleStruct here
60 // because we derived DebugTableRow earlier.
61 let _ = table.debug_print(data);
62}
63
64/*
65Expected result:
66
67┏━━━━━━━━━━━━┳━━━━━┳━━━━━━━┓
68┃ name ┃ age ┃ adult ┃
69┣━━━━━━━━━━━━╇━━━━━╇━━━━━━━┫
70┃ "Susan" │ 42 │ true ┃
71┠────────────┼─────┼───────┨
72┃ "Ashli" │ 19 │ true ┃
73┠────────────┼─────┼───────┨
74┃ "Groot" │ 5 │ false ┃
75┠────────────┼─────┼───────┨
76┃ "Henry" │ 53 │ true ┃
77┠────────────┼─────┼───────┨
78┃ "Whatever" │ 17 │ false ┃
79┗━━━━━━━━━━━━┷━━━━━┷━━━━━━━┛
80┏━━━━┯━━━━━┯━━━━┓
81┃ 7 │ -10 │ -3 ┃
82┠────┼─────┼────┨
83┃ 6 │ -7 │ -1 ┃
84┠────┼─────┼────┨
85┃ 5 │ -4 │ 1 ┃
86┠────┼─────┼────┨
87┃ 4 │ -1 │ 3 ┃
88┠────┼─────┼────┨
89┃ 3 │ 2 │ 5 ┃
90┠────┼─────┼────┨
91┃ 2 │ 5 │ 7 ┃
92┠────┼─────┼────┨
93┃ 1 │ 8 │ 9 ┃
94┠────┼─────┼────┨
95┃ 0 │ 11 │ 11 ┃
96┠────┼─────┼────┨
97┃ -1 │ 14 │ 13 ┃
98┠────┼─────┼────┨
99┃ -2 │ 17 │ 15 ┃
100┗━━━━┷━━━━━┷━━━━┛
101*/