Skip to main content

easydoc_core/style/
table.rs

1use super::color::Color;
2use super::font::FontConfig;
3
4/// 表格级格式化配置。
5///
6/// 对应 Java: `com.alibaba.excel.write.metadata.style.WriteCellStyle` 的表格级属性
7#[derive(Debug, Clone)]
8pub struct TableStyle {
9    /// Style for the header row.
10    pub header_font: FontConfig,
11    /// Style for content rows.
12    pub content_font: FontConfig,
13    /// Background color for header cells.
14    pub header_background: Option<Color>,
15    /// Whether to apply alternating row colors (banded/zebra).
16    pub banded_rows: bool,
17    /// Background color for even rows (when banded).
18    pub even_row_background: Option<Color>,
19    /// Background color for odd rows (when banded).
20    pub odd_row_background: Option<Color>,
21    /// Whether to auto-fit column widths to content.
22    pub auto_width: bool,
23    /// Table border visibility.
24    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, // white default
36            auto_width: false,
37            borders: true,
38        }
39    }
40}
41
42impl TableStyle {
43    /// Creates a default table style.
44    #[must_use]
45    pub fn new() -> Self {
46        Self::default()
47    }
48
49    /// Creates a header-only style (bold white text on accent blue).
50    #[must_use]
51    pub fn header() -> Self {
52        Self::default()
53    }
54
55    /// Creates a simple style without borders or banding.
56    #[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    /// Enables zebra striping.
67    #[must_use]
68    pub fn banded_rows(mut self, enabled: bool) -> Self {
69        self.banded_rows = enabled;
70        self
71    }
72
73    /// Enables auto column width.
74    #[must_use]
75    pub fn auto_width(mut self, enabled: bool) -> Self {
76        self.auto_width = enabled;
77        self
78    }
79
80    /// Enables table borders.
81    #[must_use]
82    pub fn borders(mut self, enabled: bool) -> Self {
83        self.borders = enabled;
84        self
85    }
86
87    /// Sets header background color.
88    #[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}