papergrid/records/vec_records/
cell.rs

1/// Cell implementation which can be used with [`VecRecords`].
2///
3/// [`VecRecords`]: crate::records::vec_records::VecRecords
4pub trait Cell {
5    /// Gets a text.
6    fn text(&self) -> &str;
7
8    /// Gets a line by index.
9    fn line(&self, line: usize) -> &str;
10
11    /// Returns a number of lines cell has.
12    fn count_lines(&self) -> usize;
13
14    /// Returns a width of cell.
15    fn width(&self) -> usize;
16
17    /// Returns a width of cell line.
18    fn line_width(&self, line: usize) -> usize;
19}
20
21impl<T> Cell for &T
22where
23    T: Cell,
24{
25    fn text(&self) -> &str {
26        T::text(self)
27    }
28
29    fn line(&self, line: usize) -> &str {
30        T::line(self, line)
31    }
32
33    fn count_lines(&self) -> usize {
34        T::count_lines(self)
35    }
36
37    fn width(&self) -> usize {
38        T::width(self)
39    }
40
41    fn line_width(&self, line: usize) -> usize {
42        T::line_width(self, line)
43    }
44}
45
46impl Cell for str {
47    fn text(&self) -> &str {
48        self
49    }
50
51    fn line(&self, line: usize) -> &str {
52        self.lines().nth(line).expect("must be found")
53    }
54
55    fn count_lines(&self) -> usize {
56        self.lines().count()
57    }
58
59    fn width(&self) -> usize {
60        crate::util::string::get_text_width(self)
61    }
62
63    fn line_width(&self, line: usize) -> usize {
64        let line = self.line(line);
65        crate::util::string::get_line_width(line)
66    }
67}
68
69impl Cell for &str {
70    fn text(&self) -> &str {
71        self
72    }
73
74    fn line(&self, line: usize) -> &str {
75        self.lines().nth(line).expect("must be found")
76    }
77
78    fn count_lines(&self) -> usize {
79        self.lines().count()
80    }
81
82    fn width(&self) -> usize {
83        crate::util::string::get_text_width(self)
84    }
85
86    fn line_width(&self, line: usize) -> usize {
87        let line = self.line(line);
88        crate::util::string::get_line_width(line)
89    }
90}
91
92impl Cell for String {
93    fn text(&self) -> &str {
94        self
95    }
96
97    fn line(&self, line: usize) -> &str {
98        self.lines().nth(line).expect("must be found")
99    }
100
101    fn count_lines(&self) -> usize {
102        self.lines().count()
103    }
104
105    fn width(&self) -> usize {
106        crate::util::string::get_text_width(self)
107    }
108
109    fn line_width(&self, line: usize) -> usize {
110        let line = self.line(line);
111        crate::util::string::get_line_width(line)
112    }
113}