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,
}
#[derive(Debug, Clone, Copy)]
enum RowGroupKind {
Header,
Footer,
}
impl RowGroupKind {
fn name(self) -> &'static str {
match self {
Self::Header => "header",
Self::Footer => "footer",
}
}
}
struct RowGroupData {
/// The range of rows of cells inside this grid row group. The
/// 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
// 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(
expand_row_group(
resolved_cells,
group_range.as_ref(),
*kind,
first_available_row,
y,
rowspan,
@ -1683,6 +1696,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
fn expand_row_group(
resolved_cells: &[Option<Entry<'_>>],
group_range: Option<&Range<usize>>,
group_kind: RowGroupKind,
first_available_row: usize,
cell_y: usize,
rowspan: usize,
@ -1706,9 +1720,10 @@ fn expand_row_group(
// 'y = 1' is the earliest row it can occupy.
if new_group_start < first_available_row {
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);
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())])
{
if new_row.iter().any(Option::is_some) {
// TODO:
// - Later/earlier rows might be confusing
// (moving to the end always works...)
// - Detect when header or footer collided with
// 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"
);
}
"cell would cause {} to expand to non-empty row {new_y}",
group_kind.name();
hint: "try moving its cells to available rows",
)
}
} else {
// Received 'None' or an empty slice, so we are expanding the

View File

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

View File

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