justintsus-experiment Public
// The search query language of the Issues and Pull requests lists, for plain .ts code: a thin adapter// over the .tsus modules next to it (parse, serialize, sql, edit), which hold all the logic. It turns// their Result and Option values into plain objects and nothing else. Loads in plain Node (no// cloudflare:* modules, no bindings), so unit tests import it directly after npm run tsus:web.import { None, Some } from "@tsus/runtime";import type { ListState, QueryError, QueryErrorCode, SearchQuery, Target } from "./ast.js";import { tabOf as tabOfQuery, withLabel as withLabelQuery, withState as withStateQuery } from "./edit.js";import { parseQuery as parse } from "./parse.js";import { serializeQuery as serialize } from "./serialize.js";import { toSql as sql } from "./sql.js";export type { ListState, QueryError, QueryErrorCode, SearchQuery, Target };export type SqlValue = string | number | null;export type SqlCtx = { repoId: string; orgId: string; viewerId: string | null; now: number;};export function parseQuery(input: string, target: Target): { ok: true; query: SearchQuery } | { ok: false; errors: QueryError[] } { const r = parse(input, target); return r.isOk() ? { ok: true, query: r.value } : { ok: false, errors: [...r.error.errors] };}export function serializeQuery(query: SearchQuery): string { return serialize(query);}export function toSql(query: SearchQuery, ctx: SqlCtx): { where: string; params: SqlValue[]; orderBy: string } { const r = sql(query, { repoId: ctx.repoId, orgId: ctx.orgId, viewer: ctx.viewerId === null ? None : Some(ctx.viewerId), now: ctx.now }); return { where: r.where, params: [...r.params], orderBy: r.orderBy };}// For the pages' links: the state tab a query is on (null: none of them), the query on another tab// (null: All), and the query with a label added (null when the name is no label).export function tabOf(query: SearchQuery): ListState | null { const t = tabOfQuery(query); return t.isSome() ? t.value : null;}export function withState(query: SearchQuery, state: ListState | null): SearchQuery { return withStateQuery(query, state === null ? None : Some(state));}export function withLabel(query: SearchQuery, label: string): SearchQuery | null { const q = withLabelQuery(query, label); return q.isSome() ? q.value : null;}