import { z } from "zod"; // -------- Encodable -------- // const encodable_text_schema = z.object({ t: z.literal("Text"), c: z.string().nullish().transform((x) => x ?? undefined), }); const encodable_timestamp_schema = z.object({ t: z.literal("Timestamp"), c: z.coerce.date().nullish().transform((x) => x ?? undefined), }); const encodable_uuid_schema = z.object({ t: z.literal("Uuid"), c: z.string().nullish().transform((x) => x ?? undefined), }); export const encodable_schema = z.union([ encodable_text_schema, encodable_timestamp_schema, encodable_uuid_schema, ]); export type Encodable = z.infer; // -------- FieldType -------- // const integer_field_type_schema = z.object({ t: z.literal("Integer"), c: z.unknown(), }); const text_field_type_schema = z.object({ t: z.literal("Text"), c: z.unknown(), }); const uuid_field_type_schema = z.object({ t: z.literal("Uuid"), c: z.unknown(), }); export const field_type_schema = z.union([ integer_field_type_schema, text_field_type_schema, uuid_field_type_schema, ]); export type FieldType = z.infer; export function get_empty_encodable_for(field_type: FieldType): Encodable { if (field_type.t === "Text") { return { t: "Text", c: undefined }; } if (field_type.t === "Uuid") { return { t: "Uuid", c: undefined }; } throw new Error("Unknown field type"); } // -------- Field -------- // export const field_schema = z.object({ id: z.string(), name: z.string(), label: z.string().nullish().transform((x) => x ?? undefined), field_type: field_type_schema, width_px: z.number(), }); export type Field = z.infer; export const field_info_schema = z.object({ field: field_schema, has_default: z.boolean(), not_null: z.boolean(), }); export type FieldInfo = z.infer; // -------- Table Utils -------- // // TODO move this to its own module export type Coords = [number, number]; export type Row = { key: string | number; data: Encodable[]; }; export function coords_eq(a: Coords, b: Coords): boolean { return a[0] === b[0] && a[1] === b[1]; }