easydoc_core/style/
table.rs1use super::color::Color;
2use super::font::FontConfig;
3
4#[derive(Debug, Clone)]
8pub struct TableStyle {
9 pub header_font: FontConfig,
11 pub content_font: FontConfig,
13 pub header_background: Option<Color>,
15 pub banded_rows: bool,
17 pub even_row_background: Option<Color>,
19 pub odd_row_background: Option<Color>,
21 pub auto_width: bool,
23 pub borders: bool,
25}
26
27impl Default for TableStyle {
28 fn default() -> Self {
29 Self {
30 header_font: FontConfig::header(),
31 content_font: FontConfig::default(),
32 header_background: Some(Color::HEADER_BLUE),
33 banded_rows: false,
34 even_row_background: Some(Color::rgb(242, 242, 242)),
35 odd_row_background: None, auto_width: false,
37 borders: true,
38 }
39 }
40}
41
42impl TableStyle {
43 #[must_use]
45 pub fn new() -> Self {
46 Self::default()
47 }
48
49 #[must_use]
51 pub fn header() -> Self {
52 Self::default()
53 }
54
55 #[must_use]
57 pub fn simple() -> Self {
58 Self {
59 borders: false,
60 header_background: None,
61 header_font: FontConfig::bold(),
62 ..Default::default()
63 }
64 }
65
66 #[must_use]
68 pub fn banded_rows(mut self, enabled: bool) -> Self {
69 self.banded_rows = enabled;
70 self
71 }
72
73 #[must_use]
75 pub fn auto_width(mut self, enabled: bool) -> Self {
76 self.auto_width = enabled;
77 self
78 }
79
80 #[must_use]
82 pub fn borders(mut self, enabled: bool) -> Self {
83 self.borders = enabled;
84 self
85 }
86
87 #[must_use]
89 pub fn header_background(mut self, color: Color) -> Self {
90 self.header_background = Some(color);
91 self
92 }
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn default_table_style() {
101 let s = TableStyle::default();
102 assert!(s.borders);
103 assert!(!s.banded_rows);
104 assert!(!s.auto_width);
105 assert_eq!(s.header_background, Some(Color::HEADER_BLUE));
106 }
107
108 #[test]
109 fn new_equals_default() {
110 let s = TableStyle::new();
111 assert!(s.borders);
112 }
113
114 #[test]
115 fn header_style() {
116 let s = TableStyle::header();
117 assert!(s.borders);
118 assert_eq!(s.header_background, Some(Color::HEADER_BLUE));
119 }
120
121 #[test]
122 fn simple_style() {
123 let s = TableStyle::simple();
124 assert!(!s.borders);
125 assert!(s.header_background.is_none());
126 assert!(s.header_font.bold);
127 }
128
129 #[test]
130 fn builder_chain() {
131 let s = TableStyle::new()
132 .banded_rows(true)
133 .auto_width(true)
134 .borders(false)
135 .header_background(Color::RED);
136 assert!(s.banded_rows);
137 assert!(s.auto_width);
138 assert!(!s.borders);
139 assert_eq!(s.header_background, Some(Color::RED));
140 }
141}