gam_problem/block_count_error.rs
1//! Neutral block-count arity mismatch carrier.
2//!
3//! Descended from `gam-models`' `block_layout::block_count` so that lower
4//! tiers (e.g. `gam-terms`) can route their own error enums through
5//! `From<BlockCountMismatch>` without depending on `gam-models`. The
6//! `validate_block_count` helper continues to live in `gam-models` and
7//! re-exports this type unchanged.
8
9/// A block-count arity mismatch: a family that needs exactly `expected`
10/// parameter blocks was handed `got` of them.
11///
12/// This is the neutral carrier produced by `validate_block_count`; each
13/// module converts it into its own error type via `From<BlockCountMismatch>`.
14pub struct BlockCountMismatch {
15 /// Human-readable family / term name used as the message prefix.
16 pub family: String,
17 /// Number of parameter blocks the family requires.
18 pub expected: usize,
19 /// Number of parameter blocks that were actually supplied.
20 pub got: usize,
21}
22
23impl BlockCountMismatch {
24 /// The canonical arity-mismatch message, e.g.
25 /// `"FooFamily expects 2 blocks, got 1"` (plural) or
26 /// `"BarFamily expects 1 block, got 0"` (singular when `expected == 1`).
27 pub fn message(&self) -> String {
28 let unit = if self.expected == 1 {
29 "block"
30 } else {
31 "blocks"
32 };
33 format!(
34 "{} expects {} {unit}, got {}",
35 self.family, self.expected, self.got
36 )
37 }
38}
39
40impl From<BlockCountMismatch> for String {
41 fn from(err: BlockCountMismatch) -> String {
42 err.message()
43 }
44}