improve header/footer expansion error message

This commit is contained in:
PgBiel 2025-03-06 01:06:47 -03:00
parent ad049491b4
commit e8eeec0b3e
3 changed files with 39 additions and 36 deletions

View File

@ -908,11 +908,21 @@ struct CellGridResolver<'a, 'b, 'x> {
span: Span, span: Span,
} }
#[derive(Debug, Clone, Copy)]
enum RowGroupKind { enum RowGroupKind {
Header, Header,
Footer, Footer,
} }
impl RowGroupKind {
fn name(self) -> &'static str {
match self {
Self::Header => "header",
Self::Footer => "footer",
}
}
}
struct RowGroupData { struct RowGroupData {
/// The range of rows of cells inside this grid row group. The /// The range of rows of cells inside this grid row group. The
/// first and last rows are guaranteed to have cells (an exception /// first and last rows are guaranteed to have cells (an exception
@ -1456,11 +1466,14 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
// Cell's header or footer must expand to include the cell's // Cell's header or footer must expand to include the cell's
// occupied positions, if possible. // occupied positions, if possible.
if let Some(RowGroupData { range: group_range, .. }) = &mut row_group_data { if let Some(RowGroupData { range: group_range, kind, .. }) =
&mut row_group_data
{
*group_range = Some( *group_range = Some(
expand_row_group( expand_row_group(
resolved_cells, resolved_cells,
group_range.as_ref(), group_range.as_ref(),
*kind,
first_available_row, first_available_row,
y, y,
rowspan, rowspan,
@ -1683,6 +1696,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
fn expand_row_group( fn expand_row_group(
resolved_cells: &[Option<Entry<'_>>], resolved_cells: &[Option<Entry<'_>>],
group_range: Option<&Range<usize>>, group_range: Option<&Range<usize>>,
group_kind: RowGroupKind,
first_available_row: usize, first_available_row: usize,
cell_y: usize, cell_y: usize,
rowspan: usize, rowspan: usize,
@ -1706,9 +1720,10 @@ fn expand_row_group(
// 'y = 1' is the earliest row it can occupy. // 'y = 1' is the earliest row it can occupy.
if new_group_start < first_available_row { if new_group_start < first_available_row {
bail!( bail!(
"cell would cause header or footer to expand to non-empty row {}", "cell would cause {} to expand to non-empty row {}",
group_kind.name(),
first_available_row.saturating_sub(1); first_available_row.saturating_sub(1);
hint: "try moving its cells to later rows" hint: "try moving its cells to available rows"
); );
} }
@ -1770,23 +1785,11 @@ fn expand_row_group(
.map(|cells| &cells[..columns.min(cells.len())]) .map(|cells| &cells[..columns.min(cells.len())])
{ {
if new_row.iter().any(Option::is_some) { if new_row.iter().any(Option::is_some) {
// TODO: bail!(
// - Later/earlier rows might be confusing "cell would cause {} to expand to non-empty row {new_y}",
// (moving to the end always works...) group_kind.name();
// - Detect when header or footer collided with hint: "try moving its cells to available rows",
// another header or footer and provide a )
// better error message if so.
if group_range.is_none_or(|r| new_y < r.start) {
bail!(
"cell would cause header or footer to expand to non-empty row {new_y}";
hint: "try moving its cells to later rows"
);
} else {
bail!(
"cell would cause header or footer to expand to non-empty row {new_y}";
hint: "try moving its cells to earlier rows"
);
}
} }
} else { } else {
// Received 'None' or an empty slice, so we are expanding the // Received 'None' or an empty slice, so we are expanding the

View File

@ -458,8 +458,8 @@
table.footer( table.footer(
table.hline(stroke: red), table.hline(stroke: red),
table.hline(y: 1, stroke: aqua), table.hline(y: 1, stroke: aqua),
// Error: 5-24 cell would cause header or footer to expand to non-empty row 0 // Error: 5-24 cell would cause footer to expand to non-empty row 0
// Hint: 5-24 try moving its cells to later rows // Hint: 5-24 try moving its cells to available rows
table.cell(y: 0)[b], table.cell(y: 0)[b],
[c] [c]
) )
@ -474,8 +474,8 @@
table.cell(y: 1)[a], table.cell(y: 1)[a],
table.footer( table.footer(
[b], [c], [b], [c],
// Error: 6-7 cell would cause header or footer to expand to non-empty row 1 // Error: 6-7 cell would cause footer to expand to non-empty row 1
// Hint: 6-7 try moving its cells to earlier rows // Hint: 6-7 try moving its cells to available rows
[d], [d],
), ),
) )
@ -489,8 +489,8 @@
table.cell(y: 2)[a], table.cell(y: 2)[a],
table.footer( table.footer(
[b], [c], [b], [c],
// Error: 5-24 cell would cause header or footer to expand to non-empty row 2 // Error: 5-24 cell would cause footer to expand to non-empty row 2
// Hint: 5-24 try moving its cells to earlier rows // Hint: 5-24 try moving its cells to available rows
table.cell(y: 3)[d], table.cell(y: 3)[d],
), ),
) )
@ -500,8 +500,8 @@
columns: 2, columns: 2,
table.header(), table.header(),
table.footer( table.footer(
// Error: 5-24 cell would cause header or footer to expand to non-empty row 0 // Error: 5-24 cell would cause footer to expand to non-empty row 0
// Hint: 5-24 try moving its cells to later rows // Hint: 5-24 try moving its cells to available rows
table.cell(y: 0)[a] table.cell(y: 0)[a]
), ),
) )

View File

@ -403,8 +403,8 @@
table.header( table.header(
table.hline(stroke: red), table.hline(stroke: red),
table.hline(y: 1, stroke: aqua), table.hline(y: 1, stroke: aqua),
// Error: 5-24 cell would cause header or footer to expand to non-empty row 0 // Error: 5-24 cell would cause header to expand to non-empty row 0
// Hint: 5-24 try moving its cells to later rows // Hint: 5-24 try moving its cells to available rows
table.cell(y: 0)[b], table.cell(y: 0)[b],
[c] [c]
) )
@ -423,8 +423,8 @@
table.header( table.header(
table.hline(stroke: red), table.hline(stroke: red),
table.hline(y: 3, stroke: aqua), table.hline(y: 3, stroke: aqua),
// Error: 5-24 cell would cause header or footer to expand to non-empty row 2 // Error: 5-24 cell would cause header to expand to non-empty row 2
// Hint: 5-24 try moving its cells to later rows // Hint: 5-24 try moving its cells to available rows
table.cell(y: 2)[b], table.cell(y: 2)[b],
) )
) )
@ -438,8 +438,8 @@
table.cell(y: 1)[a], table.cell(y: 1)[a],
table.header( table.header(
[b], [c], [b], [c],
// Error: 6-7 cell would cause header or footer to expand to non-empty row 1 // Error: 6-7 cell would cause header to expand to non-empty row 1
// Hint: 6-7 try moving its cells to earlier rows // Hint: 6-7 try moving its cells to available rows
[d], [d],
), ),
) )
@ -453,8 +453,8 @@
table.cell(y: 2)[a], table.cell(y: 2)[a],
table.header( table.header(
[b], [c], [b], [c],
// Error: 5-24 cell would cause header or footer to expand to non-empty row 2 // Error: 5-24 cell would cause header to expand to non-empty row 2
// Hint: 5-24 try moving its cells to earlier rows // Hint: 5-24 try moving its cells to available rows
table.cell(y: 3)[d], table.cell(y: 3)[d],
), ),
) )