Skip to main content

sheetkit_core/utils/
constants.rs

1//! Excel limit constants and default values.
2//!
3//! These constants mirror the hard limits enforced by Microsoft Excel 2007+
4//! (OOXML / `.xlsx` format).
5
6/// Maximum number of columns (XFD = 16 384 = 2^14).
7pub const MAX_COLUMNS: u32 = 16_384;
8
9/// Maximum number of rows (1 048 576 = 2^20).
10pub const MAX_ROWS: u32 = 1_048_576;
11
12/// Maximum number of cell styles that can be stored in a workbook.
13pub const MAX_CELL_STYLES: usize = 65_430;
14
15/// Maximum column width in character-width units.
16pub const MAX_COLUMN_WIDTH: f64 = 255.0;
17
18/// Maximum row height in points.
19pub const MAX_ROW_HEIGHT: f64 = 409.0;
20
21/// Maximum font size in points.
22pub const MAX_FONT_SIZE: f64 = 409.0;
23
24/// Maximum length (in characters) of a sheet name.
25pub const MAX_SHEET_NAME_LENGTH: usize = 31;
26
27/// Maximum number of characters that a single cell can contain.
28pub const MAX_CELL_CHARS: usize = 32_767;
29
30/// Characters that are not allowed in Excel sheet names.
31pub const SHEET_NAME_INVALID_CHARS: &[char] = &[':', '\\', '/', '?', '*', '[', ']'];
32
33/// Default column width used when no explicit width is set (character-width units).
34pub const DEFAULT_COL_WIDTH: f64 = 9.140625;
35
36/// Default row height in points.
37pub const DEFAULT_ROW_HEIGHT: f64 = 15.0;
38
39/// Chunk size used by the streaming writer (16 MiB).
40pub const STREAM_CHUNK_SIZE: usize = 16 * 1024 * 1024;
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn test_max_columns() {
48        assert_eq!(MAX_COLUMNS, 16_384);
49    }
50
51    #[test]
52    fn test_max_rows() {
53        assert_eq!(MAX_ROWS, 1_048_576);
54    }
55
56    #[test]
57    fn test_max_cell_styles() {
58        assert_eq!(MAX_CELL_STYLES, 65_430);
59    }
60
61    #[test]
62    fn test_max_column_width() {
63        assert!((MAX_COLUMN_WIDTH - 255.0).abs() < f64::EPSILON);
64    }
65
66    #[test]
67    fn test_max_row_height() {
68        assert!((MAX_ROW_HEIGHT - 409.0).abs() < f64::EPSILON);
69    }
70
71    #[test]
72    fn test_max_font_size() {
73        assert!((MAX_FONT_SIZE - 409.0).abs() < f64::EPSILON);
74    }
75
76    #[test]
77    fn test_max_sheet_name_length() {
78        assert_eq!(MAX_SHEET_NAME_LENGTH, 31);
79    }
80
81    #[test]
82    fn test_max_cell_chars() {
83        assert_eq!(MAX_CELL_CHARS, 32_767);
84    }
85
86    #[test]
87    fn test_default_col_width() {
88        assert!((DEFAULT_COL_WIDTH - 9.140625).abs() < f64::EPSILON);
89    }
90
91    #[test]
92    fn test_default_row_height() {
93        assert!((DEFAULT_ROW_HEIGHT - 15.0).abs() < f64::EPSILON);
94    }
95
96    #[test]
97    fn test_stream_chunk_size() {
98        assert_eq!(STREAM_CHUNK_SIZE, 16 * 1024 * 1024);
99    }
100
101    #[test]
102    fn test_sheet_name_invalid_chars() {
103        assert_eq!(SHEET_NAME_INVALID_CHARS.len(), 7);
104        assert!(SHEET_NAME_INVALID_CHARS.contains(&':'));
105        assert!(SHEET_NAME_INVALID_CHARS.contains(&'\\'));
106        assert!(SHEET_NAME_INVALID_CHARS.contains(&'/'));
107        assert!(SHEET_NAME_INVALID_CHARS.contains(&'?'));
108        assert!(SHEET_NAME_INVALID_CHARS.contains(&'*'));
109        assert!(SHEET_NAME_INVALID_CHARS.contains(&'['));
110        assert!(SHEET_NAME_INVALID_CHARS.contains(&']'));
111    }
112}