1use std::fmt;
19
20pub const MAX_TABLE_CELLS_VAR: &str = "DOCLING_RS_MAX_TABLE_CELLS";
23pub const MAX_TOTAL_TABLE_CELLS_VAR: &str = "DOCLING_RS_MAX_TOTAL_TABLE_CELLS";
27
28pub fn max_table_cells() -> usize {
30 crate::env::parse::<usize>(MAX_TABLE_CELLS_VAR).unwrap_or(1_000_000)
31}
32
33pub fn max_total_table_cells() -> usize {
35 crate::env::parse::<usize>(MAX_TOTAL_TABLE_CELLS_VAR).unwrap_or(5_000_000)
36}
37
38#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct LimitExceeded {
42 pub variable: &'static str,
43 pub limit: usize,
44 pub actual: usize,
45 pub what: String,
46}
47
48impl fmt::Display for LimitExceeded {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 write!(
51 f,
52 "{}: {} {} exceeds the limit of {} ({})",
53 self.what,
54 self.actual,
55 self.unit(),
56 self.limit,
57 self.variable
58 )
59 }
60}
61
62impl LimitExceeded {
63 pub fn unit(&self) -> &'static str {
67 if self.variable.ends_with("_MB") {
68 "bytes"
69 } else if self.variable.ends_with("_MEMBERS") {
70 "members"
71 } else {
72 "cells"
73 }
74 }
75}
76
77impl std::error::Error for LimitExceeded {}
78
79#[derive(Debug, Clone)]
84pub struct TableCellBudget {
85 per_table: usize,
86 total_cap: usize,
87 total: usize,
88}
89
90impl Default for TableCellBudget {
91 fn default() -> Self {
92 Self::new()
93 }
94}
95
96impl TableCellBudget {
97 pub fn new() -> Self {
98 Self {
99 per_table: max_table_cells(),
100 total_cap: max_total_table_cells(),
101 total: 0,
102 }
103 }
104
105 pub fn table(&mut self, what: &str, rows: usize, cols: usize) -> Result<(), LimitExceeded> {
108 let area = rows.saturating_mul(cols);
109 if rows > self.per_table || cols > self.per_table || area > self.per_table {
110 return Err(LimitExceeded {
111 variable: MAX_TABLE_CELLS_VAR,
112 limit: self.per_table,
113 actual: area.max(rows).max(cols),
114 what: format!("{what} of {rows}x{cols}"),
115 });
116 }
117 self.total = self.total.saturating_add(area);
118 if self.total > self.total_cap {
119 return Err(LimitExceeded {
120 variable: MAX_TOTAL_TABLE_CELLS_VAR,
121 limit: self.total_cap,
122 actual: self.total,
123 what: format!("{what}: the document's tables together"),
124 });
125 }
126 Ok(())
127 }
128}
129
130#[cfg(test)]
131mod tests {
132 use super::*;
133
134 #[test]
135 fn per_table_and_total_caps() {
136 let mut b = TableCellBudget {
137 per_table: 100,
138 total_cap: 250,
139 total: 0,
140 };
141 assert!(b.table("t", 10, 10).is_ok());
142 let e = b.table("t", 10, 11).unwrap_err();
143 assert_eq!(e.variable, MAX_TABLE_CELLS_VAR);
144 assert_eq!(e.actual, 110);
145 assert_eq!(b.table("t", 101, 0).unwrap_err().actual, 101);
147 assert!(b.table("t", 0, 101).is_err());
148 assert!(b.table("t", 10, 10).is_ok());
149 let e = b.table("t", 10, 10).unwrap_err();
150 assert_eq!(e.variable, MAX_TOTAL_TABLE_CELLS_VAR);
151 assert_eq!(e.actual, 300);
152 let mut b = TableCellBudget::new();
154 assert!(b.table("t", usize::MAX, usize::MAX).is_err());
155 assert!(b.table("t", usize::MAX, 0).is_err());
156 }
157}