import { type BranchViewAnswer, type BranchViewInfo, type CaseLabelInfo, type CheckedLeanView, leanViewChecked, type LeanViewTitle, type LemmaViewAnswer, type LemmaViewInfo, list, } from "../ir.ts"; type Inspect = (value: unknown, options: { breakLength: number }) => string; const VIEW_EXPORTS = ["title", "lemma", "group", "branch", "caseLabel"]; const TITLE_KEYS = ["lemmas", "proof"]; const LEMMA_KEYS = ["label", "group", "hide"]; const BRANCH_KEYS = ["label", "detail"]; const isFunction = (v: unknown): v is (...args: unknown[]) => unknown => typeof v === "function"; const isPlainObject = (v: unknown): v is { [key: string]: unknown } => { if (typeof v !== "object" || v === null) return false; const proto = Object.getPrototypeOf(v); return proto === Object.prototype || proto === null; }; const isValidLabel = (v: unknown): v is string => typeof v === "string" && v.trim() !== "" && !/\bundefined\b|\bNaN\b|\[object Object\]/.test(v); export function checkLeanView( view: { readonly [name: string]: unknown }, inspect: Inspect, ): CheckedLeanView { const literal = (v: unknown) => typeof v === "string" ? JSON.stringify(v) : inspect(v, { breakLength: Infinity }); const extra = Object.keys(view).filter((name) => !VIEW_EXPORTS.includes(name)); if (extra.length > 0) { throw new Error( `The view exports ${list(extra)}, which the command does not read. Export only ${list(VIEW_EXPORTS)}.`, ); } let title: LeanViewTitle | undefined; const rawTitle = view.title; if (rawTitle !== undefined) { if (typeof rawTitle === "string") { if (!isValidLabel(rawTitle)) { throw new Error(`The view's title must be a non-empty string, got ${literal(rawTitle)}.`); } title = { lemmas: rawTitle, proof: rawTitle }; } else if (isPlainObject(rawTitle)) { const badKeys = Object.keys(rawTitle).filter((k) => !TITLE_KEYS.includes(k)); if (badKeys.length > 0) { throw new Error( `The view's title has unknown keys ${list(badKeys)}. Use only ${list(TITLE_KEYS)}.`, ); } const lemmas = rawTitle.lemmas; const proof = rawTitle.proof; if (lemmas !== undefined && !isValidLabel(lemmas)) { throw new Error( `The view's title.lemmas must be a non-empty string, got ${literal(lemmas)}.`, ); } if (proof !== undefined && !isValidLabel(proof)) { throw new Error( `The view's title.proof must be a non-empty string, got ${literal(proof)}.`, ); } title = { lemmas, proof }; } else { throw new Error("The view's title must be a string or a plain object { lemmas?, proof? }."); } } const wrong: string[] = []; const pickFn = (name: string) => { const v = view[name]; if (v === undefined || isFunction(v)) return v; wrong.push(`${name} must be a function`); return undefined; }; const rawLemma = pickFn("lemma"); const rawGroup = pickFn("group"); const rawBranch = pickFn("branch"); const rawCaseLabel = pickFn("caseLabel"); if (wrong.length > 0) throw new Error(`The view's ${list(wrong)}.`); const lemma = rawLemma && ((info: LemmaViewInfo): LemmaViewAnswer | undefined => { const res = rawLemma(info); if (res === undefined) return undefined; if (res === null) return { hide: true }; if (typeof res === "string") { if (!isValidLabel(res)) { throw new Error( `The view's lemma() returned ${literal(res)} for ${info.userName}. Return a non-empty string, { label, group?, hide? }, null, or undefined.`, ); } return { hide: false, label: res, group: undefined }; } if (isPlainObject(res)) { const badKeys = Object.keys(res).filter((k) => !LEMMA_KEYS.includes(k)); if (badKeys.length > 0) { throw new Error( `The view's lemma() returned unknown keys ${list(badKeys)} for ${info.userName}. Use only ${list(LEMMA_KEYS)}.`, ); } if (res.hide !== undefined && typeof res.hide !== "boolean") { throw new Error( `The view's lemma() returned non-boolean hide ${literal(res.hide)} for ${info.userName}.`, ); } if (res.hide === true) return { hide: true }; if (!isValidLabel(res.label)) { throw new Error( `The view's lemma() returned invalid label ${literal(res.label)} for ${info.userName}.`, ); } if (res.group !== undefined && !isValidLabel(res.group)) { throw new Error( `The view's lemma() returned invalid group ${literal(res.group)} for ${info.userName}.`, ); } return { hide: false, label: res.label, group: res.group }; } throw new Error( `The view's lemma() returned ${literal(res)} for ${info.userName}. Return a non-empty string, { label, group?, hide? }, null, or undefined.`, ); }); const group = rawGroup && ((info: LemmaViewInfo): string | null | undefined => { const res = rawGroup(info); if (res === undefined || res === null) return res; if (isValidLabel(res)) return res; throw new Error( `The view's group() returned ${literal(res)} for ${info.userName}. Return a non-empty string, null, or undefined.`, ); }); const branch = rawBranch && ((info: BranchViewInfo): BranchViewAnswer | undefined => { const res = rawBranch(info); if (res === undefined) return undefined; if (typeof res === "string") { if (!isValidLabel(res)) { throw new Error( `The view's branch() returned ${literal(res)} for ${info.id}. Return a non-empty string, { label, detail? }, or undefined.`, ); } return { label: res, detail: undefined }; } if (isPlainObject(res)) { const badKeys = Object.keys(res).filter((k) => !BRANCH_KEYS.includes(k)); if (badKeys.length > 0) { throw new Error( `The view's branch() returned unknown keys ${list(badKeys)} for ${info.id}. Use only ${list(BRANCH_KEYS)}.`, ); } if (!isValidLabel(res.label)) { throw new Error( `The view's branch() returned invalid label ${literal(res.label)} for ${info.id}.`, ); } if (res.detail !== undefined && !isValidLabel(res.detail)) { throw new Error( `The view's branch() returned invalid detail ${literal(res.detail)} for ${info.id}.`, ); } return { label: res.label, detail: res.detail }; } throw new Error( `The view's branch() returned ${literal(res)} for ${info.id}. Return a non-empty string, { label, detail? }, or undefined.`, ); }); const caseLabel = rawCaseLabel && ((tag: string, info: CaseLabelInfo): string | undefined => { const res = rawCaseLabel(tag, info); if (res === undefined) return undefined; if (isValidLabel(res)) return res; throw new Error( `The view's caseLabel() returned ${literal(res)} for ${tag}. Return a non-empty string or undefined.`, ); }); return { [leanViewChecked]: true, title, lemma, group, branch, caseLabel, }; }