/** * A project's brand kit as `project_out` answers it * (crates/cotton-tools/src/mcp/handlers/projects/brand.rs `read_project_brand`): * the stored value parsed into its canonical shape, or nothing when it does * not parse. The column is longtext, so a kit stored as JSON text is read as * the object it spells. */ type Data = Record; const FONT_ROLES = ["display", "body", "mono"]; class Bad extends Error {} function chars(value: string): number { return Array.from(value).length; } function readText(source: Data, key: string): string | null { const v = source[key]; if (v === undefined || v === null) return null; if (typeof v !== "string") throw new Bad(); return v.trim() || null; } function list(raw: unknown, max: number): Data[] { if (!Array.isArray(raw) || raw.length > max) throw new Bad(); return raw.map((e) => { if (!e || typeof e !== "object" || Array.isArray(e)) throw new Bad(); return e as Data; }); } function need(value: string | null, max: number): string { if (value === null || chars(value) > max) throw new Bad(); return value; } function parse(value: unknown): Data { if (!value || typeof value !== "object" || Array.isArray(value)) throw new Bad(); const v = value as Data; const colors = v.colors === undefined ? [] : list(v.colors, 12).map((c) => { const name = need(readText(c, "name"), 60); const hex = readText(c, "hex"); if (!hex || !/^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/.test(hex)) throw new Bad(); return { name, hex: hex.toLowerCase() }; }); const fonts = v.fonts === undefined ? [] : list(v.fonts, 6).map((f) => { const role = f.role; if (typeof role !== "string" || !FONT_ROLES.includes(role)) throw new Bad(); const family = need(readText(f, "family"), 120); const source = readText(f, "source"); if (source !== null && chars(source) > 500) throw new Bad(); return source === null ? { role, family } : { role, family, source }; }); const text = (key: string, max: number): string => { const raw = v[key]; if (raw === undefined || raw === null) return ""; if (typeof raw !== "string") throw new Bad(); const t = raw.trim(); if (chars(t) > max) throw new Bad(); return t; }; const voice = text("voice", 2000); const tagline = text("tagline", 200); const links = v.links === undefined ? [] : list(v.links, 12).map((l) => { const label = need(readText(l, "label"), 80); const url = need(readText(l, "url"), 500); const lowered = url.toLowerCase(); const rest = lowered.startsWith("https://") ? lowered.slice(8) : lowered.startsWith("http://") ? lowered.slice(7) : ""; if (!rest || /\s/u.test(url)) throw new Bad(); return { label, url }; }); const brand: Data = { colors, fonts, voice }; if (tagline) brand.tagline = tagline; if (links.length) brand.links = links; return brand; } export function readProjectBrand(stored: unknown): Data | null { if (stored === undefined || stored === null) return null; let value = stored; if (typeof stored === "string") { try { value = JSON.parse(stored); } catch { return null; } } try { return parse(value); } catch (error) { if (error instanceof Bad) return null; throw error; } }