@fncts/schema 0.0.16 → 0.0.17

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 (151) hide show
  1. package/AST.d.ts +19 -6
  2. package/ASTAnnotation.d.ts +10 -2
  3. package/ParseError/ParseError.d.ts +231 -0
  4. package/ParseError/ParseErrorFormatter.d.ts +2 -0
  5. package/ParseError/PathFormatter.d.ts +13 -0
  6. package/ParseError/TreeFormatter.d.ts +14 -0
  7. package/ParseError.d.ts +4 -144
  8. package/ParseResult.d.ts +1 -8
  9. package/Parser/interpreter.d.ts +0 -1
  10. package/Schema/api/conc.d.ts +0 -2
  11. package/Schema/api/hashMap.d.ts +0 -1
  12. package/Schema/api/hashSet.d.ts +0 -1
  13. package/Schema/api/immutableArray.d.ts +0 -1
  14. package/Schema/api/list.d.ts +1 -2
  15. package/Schema/api/map.d.ts +19 -0
  16. package/Schema/api/set.d.ts +19 -0
  17. package/Schema/api.d.ts +11 -1
  18. package/Schema.d.ts +2 -0
  19. package/Show.d.ts +7 -3
  20. package/_cjs/AST.cjs +180 -113
  21. package/_cjs/AST.cjs.map +1 -1
  22. package/_cjs/ASTAnnotation.cjs +6 -1
  23. package/_cjs/ASTAnnotation.cjs.map +1 -1
  24. package/_cjs/Gen.cjs +24 -24
  25. package/_cjs/Gen.cjs.map +1 -1
  26. package/_cjs/ParseError/ParseError.cjs +260 -0
  27. package/_cjs/ParseError/ParseError.cjs.map +1 -0
  28. package/_cjs/ParseError/ParseErrorFormatter.cjs +6 -0
  29. package/_cjs/ParseError/ParseErrorFormatter.cjs.map +1 -0
  30. package/_cjs/ParseError/PathFormatter.cjs +94 -0
  31. package/_cjs/ParseError/PathFormatter.cjs.map +1 -0
  32. package/_cjs/ParseError/TreeFormatter.cjs +123 -0
  33. package/_cjs/ParseError/TreeFormatter.cjs.map +1 -0
  34. package/_cjs/ParseError.cjs +43 -289
  35. package/_cjs/ParseError.cjs.map +1 -1
  36. package/_cjs/ParseResult.cjs +1 -10
  37. package/_cjs/ParseResult.cjs.map +1 -1
  38. package/_cjs/Parser/api.cjs +2 -2
  39. package/_cjs/Parser/api.cjs.map +1 -1
  40. package/_cjs/Parser/definition.cjs +1 -1
  41. package/_cjs/Parser/interpreter.cjs +121 -117
  42. package/_cjs/Parser/interpreter.cjs.map +1 -1
  43. package/_cjs/Schema/api/conc.cjs +35 -45
  44. package/_cjs/Schema/api/conc.cjs.map +1 -1
  45. package/_cjs/Schema/api/either.cjs +24 -30
  46. package/_cjs/Schema/api/either.cjs.map +1 -1
  47. package/_cjs/Schema/api/hashMap.cjs +41 -101
  48. package/_cjs/Schema/api/hashMap.cjs.map +1 -1
  49. package/_cjs/Schema/api/hashSet.cjs +46 -106
  50. package/_cjs/Schema/api/hashSet.cjs.map +1 -1
  51. package/_cjs/Schema/api/immutableArray.cjs +22 -48
  52. package/_cjs/Schema/api/immutableArray.cjs.map +1 -1
  53. package/_cjs/Schema/api/list.cjs +35 -52
  54. package/_cjs/Schema/api/list.cjs.map +1 -1
  55. package/_cjs/Schema/api/map.cjs +97 -0
  56. package/_cjs/Schema/api/map.cjs.map +1 -0
  57. package/_cjs/Schema/api/maybe.cjs +24 -35
  58. package/_cjs/Schema/api/maybe.cjs.map +1 -1
  59. package/_cjs/Schema/api/set.cjs +76 -0
  60. package/_cjs/Schema/api/set.cjs.map +1 -0
  61. package/_cjs/Schema/api.cjs +20 -2
  62. package/_cjs/Schema/api.cjs.map +1 -1
  63. package/_cjs/Schema.cjs +22 -0
  64. package/_cjs/Schema.cjs.map +1 -1
  65. package/_cjs/Show.cjs +106 -89
  66. package/_cjs/Show.cjs.map +1 -1
  67. package/_cjs/utils.cjs +5 -0
  68. package/_cjs/utils.cjs.map +1 -1
  69. package/_mjs/AST.mjs +177 -112
  70. package/_mjs/AST.mjs.map +1 -1
  71. package/_mjs/ASTAnnotation.mjs +5 -0
  72. package/_mjs/ASTAnnotation.mjs.map +1 -1
  73. package/_mjs/Gen.mjs +24 -24
  74. package/_mjs/Gen.mjs.map +1 -1
  75. package/_mjs/ParseError/ParseError.mjs +228 -0
  76. package/_mjs/ParseError/ParseError.mjs.map +1 -0
  77. package/_mjs/ParseError/ParseErrorFormatter.mjs +2 -0
  78. package/_mjs/ParseError/ParseErrorFormatter.mjs.map +1 -0
  79. package/_mjs/ParseError/PathFormatter.mjs +86 -0
  80. package/_mjs/ParseError/PathFormatter.mjs.map +1 -0
  81. package/_mjs/ParseError/TreeFormatter.mjs +113 -0
  82. package/_mjs/ParseError/TreeFormatter.mjs.map +1 -0
  83. package/_mjs/ParseError.mjs +6 -270
  84. package/_mjs/ParseError.mjs.map +1 -1
  85. package/_mjs/ParseResult.mjs +1 -9
  86. package/_mjs/ParseResult.mjs.map +1 -1
  87. package/_mjs/Parser/api.mjs +2 -2
  88. package/_mjs/Parser/api.mjs.map +1 -1
  89. package/_mjs/Parser/definition.mjs +1 -1
  90. package/_mjs/Parser/interpreter.mjs +121 -117
  91. package/_mjs/Parser/interpreter.mjs.map +1 -1
  92. package/_mjs/Schema/api/conc.mjs +35 -44
  93. package/_mjs/Schema/api/conc.mjs.map +1 -1
  94. package/_mjs/Schema/api/either.mjs +24 -30
  95. package/_mjs/Schema/api/either.mjs.map +1 -1
  96. package/_mjs/Schema/api/hashMap.mjs +41 -101
  97. package/_mjs/Schema/api/hashMap.mjs.map +1 -1
  98. package/_mjs/Schema/api/hashSet.mjs +46 -106
  99. package/_mjs/Schema/api/hashSet.mjs.map +1 -1
  100. package/_mjs/Schema/api/immutableArray.mjs +23 -49
  101. package/_mjs/Schema/api/immutableArray.mjs.map +1 -1
  102. package/_mjs/Schema/api/list.mjs +35 -52
  103. package/_mjs/Schema/api/list.mjs.map +1 -1
  104. package/_mjs/Schema/api/map.mjs +88 -0
  105. package/_mjs/Schema/api/map.mjs.map +1 -0
  106. package/_mjs/Schema/api/maybe.mjs +24 -35
  107. package/_mjs/Schema/api/maybe.mjs.map +1 -1
  108. package/_mjs/Schema/api/set.mjs +67 -0
  109. package/_mjs/Schema/api/set.mjs.map +1 -0
  110. package/_mjs/Schema/api.mjs +18 -2
  111. package/_mjs/Schema/api.mjs.map +1 -1
  112. package/_mjs/Schema.mjs +2 -0
  113. package/_mjs/Schema.mjs.map +1 -1
  114. package/_mjs/Show.mjs +106 -90
  115. package/_mjs/Show.mjs.map +1 -1
  116. package/_mjs/utils.mjs +4 -0
  117. package/_mjs/utils.mjs.map +1 -1
  118. package/_src/AST.ts +144 -43
  119. package/_src/ASTAnnotation.ts +8 -1
  120. package/_src/Gen.ts +12 -9
  121. package/_src/ParseError/ParseError.ts +304 -0
  122. package/_src/ParseError/ParseErrorFormatter.ts +1 -0
  123. package/_src/ParseError/PathFormatter.ts +117 -0
  124. package/_src/ParseError/TreeFormatter.ts +127 -0
  125. package/_src/ParseError.ts +7 -331
  126. package/_src/ParseResult.ts +2 -9
  127. package/_src/Parser/api.ts +1 -1
  128. package/_src/Parser/interpreter.ts +98 -75
  129. package/_src/Schema/api/conc.ts +33 -42
  130. package/_src/Schema/api/either.ts +20 -30
  131. package/_src/Schema/api/hashMap.ts +40 -124
  132. package/_src/Schema/api/hashSet.ts +31 -117
  133. package/_src/Schema/api/immutableArray.ts +15 -45
  134. package/_src/Schema/api/list.ts +32 -55
  135. package/_src/Schema/api/map.ts +93 -0
  136. package/_src/Schema/api/maybe.ts +19 -34
  137. package/_src/Schema/api/set.ts +74 -0
  138. package/_src/Schema/api.ts +20 -2
  139. package/_src/Schema.ts +2 -0
  140. package/_src/Show.ts +156 -128
  141. package/_src/global.ts +0 -4
  142. package/_src/utils.ts +5 -0
  143. package/global.d.ts +0 -4
  144. package/package.json +2 -2
  145. package/utils.d.ts +1 -0
  146. package/ParseFailure.d.ts +0 -18
  147. package/_cjs/ParseFailure.cjs +0 -28
  148. package/_cjs/ParseFailure.cjs.map +0 -1
  149. package/_mjs/ParseFailure.mjs +0 -20
  150. package/_mjs/ParseFailure.mjs.map +0 -1
  151. package/_src/ParseFailure.ts +0 -18
@@ -0,0 +1,304 @@
1
+ import type { Declaration, Refinement, Transform, Tuple, TypeLiteral, Union } from "@fncts/schema/AST";
2
+
3
+ export const enum ParseErrorTag {
4
+ Declaration,
5
+ Type,
6
+ Index,
7
+ Key,
8
+ Missing,
9
+ Unexpected,
10
+ UnionMember,
11
+ Refinement,
12
+ Transformation,
13
+ TypeLiteral,
14
+ Tuple,
15
+ Union,
16
+ Iterable,
17
+ }
18
+
19
+ /**
20
+ * @tsplus type fncts.schema.ParseError
21
+ * @tsplus companion fncts.schema.ParseErrorOps
22
+ */
23
+ export type ParseError =
24
+ | DeclarationError
25
+ | TypeError
26
+ | RefinementError
27
+ | TransformationError
28
+ | TypeLiteralError
29
+ | TupleError
30
+ | UnionError
31
+ | IterableError;
32
+
33
+ /**
34
+ * @tsplus companion fncts.schema.ParseError.DeclarationError
35
+ */
36
+ export class DeclarationError {
37
+ readonly _tag = ParseErrorTag.Declaration;
38
+ constructor(
39
+ readonly ast: Declaration,
40
+ readonly actual: unknown,
41
+ readonly error: ParseError,
42
+ ) {}
43
+ }
44
+
45
+ /**
46
+ * @tsplus static fncts.schema.ParseError.DeclarationError __call
47
+ * @tsplus static fncts.schema.ParseErrorOps DeclarationError
48
+ */
49
+ export function declarationError(ast: Declaration, actual: unknown, error: ParseError): DeclarationError {
50
+ return new DeclarationError(ast, actual, error);
51
+ }
52
+
53
+ /**
54
+ * @tsplus companion fncts.schema.ParseError.TypeError
55
+ */
56
+ export class TypeError {
57
+ readonly _tag = ParseErrorTag.Type;
58
+ constructor(
59
+ readonly ast: AST,
60
+ readonly actual: unknown,
61
+ ) {}
62
+ }
63
+
64
+ /**
65
+ * @tsplus static fncts.schema.ParseError.TypeError __call
66
+ * @tsplus static fncts.schema.ParseErrorOps TypeError
67
+ */
68
+ export function typeError(expected: AST, actual: unknown): TypeError {
69
+ return new TypeError(expected, actual);
70
+ }
71
+
72
+ /**
73
+ * @tsplus companion fncts.schema.ParseError.TypeLiteralError
74
+ */
75
+ export class TypeLiteralError {
76
+ readonly _tag = ParseErrorTag.TypeLiteral;
77
+ constructor(
78
+ readonly ast: TypeLiteral,
79
+ readonly actual: unknown,
80
+ readonly errors: Vector<KeyError>,
81
+ readonly output: { readonly [x: string]: unknown } = {},
82
+ ) {}
83
+ }
84
+
85
+ /**
86
+ * @tsplus static fncts.schema.ParseError.TypeLiteralError __call
87
+ * @tsplus static fncts.schema.ParseErrorOps TypeLiteralError
88
+ */
89
+ export function typeLiteralError(
90
+ ast: TypeLiteral,
91
+ actual: unknown,
92
+ errors: Vector<KeyError>,
93
+ output: { readonly [x: string]: unknown } = {},
94
+ ): TypeLiteralError {
95
+ return new TypeLiteralError(ast, actual, errors, output);
96
+ }
97
+
98
+ /**
99
+ * @tsplus companion fncts.schema.ParseError.TupleError
100
+ */
101
+ export class TupleError {
102
+ readonly _tag = ParseErrorTag.Tuple;
103
+ constructor(
104
+ readonly ast: Tuple,
105
+ readonly actual: unknown,
106
+ readonly errors: Vector<IndexError>,
107
+ readonly output: ReadonlyArray<unknown> = [],
108
+ ) {}
109
+ }
110
+
111
+ /**
112
+ * @tsplus static fncts.schema.ParseError.TupleError __call
113
+ * @tsplus static fncts.schema.ParseErrorOps TupleError
114
+ */
115
+ export function tupleError(
116
+ ast: Tuple,
117
+ actual: unknown,
118
+ errors: Vector<IndexError>,
119
+ output: ReadonlyArray<unknown> = [],
120
+ ): TupleError {
121
+ return new TupleError(ast, actual, errors, output);
122
+ }
123
+
124
+ /**
125
+ * @tsplus companion fncts.schema.ParseError.IndexError
126
+ */
127
+ export class IndexError {
128
+ readonly _tag = ParseErrorTag.Index;
129
+ constructor(
130
+ readonly index: number,
131
+ readonly error: ParseError | MissingError | UnexpectedError,
132
+ ) {}
133
+ }
134
+
135
+ /**
136
+ * @tsplus static fncts.schema.ParseError.IndexError __call
137
+ * @tsplus static fncts.schema.ParseErrorOps IndexError
138
+ */
139
+ export function indexError(index: number, error: ParseError | MissingError | UnexpectedError): IndexError {
140
+ return new IndexError(index, error);
141
+ }
142
+
143
+ /**
144
+ * @tsplus companion fncts.schema.ParseError.KeyError
145
+ */
146
+ export class KeyError {
147
+ readonly _tag = ParseErrorTag.Key;
148
+ constructor(
149
+ readonly keyAST: AST,
150
+ readonly key: any,
151
+ readonly error: ParseError | MissingError | UnexpectedError,
152
+ ) {}
153
+ }
154
+
155
+ /**
156
+ * @tsplus static fncts.schema.ParseError.KeyError __call
157
+ * @tsplus static fncts.schema.ParseErrorOps KeyError
158
+ */
159
+ export function keyError(keyAST: AST, key: any, error: ParseError | MissingError | UnexpectedError): KeyError {
160
+ return new KeyError(keyAST, key, error);
161
+ }
162
+
163
+ /**
164
+ * @tsplus companion fncts.schema.ParseError.MissingError
165
+ */
166
+ export class MissingError {
167
+ readonly _tag = ParseErrorTag.Missing;
168
+ }
169
+
170
+ /**
171
+ * @tsplus static fncts.schema.ParseErrorOps MissingError
172
+ */
173
+ export const missingError = new MissingError();
174
+
175
+ /**
176
+ * @tsplus companion fncts.schema.ParseError.UnexpectedError
177
+ */
178
+ export class UnexpectedError {
179
+ readonly _tag = ParseErrorTag.Unexpected;
180
+ constructor(readonly actual: unknown) {}
181
+ }
182
+
183
+ /**
184
+ * @tsplus static fncts.schema.ParseError.UnexpectedError __call
185
+ * @tsplus static fncts.schema.ParseErrorOps UnexpectedError
186
+ */
187
+ export function unexpectedError(actual: unknown): UnexpectedError {
188
+ return new UnexpectedError(actual);
189
+ }
190
+
191
+ /**
192
+ * @tsplus companion fncts.schema.ParseError.UnionError
193
+ */
194
+ export class UnionError {
195
+ readonly _tag = ParseErrorTag.Union;
196
+ constructor(
197
+ readonly ast: Union,
198
+ readonly actual: unknown,
199
+ readonly errors: Vector<TypeError | TypeLiteralError | UnionMemberError>,
200
+ ) {}
201
+ }
202
+
203
+ /**
204
+ * @tsplus static fncts.schema.ParseError.UnionError __call
205
+ * @tsplus static fncts.schema.ParseErrorOps UnionError
206
+ */
207
+ export function unionError(
208
+ ast: Union,
209
+ actual: unknown,
210
+ errors: Vector<TypeError | TypeLiteralError | UnionMemberError>,
211
+ ): UnionError {
212
+ return new UnionError(ast, actual, errors);
213
+ }
214
+
215
+ /**
216
+ * @tsplus companion fncts.schema.ParseError.UnionMemberError
217
+ */
218
+ export class UnionMemberError {
219
+ readonly _tag = ParseErrorTag.UnionMember;
220
+ constructor(
221
+ readonly ast: AST,
222
+ readonly error: ParseError,
223
+ ) {}
224
+ }
225
+
226
+ /**
227
+ * @tsplus static fncts.schema.ParseError.UnionMemberError __call
228
+ * @tsplus static fncts.schema.ParseErrorOps UnionMemberError
229
+ */
230
+ export function unionMemberError(ast: AST, error: ParseError): UnionMemberError {
231
+ return new UnionMemberError(ast, error);
232
+ }
233
+
234
+ /**
235
+ * @tsplus companion fncts.schema.ParseError.RefinementError
236
+ */
237
+ export class RefinementError {
238
+ readonly _tag = ParseErrorTag.Refinement;
239
+ constructor(
240
+ readonly ast: Refinement,
241
+ readonly actual: unknown,
242
+ readonly kind: "From" | "Predicate",
243
+ readonly error: ParseError,
244
+ ) {}
245
+ }
246
+
247
+ /**
248
+ * @tsplus static fncts.schema.ParseError.RefinementError __call
249
+ * @tsplus static fncts.schema.ParseErrorOps RefinementError
250
+ */
251
+ export function refinementError(
252
+ ast: Refinement,
253
+ actual: unknown,
254
+ kind: "From" | "Predicate",
255
+ error: ParseError,
256
+ ): ParseError {
257
+ return new RefinementError(ast, actual, kind, error);
258
+ }
259
+
260
+ /**
261
+ * @tsplus companion fncts.schema.ParseError.TransformationError
262
+ */
263
+ export class TransformationError {
264
+ readonly _tag = ParseErrorTag.Transformation;
265
+ constructor(
266
+ readonly ast: Transform,
267
+ readonly actual: unknown,
268
+ readonly kind: "Encoded" | "Transformation" | "Type",
269
+ readonly error: ParseError,
270
+ ) {}
271
+ }
272
+
273
+ /**
274
+ * @tsplus static fncts.schema.ParseError.TransformationError __call
275
+ * @tsplus static fncts.schema.ParseErrorOps TransformationError
276
+ */
277
+ export function transformationError(
278
+ ast: Transform,
279
+ actual: unknown,
280
+ kind: "Encoded" | "Transformation" | "Type",
281
+ error: ParseError,
282
+ ): ParseError {
283
+ return new TransformationError(ast, actual, kind, error);
284
+ }
285
+
286
+ /**
287
+ * @tsplus companion fncts.schema.ParseError.IterableError
288
+ */
289
+ export class IterableError {
290
+ readonly _tag = ParseErrorTag.Iterable;
291
+ constructor(
292
+ readonly ast: AST,
293
+ readonly actual: unknown,
294
+ readonly errors: Vector<IndexError | KeyError>,
295
+ ) {}
296
+ }
297
+
298
+ /**
299
+ * @tsplus static fncts.schema.ParseError.IterableError __call
300
+ * @tsplus static fncts.schema.ParseErrorOps IterableError
301
+ */
302
+ export function iterableError(ast: AST, actual: unknown, errors: Vector<IndexError | KeyError>): IterableError {
303
+ return new IterableError(ast, actual, errors);
304
+ }
@@ -0,0 +1 @@
1
+ export type ParseErrorFormatter = (error: ParseError) => string;
@@ -0,0 +1,117 @@
1
+ import type { MissingError, UnexpectedError } from "./ParseError";
2
+
3
+ import { ParseErrorTag } from "./ParseError.js";
4
+ import { formatTypeError, getMessage } from "./TreeFormatter.js";
5
+
6
+ export interface ParseErrorFlat {
7
+ type: ParseErrorTag;
8
+ path: Array<string | number | symbol>;
9
+ message: string;
10
+ }
11
+
12
+ /**
13
+ * @tsplus getter fncts.schema.ParseError flatten
14
+ */
15
+ export function flatten(error: ParseError): Array<ParseErrorFlat> {
16
+ return go(error, []);
17
+
18
+ function go(error: ParseError | MissingError | UnexpectedError, path: Array<PropertyKey>): Array<ParseErrorFlat> {
19
+ switch (error._tag) {
20
+ case ParseErrorTag.Tuple: {
21
+ return getFlattenedParseError(
22
+ error,
23
+ path,
24
+ error.errors.flatMap((error) => go(error.error, path.concat(error.index))).toArray,
25
+ );
26
+ }
27
+ case ParseErrorTag.Union: {
28
+ return getFlattenedParseError(
29
+ error,
30
+ path,
31
+
32
+ error.errors.flatMap((error) => {
33
+ if (error._tag === ParseErrorTag.UnionMember) {
34
+ return go(error.error, path);
35
+ } else {
36
+ return go(error, path);
37
+ }
38
+ }).toArray,
39
+ );
40
+ }
41
+ case ParseErrorTag.TypeLiteral: {
42
+ return getFlattenedParseError(
43
+ error,
44
+ path,
45
+ error.errors.flatMap((key) => go(key.error, path.concat(key.key))).toArray,
46
+ );
47
+ }
48
+ case ParseErrorTag.Missing: {
49
+ return [
50
+ {
51
+ type: error._tag,
52
+ path: path,
53
+ message: "is missing",
54
+ } as const,
55
+ ];
56
+ }
57
+ case ParseErrorTag.Unexpected: {
58
+ return [
59
+ {
60
+ type: error._tag,
61
+ path: path,
62
+ message: "is unexpected",
63
+ } as const,
64
+ ];
65
+ }
66
+ case ParseErrorTag.Type: {
67
+ return [
68
+ {
69
+ type: error._tag,
70
+ path: path,
71
+ message: formatTypeError(error),
72
+ } as const,
73
+ ];
74
+ }
75
+ case ParseErrorTag.Declaration:
76
+ case ParseErrorTag.Transformation:
77
+ case ParseErrorTag.Refinement: {
78
+ return getFlattenedParseError(error, path, go(error.error, path));
79
+ }
80
+ case ParseErrorTag.Iterable: {
81
+ return getFlattenedParseError(
82
+ error,
83
+ path,
84
+ error.errors.flatMap((error) => {
85
+ switch (error._tag) {
86
+ case ParseErrorTag.Key: {
87
+ return go(error.error, path.concat(error.key));
88
+ }
89
+ case ParseErrorTag.Index: {
90
+ return go(error.error, path.concat(error.index));
91
+ }
92
+ }
93
+ }).toArray,
94
+ );
95
+ }
96
+ }
97
+ }
98
+ }
99
+
100
+ function getFlattenedParseError(
101
+ error: ParseError,
102
+ path: Array<PropertyKey>,
103
+ orElse: Lazy<Array<ParseErrorFlat>>,
104
+ ): Array<ParseErrorFlat> {
105
+ return getMessage(error).match(
106
+ () => orElse(),
107
+ (message) => {
108
+ return [
109
+ {
110
+ type: error._tag,
111
+ path,
112
+ message,
113
+ },
114
+ ];
115
+ },
116
+ );
117
+ }
@@ -0,0 +1,127 @@
1
+ import type {
2
+ IndexError,
3
+ KeyError,
4
+ MissingError,
5
+ RefinementError,
6
+ TransformationError,
7
+ TypeError,
8
+ UnexpectedError,
9
+ } from "./ParseError";
10
+
11
+ import { showWithOptions } from "@fncts/base/data/Showable";
12
+
13
+ import { ParseErrorTag } from "./ParseError.js";
14
+
15
+ /**
16
+ * @tsplus static fncts.schema.ParseErrorOps drawTree
17
+ * @tsplus getter fncts.schema.ParseError drawTree
18
+ */
19
+ export function format(error: ParseError): string {
20
+ return go(error).draw;
21
+ }
22
+
23
+ function formatActual(actual: unknown): string {
24
+ return showWithOptions(actual, { colors: false });
25
+ }
26
+
27
+ function formatRefinementKind(error: RefinementError): string {
28
+ switch (error.kind) {
29
+ case "From": {
30
+ return "From side refinement failure";
31
+ }
32
+ case "Predicate": {
33
+ return "Predicate refinement failure";
34
+ }
35
+ }
36
+ }
37
+
38
+ function formatTransformationKind(error: TransformationError): string {
39
+ switch (error.kind) {
40
+ case "Encoded": {
41
+ return "Encoded side transformation failure";
42
+ }
43
+ case "Transformation": {
44
+ return "Transformation process failure";
45
+ }
46
+ case "Type": {
47
+ return "Type side transformation failure";
48
+ }
49
+ }
50
+ }
51
+
52
+ export function getMessage(error: ParseError): Maybe<string> {
53
+ return error.ast.annotations.get(ASTAnnotation.Message).map((f) => f(error));
54
+ }
55
+
56
+ export function formatTypeError(error: TypeError): string {
57
+ return getMessage(error).getOrElse(`Expected ${error.ast.toString(true)}, actual ${formatActual(error.actual)}`);
58
+ }
59
+
60
+ function formatKeyErrors(errors: Vector<KeyError | IndexError>): Vector<RoseTree<string>> {
61
+ return errors.map((error) => {
62
+ if (error._tag === ParseErrorTag.Key) {
63
+ return RoseTree(`[${String(error.keyAST)}]`, Vector(go(error.error)));
64
+ } else {
65
+ return RoseTree(`[${error.index}]`, Vector(go(error.error)));
66
+ }
67
+ });
68
+ }
69
+
70
+ function getRoseTree(error: ParseError, orElse: Lazy<RoseTree<string>>) {
71
+ return getMessage(error).match(
72
+ () => orElse(),
73
+ (message) => RoseTree(message),
74
+ );
75
+ }
76
+
77
+ function go(error: ParseError | MissingError | UnexpectedError): RoseTree<string> {
78
+ switch (error._tag) {
79
+ case ParseErrorTag.Type:
80
+ return RoseTree(formatTypeError(error));
81
+ case ParseErrorTag.Declaration:
82
+ return getRoseTree(error, () => {
83
+ const shouldSkipDefaultMessage = error.error._tag === ParseErrorTag.Type && error.error.ast === error.ast;
84
+ if (shouldSkipDefaultMessage) {
85
+ return go(error.error);
86
+ } else {
87
+ return RoseTree(error.ast.toString(true), Vector(go(error.error)));
88
+ }
89
+ });
90
+ case ParseErrorTag.Unexpected:
91
+ return RoseTree("is unexpected");
92
+ case ParseErrorTag.Missing:
93
+ return RoseTree("is missing");
94
+ case ParseErrorTag.TypeLiteral:
95
+ return getRoseTree(error, () => {
96
+ return RoseTree(error.ast.toString(true), formatKeyErrors(error.errors));
97
+ });
98
+ case ParseErrorTag.Tuple:
99
+ return getRoseTree(error, () => {
100
+ return RoseTree(error.ast.toString(true), formatKeyErrors(error.errors));
101
+ });
102
+ case ParseErrorTag.Union:
103
+ return RoseTree(
104
+ error.ast.toString(true),
105
+ error.errors.map((error) => {
106
+ switch (error._tag) {
107
+ case ParseErrorTag.UnionMember:
108
+ return RoseTree("Union member", Vector(go(error.error)));
109
+ default:
110
+ return go(error);
111
+ }
112
+ }),
113
+ );
114
+ case ParseErrorTag.Refinement:
115
+ return getRoseTree(error, () => {
116
+ return RoseTree(formatRefinementKind(error), Vector(go(error.error)));
117
+ });
118
+ case ParseErrorTag.Transformation:
119
+ return getRoseTree(error, () => {
120
+ return RoseTree(formatTransformationKind(error), Vector(go(error.error)));
121
+ });
122
+ case ParseErrorTag.Iterable:
123
+ return getRoseTree(error, () => {
124
+ return RoseTree(error.ast.toString(true), formatKeyErrors(error.errors));
125
+ });
126
+ }
127
+ }