/** * Shared `:[,:…]` pair-list entry resolver for `render` flags whose * names may themselves carry `:` (`--seam`, `--pass-through`, `--angle-range`, * issue #1802). * * Pure: no clock, no randomness, no runtime. */ export interface PairFlagSpec { flag: '--seam' | '--pass-through' | '--angle-range'; form: string; noun: 'slots' | 'bones'; emptyRoles: readonly [string, string]; unknownRoles: readonly [string, string]; } /** * Split `raw` on `,` and resolve each entry against `names` by finding the `:` * position where both trimmed sides are non-empty known names. * * - An empty entry, or an entry with no `:` at all, is refused naming the entry * and `form`. * - Exactly one matching `:` position returns that `[left, right]` pair. * - More than one matching `:` position is refused by name, printing every * reading so nothing is guessed. * - Zero matching `:` positions: when the entry has one `:`, the refusal is * unchanged from before (empty-side or per-side unknown-name with the roster); * when it has two or more `:`, the refusal names the entry and the roster. */ export function resolveFlagPairs( raw: string, names: readonly string[], spec: PairFlagSpec, makeError: (message: string) => Error, ): Array<[string, string]> { const known = new Set(names); const out: Array<[string, string]> = []; const entries = raw.split(','); for (let i = 0; i < entries.length; i++) { const entry = entries[i].trim(); if (entry === '') { throw makeError(`${spec.flag} ${JSON.stringify(raw)}: entry ${i} is empty — it takes ${spec.form}`); } const colons: number[] = []; for (let k = 0; k < entry.length; k++) { if (entry[k] === ':') colons.push(k); } if (colons.length === 0) { throw makeError(`${spec.flag}: entry ${i} ${JSON.stringify(entry)} has 0 ":" — it takes ${spec.form}`); } const matches: Array<[string, string]> = []; for (const at of colons) { const left = entry.slice(0, at).trim(); const right = entry.slice(at + 1).trim(); if (left !== '' && right !== '' && known.has(left) && known.has(right)) { matches.push([left, right]); } } if (matches.length === 1) { out.push(matches[0]); continue; } if (matches.length > 1) { const readings = matches.map(([left, right]) => `${JSON.stringify(left)} : ${JSON.stringify(right)}`).join(', '); throw makeError( `${spec.flag}: entry ${i} ${JSON.stringify(entry)} is ambiguous — it matches ${matches.length} pairs of existing ${spec.noun} [${readings}]; ` + `a pair entry must have a single ":" split into known ${spec.noun}`, ); } if (colons.length === 1) { const at = colons[0]; const sides = [entry.slice(0, at).trim(), entry.slice(at + 1).trim()] as const; for (const side of [0, 1] as const) { const name = sides[side]; if (name === '') { throw makeError(`${spec.flag}: entry ${i} ${JSON.stringify(entry)} names no ${spec.emptyRoles[side]} — it takes ${spec.form}`); } if (!known.has(name)) { throw makeError( `${spec.flag}: entry ${i} names ${spec.unknownRoles[side]} ${JSON.stringify(name)}, which this skeleton does not have; its ${spec.noun} are [${names.join(', ')}]`, ); } } } throw makeError( `${spec.flag}: entry ${i} ${JSON.stringify(entry)} has no ":" where both sides are ${spec.noun} this skeleton has; its ${spec.noun} are [${names.join(', ')}]`, ); } return out; }