- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/keys.ts?version=0&format=json
91 linesFirst import 100%Last change First import in version 0, 1 version changed it
You're reading version 0. The newest is 318, and no version since then changed this file.
1// Small helpers for ids and claim keys.
3import { ATTR_NAMES, type AtomId, type AttrName, EngineError, type Key, type LineId, type NodeId } from "./types.ts";
5/** The start node of a file: "^<path>". */
6export function startNode(path: string): NodeId {
7 return `^${path}`;
8}
10export function isStartNode(node: NodeId): boolean {
11 return node.startsWith("^");
12}
14export function atomId(seq: number): AtomId {
15 return `a${seq}`;
16}
18/** The seq inside an atom id, or -1 if it isn't one. */
19export function seqOf(id: AtomId): number {
20 // Hand-parsed: this runs on every atom lookup. Same rule as /^a(0|[1-9][0-9]*)$/.
21 const n = id.length;
22 if (n < 2 || n > 16 || id.charCodeAt(0) !== 97 /* a */ || (n > 2 && id.charCodeAt(1) === 48) /* leading 0 */)
23 return -1;
24 let seq = 0;
25 for (let i = 1; i < n; i++) {
26 const c = id.charCodeAt(i) - 48;
27 if (c < 0 || c > 9) return -1;
28 seq = seq * 10 + c;
29 }
30 return seq;
31}
33export function isAtomId(s: string): boolean {
34 return seqOf(s) >= 0;
35}
37export function lineId(atom: AtomId, k: number): LineId {
38 return `${atom}.${k}`;
39}
41/** The atom that wrote a line. */
42export function writerOf(line: LineId): AtomId {
43 const dot = line.indexOf(".");
44 if (dot < 0) throw new EngineError("unknown-line", `not a line id: ${line}`);
45 return line.slice(0, dot);
46}
48export function lineKey(line: LineId): Key {
49 return `L:${line}`;
50}
52export function gapKey(node: NodeId): Key {
53 return `G:${node}`;
54}
56/** The key of one attribute of a file: "A:<attr>:<path>". */
57export function attrKey(file: string, name: AttrName): Key {
58 return `A:${name}:${file}`;
59}
61/** The key of where a line sits: "P:<lineId>". A move writes it. */
62export function posKey(line: LineId): Key {
63 return `P:${line}`;
64}
66/** For a line, gap or position key, `node` is the line (or start node); for an attribute key, the file. */
67export interface ParsedKey {
68 readonly kind: "line" | "gap" | "attr" | "pos";
69 readonly node: NodeId;
70 readonly attr?: AttrName;
71}
73export function parseKey(key: Key): ParsedKey {
74 if (key.startsWith("L:") && key.length > 2 && !isStartNode(key.slice(2))) {
75 return { kind: "line", node: key.slice(2) };
76 }
77 if (key.startsWith("G:") && key.length > 2) return { kind: "gap", node: key.slice(2) };
78 if (key.startsWith("P:") && key.length > 2 && !isStartNode(key.slice(2))) return { kind: "pos", node: key.slice(2) };
79 if (key.startsWith("A:")) {
80 const colon = key.indexOf(":", 2);
81 const attr = key.slice(2, colon) as AttrName;
82 if (colon > 2 && ATTR_NAMES.includes(attr) && key.length > colon + 1)
83 return { kind: "attr", node: key.slice(colon + 1), attr };
84 }
85 throw new EngineError("bad-atom", `not a claim key: ${key}`);
86}
88/** The line a key names, if it is an L: key. */
89export function lineOfKey(key: Key): LineId | undefined {
90 return key.startsWith("L:") ? key.slice(2) : undefined;
91}