Coverage Report

Created: 2026-04-29 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/tabled-0.20.0/src/tabled.rs
Line
Count
Source
1
use std::borrow::Cow;
2
3
/// Tabled a trait responsible for providing a header fields and a row fields.
4
///
5
/// It's urgent that `header` len is equal to `fields` len.
6
///
7
/// ```text
8
/// Self::headers().len() == self.fields().len()
9
/// ```
10
pub trait Tabled {
11
    /// A length of fields and headers,
12
    /// which must be the same.
13
    const LENGTH: usize;
14
15
    /// Fields method must return a list of cells.
16
    ///
17
    /// The cells will be placed in the same row, preserving the order.
18
    fn fields(&self) -> Vec<Cow<'_, str>>;
19
20
    /// Headers must return a list of column names.
21
    fn headers() -> Vec<Cow<'static, str>>;
22
}
23
24
impl<T> Tabled for &T
25
where
26
    T: Tabled,
27
{
28
    const LENGTH: usize = T::LENGTH;
29
30
0
    fn fields(&self) -> Vec<Cow<'_, str>> {
31
0
        T::fields(self)
32
0
    }
33
34
0
    fn headers() -> Vec<Cow<'static, str>> {
35
0
        T::headers()
36
0
    }
37
}
38
39
impl<T> Tabled for Box<T>
40
where
41
    T: Tabled,
42
{
43
    const LENGTH: usize = T::LENGTH;
44
45
0
    fn fields(&self) -> Vec<Cow<'_, str>> {
46
0
        T::fields(self)
47
0
    }
48
49
0
    fn headers() -> Vec<Cow<'static, str>> {
50
0
        T::headers()
51
0
    }
52
}
53
54
impl<T> Tabled for Option<T>
55
where
56
    T: Tabled,
57
{
58
    const LENGTH: usize = T::LENGTH;
59
60
0
    fn fields(&self) -> Vec<Cow<'_, str>> {
61
0
        match self {
62
0
            Some(value) => Tabled::fields(value),
63
0
            None => vec![Cow::Borrowed(""); Self::LENGTH],
64
        }
65
0
    }
66
67
0
    fn headers() -> Vec<Cow<'static, str>> {
68
0
        T::headers()
69
0
    }
70
}
71
72
macro_rules! tuple_table {
73
    ( $($name:ident)+ ) => {
74
        impl<$($name: Tabled),+> Tabled for ($($name,)+){
75
            const LENGTH: usize = $($name::LENGTH+)+ 0;
76
77
0
            fn fields(&self) -> Vec<Cow<'_, str>> {
78
                #![allow(non_snake_case)]
79
0
                let ($($name,)+) = self;
80
0
                let mut fields = Vec::with_capacity(Self::LENGTH);
81
0
                $(fields.append(&mut $name.fields());)+
82
0
                fields
83
0
            }
Unexecuted instantiation: <(_,) as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <(_, _) as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <(_, _, _) as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <(_, _, _, _) as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <(_, _, _, _, _) as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <(_, _, _, _, _, _) as tabled::tabled::Tabled>::fields
84
85
0
            fn headers() -> Vec<Cow<'static, str>> {
86
0
                let mut fields = Vec::with_capacity(Self::LENGTH);
87
0
                $(fields.append(&mut $name::headers());)+
88
0
                fields
89
0
            }
Unexecuted instantiation: <(_,) as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <(_, _) as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <(_, _, _) as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <(_, _, _, _) as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <(_, _, _, _, _) as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <(_, _, _, _, _, _) as tabled::tabled::Tabled>::headers
90
        }
91
    };
92
}
93
94
tuple_table! { A }
95
tuple_table! { A B }
96
tuple_table! { A B C }
97
tuple_table! { A B C D }
98
tuple_table! { A B C D E }
99
tuple_table! { A B C D E F }
100
101
macro_rules! default_table {
102
    ( $t:ty ) => {
103
        impl Tabled for $t {
104
            const LENGTH: usize = 1;
105
106
0
            fn fields(&self) -> Vec<Cow<'_, str>> {
107
0
                vec![Cow::Owned(self.to_string())]
108
0
            }
Unexecuted instantiation: <char as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <bool as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <isize as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <usize as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <u8 as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <u16 as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <u32 as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <u64 as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <u128 as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <i8 as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <i16 as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <i32 as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <i64 as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <i128 as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <f32 as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <f64 as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <alloc::string::String as tabled::tabled::Tabled>::fields
109
0
            fn headers() -> Vec<Cow<'static, str>> {
110
0
                vec![Cow::Borrowed(stringify!($t))]
111
0
            }
Unexecuted instantiation: <char as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <bool as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <isize as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <usize as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <u8 as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <u16 as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <u32 as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <u64 as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <u128 as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <i8 as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <i16 as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <i32 as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <i64 as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <i128 as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <f32 as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <f64 as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <alloc::string::String as tabled::tabled::Tabled>::headers
112
        }
113
    };
114
115
    ( $t:ty = borrowed ) => {
116
        impl Tabled for $t {
117
            const LENGTH: usize = 1;
118
119
0
            fn fields(&self) -> Vec<Cow<'_, str>> {
120
0
                vec![Cow::Borrowed(self)]
121
0
            }
Unexecuted instantiation: <&str as tabled::tabled::Tabled>::fields
Unexecuted instantiation: <str as tabled::tabled::Tabled>::fields
122
0
            fn headers() -> Vec<Cow<'static, str>> {
123
0
                vec![Cow::Borrowed(stringify!($t))]
124
0
            }
Unexecuted instantiation: <&str as tabled::tabled::Tabled>::headers
Unexecuted instantiation: <str as tabled::tabled::Tabled>::headers
125
        }
126
    };
127
}
128
129
default_table!(&str = borrowed);
130
default_table!(str = borrowed);
131
default_table!(String);
132
133
default_table!(char);
134
135
default_table!(bool);
136
137
default_table!(isize);
138
default_table!(usize);
139
140
default_table!(u8);
141
default_table!(u16);
142
default_table!(u32);
143
default_table!(u64);
144
default_table!(u128);
145
146
default_table!(i8);
147
default_table!(i16);
148
default_table!(i32);
149
default_table!(i64);
150
default_table!(i128);
151
152
default_table!(f32);
153
default_table!(f64);
154
155
impl<T, const N: usize> Tabled for [T; N]
156
where
157
    T: std::fmt::Display,
158
{
159
    const LENGTH: usize = N;
160
161
0
    fn fields(&self) -> Vec<Cow<'_, str>> {
162
0
        self.iter()
163
0
            .map(ToString::to_string)
164
0
            .map(Cow::Owned)
165
0
            .collect()
166
0
    }
167
168
0
    fn headers() -> Vec<Cow<'static, str>> {
169
0
        (0..N).map(|i| Cow::Owned(format!("{i}"))).collect()
170
0
    }
171
}