@azx-pbc/axiom-builder 0.0.0-stage → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +648 -2
  3. package/dist/cards/selectors.d.ts +70 -0
  4. package/dist/cards/selectors.d.ts.map +1 -0
  5. package/dist/cards/selectors.js +194 -0
  6. package/dist/cards/selectors.js.map +1 -0
  7. package/dist/definition.d.ts +36 -0
  8. package/dist/definition.d.ts.map +1 -0
  9. package/dist/definition.js +48 -0
  10. package/dist/definition.js.map +1 -0
  11. package/dist/evidence/cases.d.ts +44 -0
  12. package/dist/evidence/cases.d.ts.map +1 -0
  13. package/dist/evidence/cases.js +578 -0
  14. package/dist/evidence/cases.js.map +1 -0
  15. package/dist/evidence/repairs.d.ts +99 -0
  16. package/dist/evidence/repairs.d.ts.map +1 -0
  17. package/dist/evidence/repairs.js +181 -0
  18. package/dist/evidence/repairs.js.map +1 -0
  19. package/dist/evidence/summary.d.ts +35 -0
  20. package/dist/evidence/summary.d.ts.map +1 -0
  21. package/dist/evidence/summary.js +38 -0
  22. package/dist/evidence/summary.js.map +1 -0
  23. package/dist/i18n/gaps.d.ts +3 -0
  24. package/dist/i18n/gaps.d.ts.map +1 -0
  25. package/dist/i18n/gaps.js +2 -0
  26. package/dist/i18n/gaps.js.map +1 -0
  27. package/dist/i18n/message-slots.d.ts +19 -0
  28. package/dist/i18n/message-slots.d.ts.map +1 -0
  29. package/dist/i18n/message-slots.js +101 -0
  30. package/dist/i18n/message-slots.js.map +1 -0
  31. package/dist/i18n/text-locale.d.ts +13 -0
  32. package/dist/i18n/text-locale.d.ts.map +1 -0
  33. package/dist/i18n/text-locale.js +14 -0
  34. package/dist/i18n/text-locale.js.map +1 -0
  35. package/dist/i18n/translations.d.ts +56 -0
  36. package/dist/i18n/translations.d.ts.map +1 -0
  37. package/dist/i18n/translations.js +103 -0
  38. package/dist/i18n/translations.js.map +1 -0
  39. package/dist/impact/references.d.ts +143 -0
  40. package/dist/impact/references.d.ts.map +1 -0
  41. package/dist/impact/references.js +429 -0
  42. package/dist/impact/references.js.map +1 -0
  43. package/dist/index.d.ts +86 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +58 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/preview/compile-preview.d.ts +67 -0
  48. package/dist/preview/compile-preview.d.ts.map +1 -0
  49. package/dist/preview/compile-preview.js +286 -0
  50. package/dist/preview/compile-preview.js.map +1 -0
  51. package/dist/preview/handle.d.ts +119 -0
  52. package/dist/preview/handle.d.ts.map +1 -0
  53. package/dist/preview/handle.js +2 -0
  54. package/dist/preview/handle.js.map +1 -0
  55. package/dist/preview/held.d.ts +39 -0
  56. package/dist/preview/held.d.ts.map +1 -0
  57. package/dist/preview/held.js +15 -0
  58. package/dist/preview/held.js.map +1 -0
  59. package/dist/preview/measurement.d.ts +5 -0
  60. package/dist/preview/measurement.d.ts.map +1 -0
  61. package/dist/preview/measurement.js +18 -0
  62. package/dist/preview/measurement.js.map +1 -0
  63. package/dist/publish/build.d.ts +33 -0
  64. package/dist/publish/build.d.ts.map +1 -0
  65. package/dist/publish/build.js +15 -0
  66. package/dist/publish/build.js.map +1 -0
  67. package/dist/publish/diff.d.ts +208 -0
  68. package/dist/publish/diff.d.ts.map +1 -0
  69. package/dist/publish/diff.js +227 -0
  70. package/dist/publish/diff.js.map +1 -0
  71. package/dist/publish/review.d.ts +41 -0
  72. package/dist/publish/review.d.ts.map +1 -0
  73. package/dist/publish/review.js +22 -0
  74. package/dist/publish/review.js.map +1 -0
  75. package/dist/schema/dataset-input.d.ts +30 -0
  76. package/dist/schema/dataset-input.d.ts.map +1 -0
  77. package/dist/schema/dataset-input.js +58 -0
  78. package/dist/schema/dataset-input.js.map +1 -0
  79. package/dist/schema/dataset.d.ts +166 -0
  80. package/dist/schema/dataset.d.ts.map +1 -0
  81. package/dist/schema/dataset.js +281 -0
  82. package/dist/schema/dataset.js.map +1 -0
  83. package/dist/schema/declarations.d.ts +15 -0
  84. package/dist/schema/declarations.d.ts.map +1 -0
  85. package/dist/schema/declarations.js +47 -0
  86. package/dist/schema/declarations.js.map +1 -0
  87. package/dist/schema/document.d.ts +504 -0
  88. package/dist/schema/document.d.ts.map +1 -0
  89. package/dist/schema/document.js +337 -0
  90. package/dist/schema/document.js.map +1 -0
  91. package/dist/schema/error.d.ts +15 -0
  92. package/dist/schema/error.d.ts.map +1 -0
  93. package/dist/schema/error.js +18 -0
  94. package/dist/schema/error.js.map +1 -0
  95. package/dist/schema/field-id.d.ts +8 -0
  96. package/dist/schema/field-id.d.ts.map +1 -0
  97. package/dist/schema/field-id.js +47 -0
  98. package/dist/schema/field-id.js.map +1 -0
  99. package/dist/schema/layout.d.ts +118 -0
  100. package/dist/schema/layout.d.ts.map +1 -0
  101. package/dist/schema/layout.js +101 -0
  102. package/dist/schema/layout.js.map +1 -0
  103. package/dist/schema/palette.d.ts +103 -0
  104. package/dist/schema/palette.d.ts.map +1 -0
  105. package/dist/schema/palette.js +140 -0
  106. package/dist/schema/palette.js.map +1 -0
  107. package/dist/schema/read.d.ts +49 -0
  108. package/dist/schema/read.d.ts.map +1 -0
  109. package/dist/schema/read.js +942 -0
  110. package/dist/schema/read.js.map +1 -0
  111. package/dist/store/apply-dataset.d.ts +14 -0
  112. package/dist/store/apply-dataset.d.ts.map +1 -0
  113. package/dist/store/apply-dataset.js +513 -0
  114. package/dist/store/apply-dataset.js.map +1 -0
  115. package/dist/store/apply.d.ts +37 -0
  116. package/dist/store/apply.d.ts.map +1 -0
  117. package/dist/store/apply.js +2269 -0
  118. package/dist/store/apply.js.map +1 -0
  119. package/dist/store/candidate.d.ts +37 -0
  120. package/dist/store/candidate.d.ts.map +1 -0
  121. package/dist/store/candidate.js +178 -0
  122. package/dist/store/candidate.js.map +1 -0
  123. package/dist/store/commands/coverage.d.ts +58 -0
  124. package/dist/store/commands/coverage.d.ts.map +1 -0
  125. package/dist/store/commands/coverage.js +354 -0
  126. package/dist/store/commands/coverage.js.map +1 -0
  127. package/dist/store/commands/dataset.d.ts +26 -0
  128. package/dist/store/commands/dataset.d.ts.map +1 -0
  129. package/dist/store/commands/dataset.js +368 -0
  130. package/dist/store/commands/dataset.js.map +1 -0
  131. package/dist/store/commands/document.d.ts +21 -0
  132. package/dist/store/commands/document.d.ts.map +1 -0
  133. package/dist/store/commands/document.js +160 -0
  134. package/dist/store/commands/document.js.map +1 -0
  135. package/dist/store/commands/field.d.ts +32 -0
  136. package/dist/store/commands/field.d.ts.map +1 -0
  137. package/dist/store/commands/field.js +572 -0
  138. package/dist/store/commands/field.js.map +1 -0
  139. package/dist/store/commands/layout.d.ts +16 -0
  140. package/dist/store/commands/layout.d.ts.map +1 -0
  141. package/dist/store/commands/layout.js +70 -0
  142. package/dist/store/commands/layout.js.map +1 -0
  143. package/dist/store/commands/parse.d.ts +19 -0
  144. package/dist/store/commands/parse.d.ts.map +1 -0
  145. package/dist/store/commands/parse.js +328 -0
  146. package/dist/store/commands/parse.js.map +1 -0
  147. package/dist/store/commands/readers.d.ts +198 -0
  148. package/dist/store/commands/readers.d.ts.map +1 -0
  149. package/dist/store/commands/readers.js +711 -0
  150. package/dist/store/commands/readers.js.map +1 -0
  151. package/dist/store/commands/rule.d.ts +24 -0
  152. package/dist/store/commands/rule.d.ts.map +1 -0
  153. package/dist/store/commands/rule.js +379 -0
  154. package/dist/store/commands/rule.js.map +1 -0
  155. package/dist/store/commands/shapes.d.ts +900 -0
  156. package/dist/store/commands/shapes.d.ts.map +1 -0
  157. package/dist/store/commands/shapes.js +166 -0
  158. package/dist/store/commands/shapes.js.map +1 -0
  159. package/dist/store/commands/suggestions.d.ts +37 -0
  160. package/dist/store/commands/suggestions.d.ts.map +1 -0
  161. package/dist/store/commands/suggestions.js +177 -0
  162. package/dist/store/commands/suggestions.js.map +1 -0
  163. package/dist/store/commands.d.ts +16 -0
  164. package/dist/store/commands.d.ts.map +1 -0
  165. package/dist/store/commands.js +13 -0
  166. package/dist/store/commands.js.map +1 -0
  167. package/dist/store/dataset-candidate.d.ts +33 -0
  168. package/dist/store/dataset-candidate.d.ts.map +1 -0
  169. package/dist/store/dataset-candidate.js +31 -0
  170. package/dist/store/dataset-candidate.js.map +1 -0
  171. package/dist/store/dataset-pin.d.ts +93 -0
  172. package/dist/store/dataset-pin.d.ts.map +1 -0
  173. package/dist/store/dataset-pin.js +86 -0
  174. package/dist/store/dataset-pin.js.map +1 -0
  175. package/dist/store/dataset-store.d.ts +96 -0
  176. package/dist/store/dataset-store.d.ts.map +1 -0
  177. package/dist/store/dataset-store.js +112 -0
  178. package/dist/store/dataset-store.js.map +1 -0
  179. package/dist/store/history.d.ts +149 -0
  180. package/dist/store/history.d.ts.map +1 -0
  181. package/dist/store/history.js +173 -0
  182. package/dist/store/history.js.map +1 -0
  183. package/dist/store/measurement.d.ts +5 -0
  184. package/dist/store/measurement.d.ts.map +1 -0
  185. package/dist/store/measurement.js +20 -0
  186. package/dist/store/measurement.js.map +1 -0
  187. package/dist/store/notify.d.ts +11 -0
  188. package/dist/store/notify.d.ts.map +1 -0
  189. package/dist/store/notify.js +26 -0
  190. package/dist/store/notify.js.map +1 -0
  191. package/dist/store/published.d.ts +50 -0
  192. package/dist/store/published.d.ts.map +1 -0
  193. package/dist/store/published.js +27 -0
  194. package/dist/store/published.js.map +1 -0
  195. package/dist/store/refusals.d.ts +175 -0
  196. package/dist/store/refusals.d.ts.map +1 -0
  197. package/dist/store/refusals.js +160 -0
  198. package/dist/store/refusals.js.map +1 -0
  199. package/dist/store/steps.d.ts +14 -0
  200. package/dist/store/steps.d.ts.map +1 -0
  201. package/dist/store/steps.js +124 -0
  202. package/dist/store/steps.js.map +1 -0
  203. package/dist/store/store.d.ts +142 -0
  204. package/dist/store/store.d.ts.map +1 -0
  205. package/dist/store/store.js +136 -0
  206. package/dist/store/store.js.map +1 -0
  207. package/dist/store/unchanged.d.ts +12 -0
  208. package/dist/store/unchanged.d.ts.map +1 -0
  209. package/dist/store/unchanged.js +35 -0
  210. package/dist/store/unchanged.js.map +1 -0
  211. package/dist/visual/emit.d.ts +21 -0
  212. package/dist/visual/emit.d.ts.map +1 -0
  213. package/dist/visual/emit.js +133 -0
  214. package/dist/visual/emit.js.map +1 -0
  215. package/dist/visual/parse.d.ts +32 -0
  216. package/dist/visual/parse.d.ts.map +1 -0
  217. package/dist/visual/parse.js +330 -0
  218. package/dist/visual/parse.js.map +1 -0
  219. package/dist/visual/project.d.ts +40 -0
  220. package/dist/visual/project.d.ts.map +1 -0
  221. package/dist/visual/project.js +349 -0
  222. package/dist/visual/project.js.map +1 -0
  223. package/dist/visual/represent.d.ts +50 -0
  224. package/dist/visual/represent.d.ts.map +1 -0
  225. package/dist/visual/represent.js +66 -0
  226. package/dist/visual/represent.js.map +1 -0
  227. package/dist/visual/vocabulary.d.ts +121 -0
  228. package/dist/visual/vocabulary.d.ts.map +1 -0
  229. package/dist/visual/vocabulary.js +32 -0
  230. package/dist/visual/vocabulary.js.map +1 -0
  231. package/package.json +52 -3
@@ -0,0 +1,349 @@
1
+ import { parseExpr } from '@azx-pbc/axiom-compiler';
2
+ import { assertNever, isContextRootPath } from '@azx-pbc/axiom-runtime';
3
+ import { isDateSpan, isFormatCheck, isPresenceCheck, isTextMatchOperator } from './vocabulary.js';
4
+ /**
5
+ * Recognition of a rule's parsed expression as one form of the visual vocabulary.
6
+ *
7
+ * The projection is the vocabulary's inverse and it is deliberately partial: a node outside the
8
+ * vocabulary — an aggregate, a comprehension, arithmetic, a conditional — is not approximated, it
9
+ * is left alone. `projectRule` states that outcome as the rule's advanced form, carrying the
10
+ * source the author stored byte for byte, which is what makes a visual edit unable to rewrite an
11
+ * advanced formula.
12
+ *
13
+ * Nothing is normalised on the way through. A nested connective keeps its nesting, `not(not(x))`
14
+ * stays two negations, a two-argument `is_unique` stays a member test, and a date window keeps
15
+ * the operand order and the sign the author wrote. The one question the declarations answer is
16
+ * whether a path is declared: a visual rule reads declarations, never an unbound name.
17
+ *
18
+ * A `validate` rule is shown as what makes the rule valid — "Valid when …". The projection never
19
+ * negates and no inversion mode exists, so an author's "Error when …" sentence can never become a
20
+ * silent `not(...)` that changes the rule's meaning.
21
+ */
22
+ /**
23
+ * Recognises one parsed expression as a visual form, or reports that it is outside the vocabulary.
24
+ *
25
+ * `itemFieldId` is the array whose row `$it` names; it is stated only for an `item`-scoped rule,
26
+ * so the same expression read outside that scope does not become a visual rule that claims to
27
+ * address a row.
28
+ */
29
+ export function toVisual(node, index, itemFieldId) {
30
+ switch (node.kind) {
31
+ case 'and':
32
+ case 'or': {
33
+ // The printer has no source for a connective of fewer than two operands, so neither has a
34
+ // visual form; every operand has to project for the connective itself to.
35
+ const projected = node.args.map((arg) => toVisual(arg, index, itemFieldId));
36
+ return connectiveOperands(projected, node.kind);
37
+ }
38
+ case 'not': {
39
+ const [operand] = node.args;
40
+ if (operand === undefined || node.args.length !== 1)
41
+ return undefined;
42
+ const projected = toVisual(operand, index, itemFieldId);
43
+ return projected === undefined ? undefined : { kind: 'not', operand: projected };
44
+ }
45
+ case 'compare':
46
+ return compareToVisual(node, index, itemFieldId);
47
+ case 'call':
48
+ return callToVisual(node, index, itemFieldId);
49
+ case 'literal':
50
+ case 'path':
51
+ case 'list':
52
+ case 'if':
53
+ case 'coalesce':
54
+ case 'comprehension':
55
+ // A bare value is not a condition, and a conditional, a coalesce or a comprehension is not
56
+ // one of the five leaf families: the rule is an advanced one.
57
+ return undefined;
58
+ default:
59
+ return assertNever(node);
60
+ }
61
+ }
62
+ /** The connective's operands, or `undefined` when one of them is outside the vocabulary. */
63
+ function connectiveOperands(projected, kind) {
64
+ const operands = [];
65
+ for (const member of projected) {
66
+ if (member === undefined)
67
+ return undefined;
68
+ operands.push(member);
69
+ }
70
+ const [first, second, ...rest] = operands;
71
+ if (first === undefined || second === undefined)
72
+ return undefined;
73
+ return { kind, args: [first, second, ...rest] };
74
+ }
75
+ /**
76
+ * The visual form of one comparison, preferring the count and date-window shapes it may spell.
77
+ *
78
+ * The order matters: a date window is a `compare` over a `days_between` call, and a count is one
79
+ * over a `len` call, so the leaf comparison is the shape of last resort rather than a second
80
+ * spelling of those two.
81
+ */
82
+ function compareToVisual(node, index, itemFieldId) {
83
+ const count = countToVisual(node, index, itemFieldId);
84
+ if (count !== undefined)
85
+ return count;
86
+ const window = dateWindowToVisual(node, index, itemFieldId);
87
+ if (window !== undefined)
88
+ return window;
89
+ const [left, right] = node.args;
90
+ if (left === undefined || right === undefined)
91
+ return undefined;
92
+ const leftValue = valueToVisual(left, index, itemFieldId);
93
+ if (leftValue === undefined)
94
+ return undefined;
95
+ const rightValue = valueToVisual(right, index, itemFieldId);
96
+ if (rightValue === undefined)
97
+ return undefined;
98
+ if (node.op === 'in') {
99
+ // Membership is a list on the right and a scalar on the left; any other pairing is a tree
100
+ // the vocabulary does not state, so the rule opens advanced rather than as a wrong shape.
101
+ const leftScalar = scalarOf(leftValue);
102
+ return leftScalar === undefined || rightValue.kind !== 'list'
103
+ ? undefined
104
+ : { kind: 'membership', left: leftScalar, candidates: rightValue };
105
+ }
106
+ const leftScalar = scalarOf(leftValue);
107
+ const rightScalar = scalarOf(rightValue);
108
+ return leftScalar === undefined || rightScalar === undefined
109
+ ? undefined
110
+ : { kind: 'comparison', op: node.op, left: leftScalar, right: rightScalar };
111
+ }
112
+ /** `len(path) <op> whole` when the length is counted and the bound is a whole number. */
113
+ function countToVisual(node, index, itemFieldId) {
114
+ if (node.op === 'in')
115
+ return undefined;
116
+ const [left, right] = node.args;
117
+ if (left?.kind !== 'call' || left.op !== 'len' || left.args.length !== 1)
118
+ return undefined;
119
+ const [counted] = left.args;
120
+ if (counted?.kind !== 'path' || !isDeclaredPath(counted.path, index, itemFieldId)) {
121
+ return undefined;
122
+ }
123
+ const count = right === undefined ? undefined : valueToVisual(right, index, itemFieldId);
124
+ if (count === undefined || !isIntegerOperand(count, index, itemFieldId))
125
+ return undefined;
126
+ return { kind: 'count', path: counted, op: node.op, count };
127
+ }
128
+ /** `days_between(a, b) <op> whole` over dates, with the operands in the author's order. */
129
+ function dateWindowToVisual(node, index, itemFieldId) {
130
+ if (node.op === 'in')
131
+ return undefined;
132
+ const [left, right] = node.args;
133
+ if (left?.kind !== 'call' || !isDateSpan(left.op) || left.args.length !== 2)
134
+ return undefined;
135
+ const [from, to] = left.args;
136
+ const amount = right === undefined ? undefined : valueToVisual(right, index, itemFieldId);
137
+ if (from === undefined || to === undefined || amount === undefined)
138
+ return undefined;
139
+ const fromValue = valueToVisual(from, index, itemFieldId);
140
+ const toValue = valueToVisual(to, index, itemFieldId);
141
+ const fromScalar = fromValue === undefined ? undefined : scalarOf(fromValue);
142
+ const toScalar = toValue === undefined ? undefined : scalarOf(toValue);
143
+ if (fromScalar === undefined ||
144
+ toScalar === undefined ||
145
+ !isDateOperand(fromScalar, index, itemFieldId) ||
146
+ !isDateOperand(toScalar, index, itemFieldId) ||
147
+ !isIntegerOperand(amount, index, itemFieldId)) {
148
+ return undefined;
149
+ }
150
+ return {
151
+ kind: 'date_window',
152
+ span: left.op,
153
+ op: node.op,
154
+ from: fromScalar,
155
+ to: toScalar,
156
+ amount,
157
+ };
158
+ }
159
+ /** The visual form of one call node: a check, a text match, a uniqueness test or a capability. */
160
+ function callToVisual(node, index, itemFieldId) {
161
+ if (isPresenceCheck(node.op))
162
+ return presenceToVisual(node.op, node.args, index, itemFieldId);
163
+ if (isFormatCheck(node.op))
164
+ return formatToVisual(node.op, node.args, index, itemFieldId);
165
+ if (isTextMatchOperator(node.op)) {
166
+ return textMatchToVisual(node.op, node.args, index, itemFieldId);
167
+ }
168
+ switch (node.op) {
169
+ case 'is_unique': {
170
+ const [arrayNode, memberNode] = node.args;
171
+ if (arrayNode?.kind !== 'path')
172
+ return undefined;
173
+ const array = declaredFieldPath(arrayNode.path, index, itemFieldId);
174
+ if (array?.primitive !== 'array')
175
+ return undefined;
176
+ if (node.args.length === 1)
177
+ return { kind: 'unique_value', array: arrayNode };
178
+ if (memberNode?.kind !== 'path' || node.args.length !== 2)
179
+ return undefined;
180
+ return arrayMember(array, memberNode.path) === undefined
181
+ ? undefined
182
+ : { kind: 'unique_member', array: arrayNode, member: memberNode };
183
+ }
184
+ case 'has_cap': {
185
+ const [cap] = node.args;
186
+ if (cap?.kind !== 'literal' || cap.domain !== 'string' || typeof cap.value !== 'string') {
187
+ return undefined;
188
+ }
189
+ return node.args.length === 1 ? { kind: 'capability', cap: cap.value } : undefined;
190
+ }
191
+ default:
192
+ // Every other built-in — an aggregate, arithmetic, a lookup, a digest — is advanced.
193
+ return undefined;
194
+ }
195
+ }
196
+ /** `<check>(path)`, which the vocabulary states only over a declared path. */
197
+ function presenceToVisual(check, args, index, itemFieldId) {
198
+ const [operand] = args;
199
+ if (operand?.kind !== 'path' || args.length !== 1)
200
+ return undefined;
201
+ return isDeclaredPath(operand.path, index, itemFieldId)
202
+ ? { kind: 'check', check, path: operand }
203
+ : undefined;
204
+ }
205
+ /** `is_email(v)` and its siblings, which the vocabulary states only over a string operand. */
206
+ function formatToVisual(check, args, index, itemFieldId) {
207
+ const [operand] = args;
208
+ const value = operand === undefined ? undefined : valueToVisual(operand, index, itemFieldId);
209
+ const scalar = value === undefined ? undefined : scalarOf(value);
210
+ if (scalar === undefined || args.length !== 1 || !isStringOperand(scalar, index, itemFieldId)) {
211
+ return undefined;
212
+ }
213
+ return { kind: 'format_check', check, value: scalar };
214
+ }
215
+ /**
216
+ * `contains(subject, text)` and its siblings: a subject and the text it is matched against.
217
+ *
218
+ * No operand-type check is stated here, unlike `is_email`: the language's own typing lets a text
219
+ * match read a string or a list (`EXPR`, `contains` on `string | list`), so restricting the form to
220
+ * strings would hide a rule the language states.
221
+ */
222
+ function textMatchToVisual(op, args, index, itemFieldId) {
223
+ const [subjectNode, textNode] = args;
224
+ const subjectValue = subjectNode === undefined ? undefined : valueToVisual(subjectNode, index, itemFieldId);
225
+ const textValue = textNode === undefined ? undefined : valueToVisual(textNode, index, itemFieldId);
226
+ const subject = subjectValue === undefined ? undefined : scalarOf(subjectValue);
227
+ const text = textValue === undefined ? undefined : scalarOf(textValue);
228
+ if (subject === undefined || text === undefined || args.length !== 2)
229
+ return undefined;
230
+ return { kind: 'text_match', op, subject, text };
231
+ }
232
+ /** One AST node as a visual value, when it names a declared path, a literal or a list of them. */
233
+ function valueToVisual(node, index, itemFieldId) {
234
+ switch (node.kind) {
235
+ case 'literal':
236
+ return node;
237
+ case 'path':
238
+ return isDeclaredPath(node.path, index, itemFieldId) ? node : undefined;
239
+ case 'list': {
240
+ const items = [];
241
+ for (const item of node.items) {
242
+ const value = valueToVisual(item, index, itemFieldId);
243
+ const scalar = value === undefined ? undefined : scalarOf(value);
244
+ if (scalar === undefined)
245
+ return undefined;
246
+ items.push(scalar);
247
+ }
248
+ return { kind: 'list', items };
249
+ }
250
+ default:
251
+ return undefined;
252
+ }
253
+ }
254
+ /** A value where a scalar is required, or `undefined` when it is a list. */
255
+ function scalarOf(value) {
256
+ return value.kind === 'list' ? undefined : value;
257
+ }
258
+ /** Whether a path names a declared field or a declared context root. */
259
+ function isDeclaredPath(path, index, itemFieldId) {
260
+ return (declaredFieldPath(path, index, itemFieldId) !== undefined ||
261
+ contextPrimitive(path, index) !== undefined);
262
+ }
263
+ /**
264
+ * The declared field a path names, or `undefined` when it names no field.
265
+ *
266
+ * A row member resolves against the array an `item`-scoped rule addresses, and only there: outside
267
+ * that scope `$it` names nothing, so a rule written for one row cannot be read as a document path.
268
+ */
269
+ function declaredFieldPath(path, index, itemFieldId) {
270
+ const segments = path.split('.');
271
+ const head = segments[0] ?? '';
272
+ const rest = segments.slice(1);
273
+ if (head === '$it') {
274
+ if (itemFieldId === undefined || rest.length === 0)
275
+ return undefined;
276
+ const array = index.fieldsByPath.get(itemFieldId);
277
+ return array === undefined ? undefined : descend(array, rest);
278
+ }
279
+ if (head.startsWith('$'))
280
+ return undefined;
281
+ if (head === 'context' && isContextRootPath(path, index))
282
+ return undefined;
283
+ return index.fieldsByPath.get(path);
284
+ }
285
+ /**
286
+ * The primitive a declared context root has, or `undefined` when the path names no root.
287
+ *
288
+ * Only a root the index carries resolves, which includes the reserved `record_date` and
289
+ * `schema_version` the runtime always supplies; an undeclared `context.` path is not a value a
290
+ * visual rule may claim to read.
291
+ */
292
+ function contextPrimitive(path, index) {
293
+ const segments = path.split('.');
294
+ if (segments[0] !== 'context' || segments.length < 2)
295
+ return undefined;
296
+ return index.contextRoots.get(segments.slice(1).join('.'))?.primitive;
297
+ }
298
+ /** The declared member a path reaches from a container, segment by segment. */
299
+ function descend(field, segments) {
300
+ let current = field;
301
+ for (const segment of segments) {
302
+ current = current?.fields?.find((member) => member.fieldId === segment);
303
+ }
304
+ return current;
305
+ }
306
+ /** The member an array declares by the relative path a uniqueness test names. */
307
+ function arrayMember(array, path) {
308
+ return descend(array, path.split('.'));
309
+ }
310
+ /** The primitive one scalar value has: a path's declared domain, or the literal's own domain. */
311
+ function primitiveOf(value, index, itemFieldId) {
312
+ return value.kind === 'literal'
313
+ ? value.domain
314
+ : (declaredFieldPath(value.path, index, itemFieldId)?.primitive ??
315
+ contextPrimitive(value.path, index));
316
+ }
317
+ function isIntegerOperand(value, index, itemFieldId) {
318
+ return value.kind !== 'list' && primitiveOf(value, index, itemFieldId) === 'integer';
319
+ }
320
+ function isDateOperand(value, index, itemFieldId) {
321
+ return primitiveOf(value, index, itemFieldId) === 'date';
322
+ }
323
+ function isStringOperand(value, index, itemFieldId) {
324
+ return primitiveOf(value, index, itemFieldId) === 'string';
325
+ }
326
+ /**
327
+ * The rule's expression as the form an editor shows.
328
+ *
329
+ * A `defaultCompute` writer, a rule whose source does not parse, or one that parses to a node
330
+ * outside the vocabulary is the advanced arm holding the stored source unchanged. That is the
331
+ * whole of the fallback: no reference is printed, no text is normalised, and no partial
332
+ * projection is offered.
333
+ */
334
+ export function projectRule(rule, index) {
335
+ if (rule.kind === 'defaultCompute')
336
+ return { mode: 'advanced', source: rule.expr };
337
+ const parsed = parseExpr(rule.expr);
338
+ if (!parsed.ok)
339
+ return { mode: 'advanced', source: rule.expr };
340
+ const visual = toVisual(parsed.node, index, itemScopeOf(rule));
341
+ return visual === undefined
342
+ ? { mode: 'advanced', source: rule.expr }
343
+ : { mode: 'visual', visual };
344
+ }
345
+ /** The array whose row an `item`-scoped rule addresses, and nothing for every other scope. */
346
+ function itemScopeOf(rule) {
347
+ return rule.scope === 'item' ? rule.fieldId : undefined;
348
+ }
349
+ //# sourceMappingURL=project.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"project.js","sourceRoot":"","sources":["../../src/visual/project.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAaxE,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAElG;;;;;;;;;;;;;;;;;GAiBG;AAEH;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CACtB,IAAc,EACd,KAAkB,EAClB,WAAoB;IAEpB,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;QAClB,KAAK,KAAK,CAAC;QACX,KAAK,IAAI,EAAE,CAAC;YACV,0FAA0F;YAC1F,0EAA0E;YAC1E,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC;YAC5E,OAAO,kBAAkB,CAAC,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;QAClD,CAAC;QACD,KAAK,KAAK,EAAE,CAAC;YACX,MAAM,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC;YAC5B,IAAI,OAAO,KAAK,SAAS,IAAI,IAAI,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO,SAA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AS,WAAW,CAAC,IAAmB;IACtC,OAAO,IAAI,CAAC,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;AAC1D,CAAC","sourcesContent":["import { parseExpr } from '@azx-pbc/axiom-compiler';\nimport { assertNever, isContextRootPath } from '@azx-pbc/axiom-runtime';\nimport type { CallNode, CompareNode, CompiledField, ExprNode } from '@azx-pbc/axiom-runtime';\nimport type { RecordIndex } from '@azx-pbc/axiom-runtime';\nimport type { AuthoringRule } from '../schema/document.js';\nimport type {\n FormatCheck,\n PresenceCheck,\n RuleExpression,\n ScalarVisualValue,\n TextMatchOperator,\n VisualExpr,\n VisualValue,\n} from './vocabulary.js';\nimport { isDateSpan, isFormatCheck, isPresenceCheck, isTextMatchOperator } from './vocabulary.js';\n\n/**\n * Recognition of a rule's parsed expression as one form of the visual vocabulary.\n *\n * The projection is the vocabulary's inverse and it is deliberately partial: a node outside the\n * vocabulary — an aggregate, a comprehension, arithmetic, a conditional — is not approximated, it\n * is left alone. `projectRule` states that outcome as the rule's advanced form, carrying the\n * source the author stored byte for byte, which is what makes a visual edit unable to rewrite an\n * advanced formula.\n *\n * Nothing is normalised on the way through. A nested connective keeps its nesting, `not(not(x))`\n * stays two negations, a two-argument `is_unique` stays a member test, and a date window keeps\n * the operand order and the sign the author wrote. The one question the declarations answer is\n * whether a path is declared: a visual rule reads declarations, never an unbound name.\n *\n * A `validate` rule is shown as what makes the rule valid — \"Valid when …\". The projection never\n * negates and no inversion mode exists, so an author's \"Error when …\" sentence can never become a\n * silent `not(...)` that changes the rule's meaning.\n */\n\n/**\n * Recognises one parsed expression as a visual form, or reports that it is outside the vocabulary.\n *\n * `itemFieldId` is the array whose row `$it` names; it is stated only for an `item`-scoped rule,\n * so the same expression read outside that scope does not become a visual rule that claims to\n * address a row.\n */\nexport function toVisual(\n node: ExprNode,\n index: RecordIndex,\n itemFieldId?: string,\n): VisualExpr | undefined {\n switch (node.kind) {\n case 'and':\n case 'or': {\n // The printer has no source for a connective of fewer than two operands, so neither has a\n // visual form; every operand has to project for the connective itself to.\n const projected = node.args.map((arg) => toVisual(arg, index, itemFieldId));\n return connectiveOperands(projected, node.kind);\n }\n case 'not': {\n const [operand] = node.args;\n if (operand === undefined || node.args.length !== 1) return undefined;\n const projected = toVisual(operand, index, itemFieldId);\n return projected === undefined ? undefined : { kind: 'not', operand: projected };\n }\n case 'compare':\n return compareToVisual(node, index, itemFieldId);\n case 'call':\n return callToVisual(node, index, itemFieldId);\n case 'literal':\n case 'path':\n case 'list':\n case 'if':\n case 'coalesce':\n case 'comprehension':\n // A bare value is not a condition, and a conditional, a coalesce or a comprehension is not\n // one of the five leaf families: the rule is an advanced one.\n return undefined;\n default:\n return assertNever(node);\n }\n}\n\n/** The connective's operands, or `undefined` when one of them is outside the vocabulary. */\nfunction connectiveOperands(\n projected: readonly (VisualExpr | undefined)[],\n kind: 'and' | 'or',\n): VisualExpr | undefined {\n const operands: VisualExpr[] = [];\n for (const member of projected) {\n if (member === undefined) return undefined;\n operands.push(member);\n }\n const [first, second, ...rest] = operands;\n if (first === undefined || second === undefined) return undefined;\n return { kind, args: [first, second, ...rest] };\n}\n\n/**\n * The visual form of one comparison, preferring the count and date-window shapes it may spell.\n *\n * The order matters: a date window is a `compare` over a `days_between` call, and a count is one\n * over a `len` call, so the leaf comparison is the shape of last resort rather than a second\n * spelling of those two.\n */\nfunction compareToVisual(\n node: CompareNode,\n index: RecordIndex,\n itemFieldId: string | undefined,\n): VisualExpr | undefined {\n const count = countToVisual(node, index, itemFieldId);\n if (count !== undefined) return count;\n const window = dateWindowToVisual(node, index, itemFieldId);\n if (window !== undefined) return window;\n const [left, right] = node.args;\n if (left === undefined || right === undefined) return undefined;\n const leftValue = valueToVisual(left, index, itemFieldId);\n if (leftValue === undefined) return undefined;\n const rightValue = valueToVisual(right, index, itemFieldId);\n if (rightValue === undefined) return undefined;\n if (node.op === 'in') {\n // Membership is a list on the right and a scalar on the left; any other pairing is a tree\n // the vocabulary does not state, so the rule opens advanced rather than as a wrong shape.\n const leftScalar = scalarOf(leftValue);\n return leftScalar === undefined || rightValue.kind !== 'list'\n ? undefined\n : { kind: 'membership', left: leftScalar, candidates: rightValue };\n }\n const leftScalar = scalarOf(leftValue);\n const rightScalar = scalarOf(rightValue);\n return leftScalar === undefined || rightScalar === undefined\n ? undefined\n : { kind: 'comparison', op: node.op, left: leftScalar, right: rightScalar };\n}\n\n/** `len(path) <op> whole` when the length is counted and the bound is a whole number. */\nfunction countToVisual(\n node: CompareNode,\n index: RecordIndex,\n itemFieldId: string | undefined,\n): VisualExpr | undefined {\n if (node.op === 'in') return undefined;\n const [left, right] = node.args;\n if (left?.kind !== 'call' || left.op !== 'len' || left.args.length !== 1) return undefined;\n const [counted] = left.args;\n if (counted?.kind !== 'path' || !isDeclaredPath(counted.path, index, itemFieldId)) {\n return undefined;\n }\n const count = right === undefined ? undefined : valueToVisual(right, index, itemFieldId);\n if (count === undefined || !isIntegerOperand(count, index, itemFieldId)) return undefined;\n return { kind: 'count', path: counted, op: node.op, count };\n}\n\n/** `days_between(a, b) <op> whole` over dates, with the operands in the author's order. */\nfunction dateWindowToVisual(\n node: CompareNode,\n index: RecordIndex,\n itemFieldId: string | undefined,\n): VisualExpr | undefined {\n if (node.op === 'in') return undefined;\n const [left, right] = node.args;\n if (left?.kind !== 'call' || !isDateSpan(left.op) || left.args.length !== 2) return undefined;\n const [from, to] = left.args;\n const amount = right === undefined ? undefined : valueToVisual(right, index, itemFieldId);\n if (from === undefined || to === undefined || amount === undefined) return undefined;\n const fromValue = valueToVisual(from, index, itemFieldId);\n const toValue = valueToVisual(to, index, itemFieldId);\n const fromScalar = fromValue === undefined ? undefined : scalarOf(fromValue);\n const toScalar = toValue === undefined ? undefined : scalarOf(toValue);\n if (\n fromScalar === undefined ||\n toScalar === undefined ||\n !isDateOperand(fromScalar, index, itemFieldId) ||\n !isDateOperand(toScalar, index, itemFieldId) ||\n !isIntegerOperand(amount, index, itemFieldId)\n ) {\n return undefined;\n }\n return {\n kind: 'date_window',\n span: left.op,\n op: node.op,\n from: fromScalar,\n to: toScalar,\n amount,\n };\n}\n\n/** The visual form of one call node: a check, a text match, a uniqueness test or a capability. */\nfunction callToVisual(\n node: CallNode,\n index: RecordIndex,\n itemFieldId: string | undefined,\n): VisualExpr | undefined {\n if (isPresenceCheck(node.op)) return presenceToVisual(node.op, node.args, index, itemFieldId);\n if (isFormatCheck(node.op)) return formatToVisual(node.op, node.args, index, itemFieldId);\n if (isTextMatchOperator(node.op)) {\n return textMatchToVisual(node.op, node.args, index, itemFieldId);\n }\n switch (node.op) {\n case 'is_unique': {\n const [arrayNode, memberNode] = node.args;\n if (arrayNode?.kind !== 'path') return undefined;\n const array = declaredFieldPath(arrayNode.path, index, itemFieldId);\n if (array?.primitive !== 'array') return undefined;\n if (node.args.length === 1) return { kind: 'unique_value', array: arrayNode };\n if (memberNode?.kind !== 'path' || node.args.length !== 2) return undefined;\n return arrayMember(array, memberNode.path) === undefined\n ? undefined\n : { kind: 'unique_member', array: arrayNode, member: memberNode };\n }\n case 'has_cap': {\n const [cap] = node.args;\n if (cap?.kind !== 'literal' || cap.domain !== 'string' || typeof cap.value !== 'string') {\n return undefined;\n }\n return node.args.length === 1 ? { kind: 'capability', cap: cap.value } : undefined;\n }\n default:\n // Every other built-in — an aggregate, arithmetic, a lookup, a digest — is advanced.\n return undefined;\n }\n}\n\n/** `<check>(path)`, which the vocabulary states only over a declared path. */\nfunction presenceToVisual(\n check: PresenceCheck,\n args: readonly ExprNode[],\n index: RecordIndex,\n itemFieldId: string | undefined,\n): VisualExpr | undefined {\n const [operand] = args;\n if (operand?.kind !== 'path' || args.length !== 1) return undefined;\n return isDeclaredPath(operand.path, index, itemFieldId)\n ? { kind: 'check', check, path: operand }\n : undefined;\n}\n\n/** `is_email(v)` and its siblings, which the vocabulary states only over a string operand. */\nfunction formatToVisual(\n check: FormatCheck,\n args: readonly ExprNode[],\n index: RecordIndex,\n itemFieldId: string | undefined,\n): VisualExpr | undefined {\n const [operand] = args;\n const value = operand === undefined ? undefined : valueToVisual(operand, index, itemFieldId);\n const scalar = value === undefined ? undefined : scalarOf(value);\n if (scalar === undefined || args.length !== 1 || !isStringOperand(scalar, index, itemFieldId)) {\n return undefined;\n }\n return { kind: 'format_check', check, value: scalar };\n}\n\n/**\n * `contains(subject, text)` and its siblings: a subject and the text it is matched against.\n *\n * No operand-type check is stated here, unlike `is_email`: the language's own typing lets a text\n * match read a string or a list (`EXPR`, `contains` on `string | list`), so restricting the form to\n * strings would hide a rule the language states.\n */\nfunction textMatchToVisual(\n op: TextMatchOperator,\n args: readonly ExprNode[],\n index: RecordIndex,\n itemFieldId: string | undefined,\n): VisualExpr | undefined {\n const [subjectNode, textNode] = args;\n const subjectValue =\n subjectNode === undefined ? undefined : valueToVisual(subjectNode, index, itemFieldId);\n const textValue =\n textNode === undefined ? undefined : valueToVisual(textNode, index, itemFieldId);\n const subject = subjectValue === undefined ? undefined : scalarOf(subjectValue);\n const text = textValue === undefined ? undefined : scalarOf(textValue);\n if (subject === undefined || text === undefined || args.length !== 2) return undefined;\n return { kind: 'text_match', op, subject, text };\n}\n\n/** One AST node as a visual value, when it names a declared path, a literal or a list of them. */\nfunction valueToVisual(\n node: ExprNode,\n index: RecordIndex,\n itemFieldId: string | undefined,\n): VisualValue | undefined {\n switch (node.kind) {\n case 'literal':\n return node;\n case 'path':\n return isDeclaredPath(node.path, index, itemFieldId) ? node : undefined;\n case 'list': {\n const items: ScalarVisualValue[] = [];\n for (const item of node.items) {\n const value = valueToVisual(item, index, itemFieldId);\n const scalar = value === undefined ? undefined : scalarOf(value);\n if (scalar === undefined) return undefined;\n items.push(scalar);\n }\n return { kind: 'list', items };\n }\n default:\n return undefined;\n }\n}\n\n/** A value where a scalar is required, or `undefined` when it is a list. */\nfunction scalarOf(value: VisualValue): ScalarVisualValue | undefined {\n return value.kind === 'list' ? undefined : value;\n}\n\n/** Whether a path names a declared field or a declared context root. */\nfunction isDeclaredPath(\n path: string,\n index: RecordIndex,\n itemFieldId: string | undefined,\n): boolean {\n return (\n declaredFieldPath(path, index, itemFieldId) !== undefined ||\n contextPrimitive(path, index) !== undefined\n );\n}\n\n/**\n * The declared field a path names, or `undefined` when it names no field.\n *\n * A row member resolves against the array an `item`-scoped rule addresses, and only there: outside\n * that scope `$it` names nothing, so a rule written for one row cannot be read as a document path.\n */\nfunction declaredFieldPath(\n path: string,\n index: RecordIndex,\n itemFieldId: string | undefined,\n): CompiledField | undefined {\n const segments = path.split('.');\n const head = segments[0] ?? '';\n const rest = segments.slice(1);\n if (head === '$it') {\n if (itemFieldId === undefined || rest.length === 0) return undefined;\n const array = index.fieldsByPath.get(itemFieldId);\n return array === undefined ? undefined : descend(array, rest);\n }\n if (head.startsWith('$')) return undefined;\n if (head === 'context' && isContextRootPath(path, index)) return undefined;\n return index.fieldsByPath.get(path);\n}\n\n/**\n * The primitive a declared context root has, or `undefined` when the path names no root.\n *\n * Only a root the index carries resolves, which includes the reserved `record_date` and\n * `schema_version` the runtime always supplies; an undeclared `context.` path is not a value a\n * visual rule may claim to read.\n */\nfunction contextPrimitive(path: string, index: RecordIndex): string | undefined {\n const segments = path.split('.');\n if (segments[0] !== 'context' || segments.length < 2) return undefined;\n return index.contextRoots.get(segments.slice(1).join('.'))?.primitive;\n}\n\n/** The declared member a path reaches from a container, segment by segment. */\nfunction descend(field: CompiledField, segments: readonly string[]): CompiledField | undefined {\n let current: CompiledField | undefined = field;\n for (const segment of segments) {\n current = current?.fields?.find((member) => member.fieldId === segment);\n }\n return current;\n}\n\n/** The member an array declares by the relative path a uniqueness test names. */\nfunction arrayMember(array: CompiledField, path: string): CompiledField | undefined {\n return descend(array, path.split('.'));\n}\n\n/** The primitive one scalar value has: a path's declared domain, or the literal's own domain. */\nfunction primitiveOf(\n value: ScalarVisualValue,\n index: RecordIndex,\n itemFieldId: string | undefined,\n): string | undefined {\n return value.kind === 'literal'\n ? value.domain\n : (declaredFieldPath(value.path, index, itemFieldId)?.primitive ??\n contextPrimitive(value.path, index));\n}\n\nfunction isIntegerOperand(\n value: VisualValue,\n index: RecordIndex,\n itemFieldId: string | undefined,\n): value is ScalarVisualValue {\n return value.kind !== 'list' && primitiveOf(value, index, itemFieldId) === 'integer';\n}\n\nfunction isDateOperand(\n value: ScalarVisualValue,\n index: RecordIndex,\n itemFieldId: string | undefined,\n): boolean {\n return primitiveOf(value, index, itemFieldId) === 'date';\n}\n\nfunction isStringOperand(\n value: ScalarVisualValue,\n index: RecordIndex,\n itemFieldId: string | undefined,\n): boolean {\n return primitiveOf(value, index, itemFieldId) === 'string';\n}\n\n/**\n * The rule's expression as the form an editor shows.\n *\n * A `defaultCompute` writer, a rule whose source does not parse, or one that parses to a node\n * outside the vocabulary is the advanced arm holding the stored source unchanged. That is the\n * whole of the fallback: no reference is printed, no text is normalised, and no partial\n * projection is offered.\n */\nexport function projectRule(rule: AuthoringRule, index: RecordIndex): RuleExpression {\n if (rule.kind === 'defaultCompute') return { mode: 'advanced', source: rule.expr };\n const parsed = parseExpr(rule.expr);\n if (!parsed.ok) return { mode: 'advanced', source: rule.expr };\n const visual = toVisual(parsed.node, index, itemScopeOf(rule));\n return visual === undefined\n ? { mode: 'advanced', source: rule.expr }\n : { mode: 'visual', visual };\n}\n\n/** The array whose row an `item`-scoped rule addresses, and nothing for every other scope. */\nfunction itemScopeOf(rule: AuthoringRule): string | undefined {\n return rule.scope === 'item' ? rule.fieldId : undefined;\n}\n"]}
@@ -0,0 +1,50 @@
1
+ import type { RecordIndex } from '@azx-pbc/axiom-runtime';
2
+ import type { VisualExpr } from './vocabulary.js';
3
+ /**
4
+ * Whether one visual expression may be stored for one rule, and the source to store.
5
+ *
6
+ * A visual expression is only meaningful inside a document: its paths name declared fields or
7
+ * context roots, `$it` names a row only where the rule is item-scoped, and the form itself has to
8
+ * be one the projection recognises. `representVisual` decides that by projecting the emission
9
+ * back and comparing it with what it started from — the round trip is the check, so a hand-built
10
+ * visual cannot store source the projection would read differently.
11
+ *
12
+ * The comparison is structural, through the runtime's canonicaliser, for the same reason the
13
+ * printer's self-check compares canonical text: object member order is not part of a tree, and
14
+ * one canonical form is already the repository's definition of two trees being equal.
15
+ *
16
+ * A visual that is in scope can still have no source form — a path segment that is an Expr v1
17
+ * reserved word, or a tree past the expression budget — and that is the printer's refusal, not a
18
+ * second check here.
19
+ *
20
+ * The visual is a value the type states: its literals are canonical JSON, which is what the store's
21
+ * boundary read proves before a command reaches this function. A caller that builds one past the
22
+ * type is outside the contract, and the canonical comparison reports the canonicaliser's own
23
+ * refusal as `non_canonical` rather than throwing through the public door. Only that refusal is
24
+ * translated: a value thrown by anything else — a throwing accessor, a broken invariant — is a
25
+ * fault in this process and propagates unchanged, so it is never blamed on the author's form.
26
+ */
27
+ /** Why a visual expression cannot be stored for a rule. */
28
+ export type VisualRefusalReason =
29
+ /** The form reads a path this document declares nothing for, at this rule's scope. */
30
+ 'out_of_scope'
31
+ /** The value is outside canonical JSON, so two forms cannot be compared through it. */
32
+ | 'non_canonical'
33
+ /** The emitted tree has no Expr v1 source form, and the printer states why. */
34
+ | 'unspellable';
35
+ /** One refused visual, with the reason and the detail a host reports. */
36
+ export interface VisualRefusal {
37
+ readonly reason: VisualRefusalReason;
38
+ readonly detail: string;
39
+ }
40
+ /** The source a rule stores for its visual form, or the refusal that it stores none. */
41
+ export type VisualRepresentation = {
42
+ readonly ok: true;
43
+ readonly source: string;
44
+ } | {
45
+ readonly ok: false;
46
+ readonly refusal: VisualRefusal;
47
+ };
48
+ /** Stores one visual expression for a rule, or states why it cannot be stored. */
49
+ export declare function representVisual(visual: VisualExpr, index: RecordIndex, itemFieldId: string | undefined): VisualRepresentation;
50
+ //# sourceMappingURL=represent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"represent.d.ts","sourceRoot":"","sources":["../../src/visual/represent.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAG1D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,2DAA2D;AAC3D,MAAM,MAAM,mBAAmB;AAC7B,sFAAsF;AACpF,cAAc;AAChB,uFAAuF;GACrF,eAAe;AACjB,+EAA+E;GAC7E,aAAa,CAAC;AAElB,yEAAyE;AACzE,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,MAAM,EAAE,mBAAmB,CAAC;IACrC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,wFAAwF;AACxF,MAAM,MAAM,oBAAoB,GAC5B;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAC9C;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAA;CAAE,CAAC;AAsC5D,kFAAkF;AAClF,wBAAgB,eAAe,CAC7B,MAAM,EAAE,UAAU,EAClB,KAAK,EAAE,WAAW,EAClB,WAAW,EAAE,MAAM,GAAG,SAAS,GAC9B,oBAAoB,CAwBtB"}
@@ -0,0 +1,66 @@
1
+ import { AxiomError, canonicalizeRestricted } from '@azx-pbc/axiom-runtime';
2
+ import { emitVisual, visualSource } from './emit.js';
3
+ import { toVisual } from './project.js';
4
+ function outOfScope() {
5
+ return {
6
+ ok: false,
7
+ refusal: {
8
+ reason: 'out_of_scope',
9
+ detail: 'the form is not one this draft reads back at this rule’s scope: name a declared field or context root, write a row member through `$it` only on an item-scoped rule, and give a count, window or format check an operand of the type it measures',
10
+ },
11
+ };
12
+ }
13
+ /** Why a caller's value has no canonical form to compare a projection against. */
14
+ function nonCanonical(error) {
15
+ return {
16
+ ok: false,
17
+ refusal: {
18
+ reason: 'non_canonical',
19
+ detail: `the form is not canonical JSON: ${error.message}`,
20
+ },
21
+ };
22
+ }
23
+ /**
24
+ * Whether two visual values state the same tree, through one canonical spelling.
25
+ *
26
+ * One side is the caller's value; the other is the projection read back from that value's
27
+ * emission. The restricted profile is closed, so a value the type admits but JSON cannot carry —
28
+ * an `undefined` member, a prototype, a negative zero — is a refusal here rather than an exception
29
+ * out of the public door. The refusal is the canonicaliser's `AxiomError`; the class is the
30
+ * boundary because every profile refusal is that class and the runtime states no narrower
31
+ * predicate for one, so anything else thrown here is this process's defect and is rethrown.
32
+ */
33
+ function sameTree(left, right) {
34
+ return canonicalizeRestricted(left) === canonicalizeRestricted(right);
35
+ }
36
+ /** Stores one visual expression for a rule, or states why it cannot be stored. */
37
+ export function representVisual(visual, index, itemFieldId) {
38
+ // The printer's self-check is also the canonical-profile and expression-budget read of the
39
+ // emitted tree. It runs before projection and before canonicalising the editor's value, so an
40
+ // over-budget tree, a lone surrogate or negative zero is a refusal rather than an exception.
41
+ const source = visualSource(visual);
42
+ if (!source.ok) {
43
+ return {
44
+ ok: false,
45
+ refusal: { reason: 'unspellable', detail: `${source.reason}: ${source.detail}` },
46
+ };
47
+ }
48
+ const emitted = emitVisual(visual);
49
+ const projected = toVisual(emitted, index, itemFieldId);
50
+ if (projected === undefined)
51
+ return outOfScope();
52
+ try {
53
+ if (!sameTree(projected, visual))
54
+ return outOfScope();
55
+ }
56
+ catch (error) {
57
+ // `AxiomError` is the canonicaliser's refusal, and the only throw this catch translates.
58
+ // Anything else is this process's defect — a getter that throws, a broken invariant — and
59
+ // must stay visible instead of being reported as the author's mistake.
60
+ if (!(error instanceof AxiomError))
61
+ throw error;
62
+ return nonCanonical(error);
63
+ }
64
+ return { ok: true, source: source.source };
65
+ }
66
+ //# sourceMappingURL=represent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"represent.js","sourceRoot":"","sources":["../../src/visual/represent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAE5E,OAAO,EAAE,UAAU,EAAE,YAA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AAE,MAAM,CAAC;YAAE,OAAO,UAAU,EAAE,CAAC;IACxD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,yFAAyF;QACzF,0FAA0F;QAC1F,uEAAuE;QACvE,IAAI,CAAC,CAAC,KAAK,YAAY,UAAU,CAAC;YAAE,MAAM,KAAK,CAAC;QAChD,OAAO,YAAY,CAAC,KAAK,CAAC,CAAC;IAC7B,CAAC;IACD,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC;AAC7C,CAAC","sourcesContent":["import { AxiomError, canonicalizeRestricted } from '@azx-pbc/axiom-runtime';\nimport type { RecordIndex } from '@azx-pbc/axiom-runtime';\nimport { emitVisual, visualSource } from './emit.js';\nimport { toVisual } from './project.js';\nimport type { VisualExpr } from './vocabulary.js';\n\n/**\n * Whether one visual expression may be stored for one rule, and the source to store.\n *\n * A visual expression is only meaningful inside a document: its paths name declared fields or\n * context roots, `$it` names a row only where the rule is item-scoped, and the form itself has to\n * be one the projection recognises. `representVisual` decides that by projecting the emission\n * back and comparing it with what it started from — the round trip is the check, so a hand-built\n * visual cannot store source the projection would read differently.\n *\n * The comparison is structural, through the runtime's canonicaliser, for the same reason the\n * printer's self-check compares canonical text: object member order is not part of a tree, and\n * one canonical form is already the repository's definition of two trees being equal.\n *\n * A visual that is in scope can still have no source form — a path segment that is an Expr v1\n * reserved word, or a tree past the expression budget — and that is the printer's refusal, not a\n * second check here.\n *\n * The visual is a value the type states: its literals are canonical JSON, which is what the store's\n * boundary read proves before a command reaches this function. A caller that builds one past the\n * type is outside the contract, and the canonical comparison reports the canonicaliser's own\n * refusal as `non_canonical` rather than throwing through the public door. Only that refusal is\n * translated: a value thrown by anything else — a throwing accessor, a broken invariant — is a\n * fault in this process and propagates unchanged, so it is never blamed on the author's form.\n */\n\n/** Why a visual expression cannot be stored for a rule. */\nexport type VisualRefusalReason =\n /** The form reads a path this document declares nothing for, at this rule's scope. */\n | 'out_of_scope'\n /** The value is outside canonical JSON, so two forms cannot be compared through it. */\n | 'non_canonical'\n /** The emitted tree has no Expr v1 source form, and the printer states why. */\n | 'unspellable';\n\n/** One refused visual, with the reason and the detail a host reports. */\nexport interface VisualRefusal {\n readonly reason: VisualRefusalReason;\n readonly detail: string;\n}\n\n/** The source a rule stores for its visual form, or the refusal that it stores none. */\nexport type VisualRepresentation =\n | { readonly ok: true; readonly source: string }\n | { readonly ok: false; readonly refusal: VisualRefusal };\n\nfunction outOfScope(): VisualRepresentation {\n return {\n ok: false,\n refusal: {\n reason: 'out_of_scope',\n detail:\n 'the form is not one this draft reads back at this rule’s scope: name a declared field or context root, write a row member through `$it` only on an item-scoped rule, and give a count, window or format check an operand of the type it measures',\n },\n };\n}\n\n/** Why a caller's value has no canonical form to compare a projection against. */\nfunction nonCanonical(error: AxiomError): VisualRepresentation {\n return {\n ok: false,\n refusal: {\n reason: 'non_canonical',\n detail: `the form is not canonical JSON: ${error.message}`,\n },\n };\n}\n\n/**\n * Whether two visual values state the same tree, through one canonical spelling.\n *\n * One side is the caller's value; the other is the projection read back from that value's\n * emission. The restricted profile is closed, so a value the type admits but JSON cannot carry —\n * an `undefined` member, a prototype, a negative zero — is a refusal here rather than an exception\n * out of the public door. The refusal is the canonicaliser's `AxiomError`; the class is the\n * boundary because every profile refusal is that class and the runtime states no narrower\n * predicate for one, so anything else thrown here is this process's defect and is rethrown.\n */\nfunction sameTree(left: VisualExpr, right: VisualExpr): boolean {\n return canonicalizeRestricted(left) === canonicalizeRestricted(right);\n}\n\n/** Stores one visual expression for a rule, or states why it cannot be stored. */\nexport function representVisual(\n visual: VisualExpr,\n index: RecordIndex,\n itemFieldId: string | undefined,\n): VisualRepresentation {\n // The printer's self-check is also the canonical-profile and expression-budget read of the\n // emitted tree. It runs before projection and before canonicalising the editor's value, so an\n // over-budget tree, a lone surrogate or negative zero is a refusal rather than an exception.\n const source = visualSource(visual);\n if (!source.ok) {\n return {\n ok: false,\n refusal: { reason: 'unspellable', detail: `${source.reason}: ${source.detail}` },\n };\n }\n const emitted = emitVisual(visual);\n const projected = toVisual(emitted, index, itemFieldId);\n if (projected === undefined) return outOfScope();\n try {\n if (!sameTree(projected, visual)) return outOfScope();\n } catch (error) {\n // `AxiomError` is the canonicaliser's refusal, and the only throw this catch translates.\n // Anything else is this process's defect — a getter that throws, a broken invariant — and\n // must stay visible instead of being reported as the author's mistake.\n if (!(error instanceof AxiomError)) throw error;\n return nonCanonical(error);\n }\n return { ok: true, source: source.source };\n}\n"]}