@shirudo/result 0.0.3 → 0.0.5
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.
- package/README.md +2 -0
- package/dist/index.cjs +240 -142
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +160 -114
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +160 -114
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +225 -143
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -13,14 +13,96 @@ var Pipeable = class {
|
|
|
13
13
|
}
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/errors.ts
|
|
18
|
+
const ERR_INVALID_STATE = "ERR_INVALID_STATE";
|
|
19
|
+
const ERR_TASK_YIELD_NOT_RESULT = "ERR_TASK_YIELD_NOT_RESULT";
|
|
20
|
+
const ERR_MATCH_ON_OK = "ERR_MATCH_ON_OK";
|
|
21
|
+
const ERR_UNWRAP_ON_ERR = "ERR_UNWRAP_ON_ERR";
|
|
22
|
+
const ERR_UNWRAP_ERR_ON_OK = "ERR_UNWRAP_ERR_ON_OK";
|
|
23
|
+
const ERR_EXPECT_OK = "ERR_EXPECT_OK";
|
|
24
|
+
const ERR_EXPECT_ERR = "ERR_EXPECT_ERR";
|
|
25
|
+
const formatResultErrorMessage = (code, message, context) => {
|
|
26
|
+
if (context) return `${code}: ${message} (context: ${context})`;
|
|
27
|
+
return `${code}: ${message}`;
|
|
28
|
+
};
|
|
29
|
+
var ResultError = class extends Error {
|
|
30
|
+
code;
|
|
31
|
+
context;
|
|
32
|
+
constructor(message, code, context) {
|
|
33
|
+
super(formatResultErrorMessage(code, message, context));
|
|
34
|
+
this.code = code;
|
|
35
|
+
this.context = context;
|
|
36
|
+
this.name = new.target.name;
|
|
37
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var ResultTypeError = class extends TypeError {
|
|
41
|
+
code;
|
|
42
|
+
context;
|
|
43
|
+
constructor(message, code, context) {
|
|
44
|
+
super(formatResultErrorMessage(code, message, context));
|
|
45
|
+
this.code = code;
|
|
46
|
+
this.context = context;
|
|
47
|
+
this.name = new.target.name;
|
|
48
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
const INVALID_RESULT_STATE_MESSAGE = "Unreachable: Result is neither Ok nor Err";
|
|
52
|
+
var InvalidResultStateError = class extends ResultError {
|
|
53
|
+
constructor(context) {
|
|
54
|
+
super(INVALID_RESULT_STATE_MESSAGE, ERR_INVALID_STATE, context);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var TaskYieldNotResultError = class extends ResultTypeError {
|
|
58
|
+
yieldedValue;
|
|
59
|
+
constructor(yieldedValue) {
|
|
60
|
+
super("task() expected yielded values to be Result. Use `yield*` on a Result.", ERR_TASK_YIELD_NOT_RESULT);
|
|
61
|
+
this.yieldedValue = yieldedValue;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
var MatchOnOkError = class extends ResultTypeError {
|
|
65
|
+
constructor() {
|
|
66
|
+
super("match() can only be called on Err results. Use `if (result.isErr()) { ... }` first.", ERR_MATCH_ON_OK);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
var UnwrapOnErrError = class extends ResultTypeError {
|
|
70
|
+
errorValue;
|
|
71
|
+
constructor(errorValue) {
|
|
72
|
+
super(`Called unwrap() on Err: ${String(errorValue)}`, ERR_UNWRAP_ON_ERR);
|
|
73
|
+
this.errorValue = errorValue;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
var UnwrapErrOnOkError = class extends ResultTypeError {
|
|
77
|
+
okValue;
|
|
78
|
+
constructor(okValue) {
|
|
79
|
+
super(`Called unwrapErr() on Ok: ${String(okValue)}`, ERR_UNWRAP_ERR_ON_OK);
|
|
80
|
+
this.okValue = okValue;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
var ExpectOkError = class extends ResultError {
|
|
84
|
+
expectedMessage;
|
|
85
|
+
constructor(expectedMessage) {
|
|
86
|
+
super(expectedMessage, ERR_EXPECT_OK);
|
|
87
|
+
this.expectedMessage = expectedMessage;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
var ExpectErrError = class extends ResultError {
|
|
91
|
+
expectedMessage;
|
|
92
|
+
constructor(expectedMessage) {
|
|
93
|
+
super(expectedMessage, ERR_EXPECT_ERR);
|
|
94
|
+
this.expectedMessage = expectedMessage;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
16
98
|
//#endregion
|
|
17
99
|
//#region src/core/matcher.ts
|
|
18
100
|
/**
|
|
19
|
-
* Matcher
|
|
101
|
+
* Matcher for Err values (returns an arbitrary return type, e.g. string messages).
|
|
20
102
|
*
|
|
21
|
-
* - `.when(Ctor, handler)`
|
|
22
|
-
* - `.whenGuard(guard, handler)`
|
|
23
|
-
* - `.run()`
|
|
103
|
+
* - `.when(Ctor, handler)` matches via `instanceof`
|
|
104
|
+
* - `.whenGuard(guard, handler)` matches via Type-Guard
|
|
105
|
+
* - `.run()` is only allowed if all error cases have been handled (`E` has been reduced to `never`)
|
|
24
106
|
*/
|
|
25
107
|
var ErrorMatchBuilder = class ErrorMatchBuilder {
|
|
26
108
|
#error;
|
|
@@ -56,11 +138,11 @@ function isResult$1(value) {
|
|
|
56
138
|
return typeof value === "object" && value !== null && "isOk" in value && typeof value.isOk === "function" && "isErr" in value && typeof value.isErr === "function";
|
|
57
139
|
}
|
|
58
140
|
/**
|
|
59
|
-
* Matcher
|
|
141
|
+
* Matcher for `Result` Errors, which returns a `Result` again.
|
|
60
142
|
*
|
|
61
|
-
*
|
|
62
|
-
* -
|
|
63
|
-
* -
|
|
143
|
+
* Handlers may:
|
|
144
|
+
* - return a `Result` (is returned directly)
|
|
145
|
+
* - return an Error value (is automatically wrapped into `Err(error)`)
|
|
64
146
|
*/
|
|
65
147
|
var ErrMatchBuilder = class ErrMatchBuilder {
|
|
66
148
|
#makeErr;
|
|
@@ -75,7 +157,7 @@ var ErrMatchBuilder = class ErrMatchBuilder {
|
|
|
75
157
|
static fromResult(result, makeErr) {
|
|
76
158
|
if (result.isOk()) return new ErrMatchBuilder(makeErr, void 0, result);
|
|
77
159
|
if (result.isErr()) return new ErrMatchBuilder(makeErr, result.error);
|
|
78
|
-
throw new
|
|
160
|
+
throw new InvalidResultStateError("ErrMatchBuilder.fromResult");
|
|
79
161
|
}
|
|
80
162
|
when(ctor, handler) {
|
|
81
163
|
if (this.#resolved) return this;
|
|
@@ -110,8 +192,8 @@ var ErrMatchBuilder = class ErrMatchBuilder {
|
|
|
110
192
|
//#endregion
|
|
111
193
|
//#region src/core/map.ts
|
|
112
194
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
195
|
+
* Transforms the value (Ok case).
|
|
196
|
+
* Corresponds to Rust `map`.
|
|
115
197
|
*/
|
|
116
198
|
function map(project) {
|
|
117
199
|
return (source) => {
|
|
@@ -123,8 +205,8 @@ function map(project) {
|
|
|
123
205
|
//#endregion
|
|
124
206
|
//#region src/core/mapErr.ts
|
|
125
207
|
/**
|
|
126
|
-
*
|
|
127
|
-
*
|
|
208
|
+
* Transforms the error (Err case).
|
|
209
|
+
* Corresponds to Rust `map_err`.
|
|
128
210
|
*/
|
|
129
211
|
function mapErr(project) {
|
|
130
212
|
return (source) => {
|
|
@@ -136,26 +218,26 @@ function mapErr(project) {
|
|
|
136
218
|
//#endregion
|
|
137
219
|
//#region src/core/mapBoth.ts
|
|
138
220
|
/**
|
|
139
|
-
*
|
|
140
|
-
*
|
|
221
|
+
* Transforms both the Ok value and the Err error.
|
|
222
|
+
* Corresponds to FP `bimap` / `mapBoth`.
|
|
141
223
|
*/
|
|
142
224
|
function mapBoth(mapOk, mapErr$1) {
|
|
143
225
|
return (source) => {
|
|
144
226
|
if (source.isOk()) return ok(mapOk(source.value));
|
|
145
227
|
if (source.isErr()) return err(mapErr$1(source.error));
|
|
146
|
-
throw new
|
|
228
|
+
throw new InvalidResultStateError("mapBoth");
|
|
147
229
|
};
|
|
148
230
|
}
|
|
149
231
|
/**
|
|
150
|
-
* Alias
|
|
232
|
+
* Alias for `mapBoth`.
|
|
151
233
|
*/
|
|
152
234
|
const bimap = mapBoth;
|
|
153
235
|
|
|
154
236
|
//#endregion
|
|
155
237
|
//#region src/core/flatMap.ts
|
|
156
238
|
/**
|
|
157
|
-
*
|
|
158
|
-
*
|
|
239
|
+
* Chains another operation that returns a Result.
|
|
240
|
+
* Corresponds to Rust `and_then` or JS `flatMap`.
|
|
159
241
|
*/
|
|
160
242
|
function flatMap(project) {
|
|
161
243
|
return (source) => {
|
|
@@ -170,7 +252,7 @@ function zipImpl(left, right) {
|
|
|
170
252
|
if (left.isErr()) return left;
|
|
171
253
|
if (right.isErr()) return right;
|
|
172
254
|
if (left.isOk() && right.isOk()) return ok([left.value, right.value]);
|
|
173
|
-
throw new
|
|
255
|
+
throw new InvalidResultStateError("zip");
|
|
174
256
|
}
|
|
175
257
|
function zip(...args) {
|
|
176
258
|
if (args.length === 1) {
|
|
@@ -199,8 +281,8 @@ function combine(...args) {
|
|
|
199
281
|
//#endregion
|
|
200
282
|
//#region src/core/tap.ts
|
|
201
283
|
/**
|
|
202
|
-
*
|
|
203
|
-
*
|
|
284
|
+
* Executes a side effect (logging, debugging) without changing the Result.
|
|
285
|
+
* Corresponds to Rust `inspect` / `inspect_err`.
|
|
204
286
|
*/
|
|
205
287
|
function tap(observer) {
|
|
206
288
|
return (source) => {
|
|
@@ -213,8 +295,8 @@ function tap(observer) {
|
|
|
213
295
|
//#endregion
|
|
214
296
|
//#region src/core/filter.ts
|
|
215
297
|
/**
|
|
216
|
-
*
|
|
217
|
-
*
|
|
298
|
+
* Checks a condition. If false, the Result becomes Err.
|
|
299
|
+
* Corresponds to Rust `filter` (partially).
|
|
218
300
|
*/
|
|
219
301
|
function filter(predicate, errorFn) {
|
|
220
302
|
return (source) => {
|
|
@@ -229,59 +311,59 @@ function filter(predicate, errorFn) {
|
|
|
229
311
|
//#endregion
|
|
230
312
|
//#region src/core/match.ts
|
|
231
313
|
/**
|
|
232
|
-
*
|
|
233
|
-
*
|
|
314
|
+
* Resolves the Result. The end of the pipe.
|
|
315
|
+
* Corresponds to Rust `match`.
|
|
234
316
|
*/
|
|
235
317
|
function match(handlers) {
|
|
236
318
|
return (source) => {
|
|
237
319
|
if (source.isOk()) return handlers.ok(source.value);
|
|
238
320
|
if (source.isErr()) return handlers.err(source.error);
|
|
239
|
-
throw new
|
|
321
|
+
throw new InvalidResultStateError("match");
|
|
240
322
|
};
|
|
241
323
|
}
|
|
242
324
|
|
|
243
325
|
//#endregion
|
|
244
326
|
//#region src/core/recover.ts
|
|
245
327
|
/**
|
|
246
|
-
* Recover:
|
|
247
|
-
*
|
|
328
|
+
* Recover: converts Err to Ok(defaultValue).
|
|
329
|
+
* Result is guaranteed to be Ok → Error type becomes `never`.
|
|
248
330
|
*/
|
|
249
331
|
function recover(defaultValue) {
|
|
250
332
|
return (source) => {
|
|
251
333
|
if (source.isOk()) return source;
|
|
252
334
|
if (source.isErr()) return ok(defaultValue);
|
|
253
|
-
throw new
|
|
335
|
+
throw new InvalidResultStateError("recover");
|
|
254
336
|
};
|
|
255
337
|
}
|
|
256
338
|
/**
|
|
257
|
-
*
|
|
339
|
+
* Like `recover`, but calculates the default value based on the error.
|
|
258
340
|
*/
|
|
259
341
|
function recoverWith(fn) {
|
|
260
342
|
return (source) => {
|
|
261
343
|
if (source.isOk()) return source;
|
|
262
344
|
if (source.isErr()) return ok(fn(source.error));
|
|
263
|
-
throw new
|
|
345
|
+
throw new InvalidResultStateError("recoverWith");
|
|
264
346
|
};
|
|
265
347
|
}
|
|
266
348
|
|
|
267
349
|
//#endregion
|
|
268
350
|
//#region src/core/swap.ts
|
|
269
351
|
/**
|
|
270
|
-
*
|
|
352
|
+
* Swaps Ok and Err.
|
|
271
353
|
* Result<T, E> → Result<E, T>
|
|
272
354
|
*/
|
|
273
355
|
function swap(result) {
|
|
274
356
|
if (result.isOk()) return err(result.value);
|
|
275
357
|
if (result.isErr()) return ok(result.error);
|
|
276
|
-
throw new
|
|
358
|
+
throw new InvalidResultStateError("swap");
|
|
277
359
|
}
|
|
278
360
|
|
|
279
361
|
//#endregion
|
|
280
362
|
//#region src/core/tryCatch.ts
|
|
281
363
|
/**
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
*
|
|
364
|
+
* Executes a function and catches exceptions.
|
|
365
|
+
* Converts exceptions into Result<E>.
|
|
366
|
+
* Corresponds to Rust `Result::from` for fallible operations.
|
|
285
367
|
*/
|
|
286
368
|
function tryCatch(fn, errorMapper) {
|
|
287
369
|
return (source) => {
|
|
@@ -297,14 +379,14 @@ function tryCatch(fn, errorMapper) {
|
|
|
297
379
|
//#endregion
|
|
298
380
|
//#region src/core/tryMap.ts
|
|
299
381
|
/**
|
|
300
|
-
*
|
|
382
|
+
* Like `map`, but catches exceptions and converts them to Err.
|
|
301
383
|
*/
|
|
302
384
|
function tryMap(project, errorMapper) {
|
|
303
385
|
return (source) => {
|
|
304
386
|
if (source.isErr()) return source;
|
|
305
387
|
try {
|
|
306
388
|
if (source.isOk()) return ok(project(source.value));
|
|
307
|
-
throw new
|
|
389
|
+
throw new InvalidResultStateError("tryMap");
|
|
308
390
|
} catch (error) {
|
|
309
391
|
return err(errorMapper ? errorMapper(error) : error);
|
|
310
392
|
}
|
|
@@ -326,7 +408,7 @@ function collectFirstOk(results) {
|
|
|
326
408
|
errors.push(result.error);
|
|
327
409
|
continue;
|
|
328
410
|
}
|
|
329
|
-
throw new
|
|
411
|
+
throw new InvalidResultStateError("collectFirstOk");
|
|
330
412
|
}
|
|
331
413
|
return err(errors);
|
|
332
414
|
}
|
|
@@ -334,7 +416,7 @@ function collectFirstOk(results) {
|
|
|
334
416
|
//#endregion
|
|
335
417
|
//#region src/core/mapAsync.ts
|
|
336
418
|
/**
|
|
337
|
-
* Async
|
|
419
|
+
* Async version of map.
|
|
338
420
|
*/
|
|
339
421
|
function mapAsync(project) {
|
|
340
422
|
return async (source) => {
|
|
@@ -346,7 +428,7 @@ function mapAsync(project) {
|
|
|
346
428
|
//#endregion
|
|
347
429
|
//#region src/core/mapErrAsync.ts
|
|
348
430
|
/**
|
|
349
|
-
* Async
|
|
431
|
+
* Async version of mapErr.
|
|
350
432
|
*/
|
|
351
433
|
function mapErrAsync(project) {
|
|
352
434
|
return async (source) => {
|
|
@@ -358,7 +440,7 @@ function mapErrAsync(project) {
|
|
|
358
440
|
//#endregion
|
|
359
441
|
//#region src/core/flatMapAsync.ts
|
|
360
442
|
/**
|
|
361
|
-
* Async
|
|
443
|
+
* Async version of flatMap.
|
|
362
444
|
*/
|
|
363
445
|
function flatMapAsync(project) {
|
|
364
446
|
return async (source) => {
|
|
@@ -370,7 +452,7 @@ function flatMapAsync(project) {
|
|
|
370
452
|
//#endregion
|
|
371
453
|
//#region src/core/tapAsync.ts
|
|
372
454
|
/**
|
|
373
|
-
* Async
|
|
455
|
+
* Async version of tap.
|
|
374
456
|
*/
|
|
375
457
|
function tapAsync(observer) {
|
|
376
458
|
return async (source) => {
|
|
@@ -383,7 +465,7 @@ function tapAsync(observer) {
|
|
|
383
465
|
//#endregion
|
|
384
466
|
//#region src/core/filterAsync.ts
|
|
385
467
|
/**
|
|
386
|
-
* Async
|
|
468
|
+
* Async version of filter.
|
|
387
469
|
*/
|
|
388
470
|
function filterAsync(predicate, errorFn) {
|
|
389
471
|
return async (source) => {
|
|
@@ -398,20 +480,20 @@ function filterAsync(predicate, errorFn) {
|
|
|
398
480
|
//#endregion
|
|
399
481
|
//#region src/core/matchAsync.ts
|
|
400
482
|
/**
|
|
401
|
-
* Async
|
|
483
|
+
* Async version of match.
|
|
402
484
|
*/
|
|
403
485
|
function matchAsync(handlers) {
|
|
404
486
|
return async (source) => {
|
|
405
487
|
if (source.isOk()) return await handlers.ok(source.value);
|
|
406
488
|
if (source.isErr()) return await handlers.err(source.error);
|
|
407
|
-
throw new
|
|
489
|
+
throw new InvalidResultStateError("matchAsync");
|
|
408
490
|
};
|
|
409
491
|
}
|
|
410
492
|
|
|
411
493
|
//#endregion
|
|
412
494
|
//#region src/core/tryCatchAsync.ts
|
|
413
495
|
/**
|
|
414
|
-
* Async
|
|
496
|
+
* Async version of tryCatch.
|
|
415
497
|
*/
|
|
416
498
|
function tryCatchAsync(fn, errorMapper) {
|
|
417
499
|
return async (source) => {
|
|
@@ -427,14 +509,14 @@ function tryCatchAsync(fn, errorMapper) {
|
|
|
427
509
|
//#endregion
|
|
428
510
|
//#region src/core/tryMapAsync.ts
|
|
429
511
|
/**
|
|
430
|
-
* Async
|
|
512
|
+
* Async version of tryMap.
|
|
431
513
|
*/
|
|
432
514
|
function tryMapAsync(project, errorMapper) {
|
|
433
515
|
return async (source) => {
|
|
434
516
|
if (source.isErr()) return source;
|
|
435
517
|
try {
|
|
436
518
|
if (source.isOk()) return ok(await project(source.value));
|
|
437
|
-
throw new
|
|
519
|
+
throw new InvalidResultStateError("tryMapAsync");
|
|
438
520
|
} catch (error) {
|
|
439
521
|
return err(errorMapper ? errorMapper(error) : error);
|
|
440
522
|
}
|
|
@@ -459,7 +541,7 @@ var ResultBase = class extends Pipeable {
|
|
|
459
541
|
fold(onOk, onErr) {
|
|
460
542
|
if (this._tag === "Ok") return onOk(this.value);
|
|
461
543
|
if (this._tag === "Err") return onErr(this.error);
|
|
462
|
-
throw new
|
|
544
|
+
throw new InvalidResultStateError("Result.fold");
|
|
463
545
|
}
|
|
464
546
|
/**
|
|
465
547
|
* Enables `yield* result` in generators (Do-notation).
|
|
@@ -472,27 +554,27 @@ var ResultBase = class extends Pipeable {
|
|
|
472
554
|
return yield this;
|
|
473
555
|
}
|
|
474
556
|
/**
|
|
475
|
-
*
|
|
557
|
+
* Matches on the Err value via `.when(...)` chain.
|
|
476
558
|
*
|
|
477
|
-
*
|
|
478
|
-
*
|
|
559
|
+
* Note: for type safety reasons, `.match()` can only be called on a Result already narrowed to `Err`,
|
|
560
|
+
* e.g. inside `if (result.isErr()) { ... }`.
|
|
479
561
|
*/
|
|
480
562
|
match() {
|
|
481
563
|
if (this._tag === "Err") return new ErrorMatchBuilder(this.error);
|
|
482
|
-
throw new
|
|
564
|
+
throw new MatchOnOkError();
|
|
483
565
|
}
|
|
484
566
|
/**
|
|
485
|
-
*
|
|
486
|
-
* -
|
|
487
|
-
* -
|
|
567
|
+
* Matches on the Err value, but normalizes every branch to a `Result`:
|
|
568
|
+
* - Handlers may return a `Result` (is returned directly)
|
|
569
|
+
* - or an Error value (is wrapped into `Err(error)`)
|
|
488
570
|
*/
|
|
489
571
|
matchErr() {
|
|
490
572
|
const makeErr = (error) => err(error);
|
|
491
573
|
return ErrMatchBuilder.fromResult(this, makeErr);
|
|
492
574
|
}
|
|
493
575
|
/**
|
|
494
|
-
*
|
|
495
|
-
*
|
|
576
|
+
* Serializes the Result into a simple object format.
|
|
577
|
+
* Preserves the original types.
|
|
496
578
|
*/
|
|
497
579
|
serialize() {
|
|
498
580
|
if (this._tag === "Ok") return {
|
|
@@ -505,8 +587,8 @@ var ResultBase = class extends Pipeable {
|
|
|
505
587
|
};
|
|
506
588
|
}
|
|
507
589
|
/**
|
|
508
|
-
*
|
|
509
|
-
*
|
|
590
|
+
* Serializes the Result into a user-friendly format.
|
|
591
|
+
* Converts Errors to readable strings.
|
|
510
592
|
*/
|
|
511
593
|
toUserFriendly() {
|
|
512
594
|
if (this._tag === "Ok") return {
|
|
@@ -614,7 +696,7 @@ async function task(makeGenerator, onThrow) {
|
|
|
614
696
|
return err(onThrow(caught));
|
|
615
697
|
}
|
|
616
698
|
const yielded = step.value;
|
|
617
|
-
if (!isResult(yielded)) throw new
|
|
699
|
+
if (!isResult(yielded)) throw new TaskYieldNotResultError(yielded);
|
|
618
700
|
if (yielded.isOk()) {
|
|
619
701
|
input = yielded.value;
|
|
620
702
|
continue;
|
|
@@ -628,7 +710,7 @@ async function task(makeGenerator, onThrow) {
|
|
|
628
710
|
}
|
|
629
711
|
return yielded;
|
|
630
712
|
}
|
|
631
|
-
throw new
|
|
713
|
+
throw new InvalidResultStateError("task");
|
|
632
714
|
}
|
|
633
715
|
}
|
|
634
716
|
const gen = task;
|
|
@@ -645,7 +727,7 @@ function fold(handlers) {
|
|
|
645
727
|
return (source) => {
|
|
646
728
|
if (source.isOk()) return handlers.ok(source.value);
|
|
647
729
|
if (source.isErr()) return handlers.err(source.error);
|
|
648
|
-
throw new
|
|
730
|
+
throw new InvalidResultStateError("fold");
|
|
649
731
|
};
|
|
650
732
|
}
|
|
651
733
|
|
|
@@ -661,27 +743,27 @@ function foldAsync(handlers) {
|
|
|
661
743
|
return async (source) => {
|
|
662
744
|
if (source.isOk()) return await handlers.ok(source.value);
|
|
663
745
|
if (source.isErr()) return await handlers.err(source.error);
|
|
664
|
-
throw new
|
|
746
|
+
throw new InvalidResultStateError("foldAsync");
|
|
665
747
|
};
|
|
666
748
|
}
|
|
667
749
|
|
|
668
750
|
//#endregion
|
|
669
751
|
//#region src/core/unwrap.ts
|
|
670
752
|
/**
|
|
671
|
-
*
|
|
672
|
-
*
|
|
753
|
+
* Returns the value or throws an Error.
|
|
754
|
+
* Corresponds to Rust `unwrap`.
|
|
673
755
|
*/
|
|
674
756
|
function unwrap(result) {
|
|
675
757
|
if (result.isOk()) return result.value;
|
|
676
|
-
if (result.isErr()) throw new
|
|
677
|
-
throw new
|
|
758
|
+
if (result.isErr()) throw new UnwrapOnErrError(result.error);
|
|
759
|
+
throw new InvalidResultStateError("unwrap");
|
|
678
760
|
}
|
|
679
761
|
|
|
680
762
|
//#endregion
|
|
681
763
|
//#region src/core/unwrapOr.ts
|
|
682
764
|
/**
|
|
683
|
-
*
|
|
684
|
-
* Pure function
|
|
765
|
+
* Returns the value or a default value.
|
|
766
|
+
* Pure function alternative to the instance method.
|
|
685
767
|
*/
|
|
686
768
|
function unwrapOr(result, defaultValue) {
|
|
687
769
|
if (result.isOk()) return result.value;
|
|
@@ -691,20 +773,20 @@ function unwrapOr(result, defaultValue) {
|
|
|
691
773
|
//#endregion
|
|
692
774
|
//#region src/core/unwrapOrElse.ts
|
|
693
775
|
/**
|
|
694
|
-
*
|
|
695
|
-
*
|
|
776
|
+
* Returns the value or calculates a default value using a function.
|
|
777
|
+
* Corresponds to Rust `unwrap_or_else`.
|
|
696
778
|
*/
|
|
697
779
|
function unwrapOrElse(result, fn) {
|
|
698
780
|
if (result.isOk()) return result.value;
|
|
699
781
|
if (result.isErr()) return fn(result.error);
|
|
700
|
-
throw new
|
|
782
|
+
throw new InvalidResultStateError("unwrapOrElse");
|
|
701
783
|
}
|
|
702
784
|
|
|
703
785
|
//#endregion
|
|
704
786
|
//#region src/core/unwrapOrDefault.ts
|
|
705
787
|
/**
|
|
706
|
-
* Alias
|
|
707
|
-
*
|
|
788
|
+
* Alias for `unwrapOr`.
|
|
789
|
+
* Corresponds to Rust `unwrap_or_default` (with explicit default value).
|
|
708
790
|
*/
|
|
709
791
|
function unwrapOrDefault(result, defaultValue) {
|
|
710
792
|
return unwrapOr(result, defaultValue);
|
|
@@ -713,54 +795,54 @@ function unwrapOrDefault(result, defaultValue) {
|
|
|
713
795
|
//#endregion
|
|
714
796
|
//#region src/core/unwrapOrThrow.ts
|
|
715
797
|
/**
|
|
716
|
-
*
|
|
717
|
-
*
|
|
798
|
+
* Returns the value or throws the original Err value (not wrapped).
|
|
799
|
+
* Useful to preserve `Error` instances including stack traces.
|
|
718
800
|
*/
|
|
719
801
|
function unwrapOrThrow(result) {
|
|
720
802
|
if (result.isOk()) return result.value;
|
|
721
803
|
if (result.isErr()) throw result.error;
|
|
722
|
-
throw new
|
|
804
|
+
throw new InvalidResultStateError("unwrapOrThrow");
|
|
723
805
|
}
|
|
724
806
|
|
|
725
807
|
//#endregion
|
|
726
808
|
//#region src/core/unwrapErr.ts
|
|
727
809
|
/**
|
|
728
|
-
*
|
|
729
|
-
*
|
|
810
|
+
* Returns the error or throws an Error.
|
|
811
|
+
* Corresponds to Rust `unwrap_err`.
|
|
730
812
|
*/
|
|
731
813
|
function unwrapErr(result) {
|
|
732
814
|
if (result.isErr()) return result.error;
|
|
733
|
-
if (result.isOk()) throw new
|
|
734
|
-
throw new
|
|
815
|
+
if (result.isOk()) throw new UnwrapErrOnOkError(result.value);
|
|
816
|
+
throw new InvalidResultStateError("unwrapErr");
|
|
735
817
|
}
|
|
736
818
|
|
|
737
819
|
//#endregion
|
|
738
820
|
//#region src/core/expectResult.ts
|
|
739
821
|
/**
|
|
740
|
-
*
|
|
741
|
-
*
|
|
822
|
+
* Returns the value or throws an Error with a custom message.
|
|
823
|
+
* Corresponds to Rust `expect`.
|
|
742
824
|
*/
|
|
743
825
|
function expectResult(result, message) {
|
|
744
826
|
if (result.isOk()) return result.value;
|
|
745
|
-
throw new
|
|
827
|
+
throw new ExpectOkError(message);
|
|
746
828
|
}
|
|
747
829
|
|
|
748
830
|
//#endregion
|
|
749
831
|
//#region src/core/expectErr.ts
|
|
750
832
|
/**
|
|
751
|
-
*
|
|
752
|
-
*
|
|
833
|
+
* Returns the error or throws an Error with a custom message.
|
|
834
|
+
* Corresponds to Rust `expect_err`.
|
|
753
835
|
*/
|
|
754
836
|
function expectErr(result, message) {
|
|
755
837
|
if (result.isErr()) return result.error;
|
|
756
|
-
throw new
|
|
838
|
+
throw new ExpectErrError(message);
|
|
757
839
|
}
|
|
758
840
|
|
|
759
841
|
//#endregion
|
|
760
842
|
//#region src/core/and.ts
|
|
761
843
|
/**
|
|
762
|
-
*
|
|
763
|
-
*
|
|
844
|
+
* Combines two Results. Returns the second one only if the first is Ok.
|
|
845
|
+
* Corresponds to Rust `and`.
|
|
764
846
|
*/
|
|
765
847
|
function and(result, other) {
|
|
766
848
|
return result.isOk() ? other : result;
|
|
@@ -769,8 +851,8 @@ function and(result, other) {
|
|
|
769
851
|
//#endregion
|
|
770
852
|
//#region src/core/or.ts
|
|
771
853
|
/**
|
|
772
|
-
* Fallback
|
|
773
|
-
*
|
|
854
|
+
* Fallback to another Result if the first is Err.
|
|
855
|
+
* Corresponds to Rust `or`.
|
|
774
856
|
*/
|
|
775
857
|
function or(result, other) {
|
|
776
858
|
return result.isOk() ? result : other;
|
|
@@ -779,20 +861,20 @@ function or(result, other) {
|
|
|
779
861
|
//#endregion
|
|
780
862
|
//#region src/core/orElse.ts
|
|
781
863
|
/**
|
|
782
|
-
* Fallback
|
|
783
|
-
*
|
|
864
|
+
* Fallback with a function that returns a Result.
|
|
865
|
+
* Corresponds to Rust `or_else`.
|
|
784
866
|
*/
|
|
785
867
|
function orElse(result, fn) {
|
|
786
868
|
if (result.isOk()) return result;
|
|
787
869
|
if (result.isErr()) return fn(result.error);
|
|
788
|
-
throw new
|
|
870
|
+
throw new InvalidResultStateError("orElse");
|
|
789
871
|
}
|
|
790
872
|
|
|
791
873
|
//#endregion
|
|
792
874
|
//#region src/core/mapOr.ts
|
|
793
875
|
/**
|
|
794
|
-
*
|
|
795
|
-
*
|
|
876
|
+
* Transforms the value or returns a default value.
|
|
877
|
+
* Corresponds to Rust `map_or`.
|
|
796
878
|
*/
|
|
797
879
|
function mapOr(result, defaultValue, fn) {
|
|
798
880
|
if (result.isOk()) return fn(result.value);
|
|
@@ -802,21 +884,21 @@ function mapOr(result, defaultValue, fn) {
|
|
|
802
884
|
//#endregion
|
|
803
885
|
//#region src/core/mapOrElse.ts
|
|
804
886
|
/**
|
|
805
|
-
*
|
|
806
|
-
*
|
|
887
|
+
* Transforms the value or calculates a default value using a function.
|
|
888
|
+
* Corresponds to Rust `map_or_else`.
|
|
807
889
|
*/
|
|
808
890
|
function mapOrElse(result, defaultFn, fn) {
|
|
809
891
|
if (result.isOk()) return fn(result.value);
|
|
810
892
|
if (result.isErr()) return defaultFn(result.error);
|
|
811
|
-
throw new
|
|
893
|
+
throw new InvalidResultStateError("mapOrElse");
|
|
812
894
|
}
|
|
813
895
|
|
|
814
896
|
//#endregion
|
|
815
897
|
//#region src/core/sequence.ts
|
|
816
898
|
/**
|
|
817
|
-
*
|
|
818
|
-
* Short-circuits
|
|
819
|
-
*
|
|
899
|
+
* Combines a list of Results into a single Result of a list.
|
|
900
|
+
* Short-circuits on the first Err.
|
|
901
|
+
* Analogous to Rust `collect::<Result<Vec<_>, _>>()`.
|
|
820
902
|
*/
|
|
821
903
|
function sequence(results) {
|
|
822
904
|
const values = [];
|
|
@@ -826,12 +908,12 @@ function sequence(results) {
|
|
|
826
908
|
continue;
|
|
827
909
|
}
|
|
828
910
|
if (result.isErr()) return result;
|
|
829
|
-
throw new
|
|
911
|
+
throw new InvalidResultStateError("sequence");
|
|
830
912
|
}
|
|
831
913
|
return ok(values);
|
|
832
914
|
}
|
|
833
915
|
/**
|
|
834
|
-
* Alias
|
|
916
|
+
* Alias for `sequence`.
|
|
835
917
|
*/
|
|
836
918
|
function all(results) {
|
|
837
919
|
return sequence(results);
|
|
@@ -840,8 +922,8 @@ function all(results) {
|
|
|
840
922
|
//#endregion
|
|
841
923
|
//#region src/core/sequenceRecord.ts
|
|
842
924
|
/**
|
|
843
|
-
*
|
|
844
|
-
* Short-circuits
|
|
925
|
+
* Like `sequence`, but for Records/Objects.
|
|
926
|
+
* Short-circuits on the first Err.
|
|
845
927
|
*/
|
|
846
928
|
function sequenceRecord(record) {
|
|
847
929
|
const out = {};
|
|
@@ -853,7 +935,7 @@ function sequenceRecord(record) {
|
|
|
853
935
|
continue;
|
|
854
936
|
}
|
|
855
937
|
if (result.isErr()) return result;
|
|
856
|
-
throw new
|
|
938
|
+
throw new InvalidResultStateError("sequenceRecord");
|
|
857
939
|
}
|
|
858
940
|
return ok(out);
|
|
859
941
|
}
|
|
@@ -861,11 +943,11 @@ function sequenceRecord(record) {
|
|
|
861
943
|
//#endregion
|
|
862
944
|
//#region src/core/collectFirstOkAsync.ts
|
|
863
945
|
/**
|
|
864
|
-
* Async
|
|
946
|
+
* Async version of collectFirstOk.
|
|
865
947
|
*
|
|
866
|
-
* -
|
|
867
|
-
* -
|
|
868
|
-
* -
|
|
948
|
+
* - Takes either already started Promises or "Thunks" (`() => Awaitable<Result<...>>`).
|
|
949
|
+
* - Processes inputs strictly sequentially (like `for ... of` + `await`).
|
|
950
|
+
* - Returns the first `Ok` and collects all errors if no `Ok` is found.
|
|
869
951
|
*/
|
|
870
952
|
async function collectFirstOkAsync(inputs) {
|
|
871
953
|
const errors = [];
|
|
@@ -876,7 +958,7 @@ async function collectFirstOkAsync(inputs) {
|
|
|
876
958
|
errors.push(result.error);
|
|
877
959
|
continue;
|
|
878
960
|
}
|
|
879
|
-
throw new
|
|
961
|
+
throw new InvalidResultStateError("collectFirstOkAsync");
|
|
880
962
|
} catch (error) {
|
|
881
963
|
errors.push(error);
|
|
882
964
|
}
|
|
@@ -886,15 +968,15 @@ async function collectFirstOkAsync(inputs) {
|
|
|
886
968
|
//#endregion
|
|
887
969
|
//#region src/core/collectFirstOkParallelAsync.ts
|
|
888
970
|
/**
|
|
889
|
-
* Parallel
|
|
971
|
+
* Parallel version of `collectFirstOkAsync`.
|
|
890
972
|
*
|
|
891
|
-
* -
|
|
892
|
-
* -
|
|
893
|
-
* -
|
|
894
|
-
* - Rejections
|
|
895
|
-
* -
|
|
896
|
-
*
|
|
897
|
-
* -
|
|
973
|
+
* - Starts all inputs immediately (Promises or Thunks).
|
|
974
|
+
* - Returns the first `Ok` as soon as it is available.
|
|
975
|
+
* - If no `Ok` is found, returns an `Err` with all error values (in input order).
|
|
976
|
+
* - Rejections are treated as `ErrValue` (`caught as ErrValue`).
|
|
977
|
+
* - If multiple inputs provide an `Ok`, the one that completes first wins.
|
|
978
|
+
* In case of simultaneous completion, the first observed result wins.
|
|
979
|
+
* - If no `Ok` arrives and at least one input never settles, the Promise remains pending.
|
|
898
980
|
*/
|
|
899
981
|
async function collectFirstOkParallelAsync(inputs) {
|
|
900
982
|
if (inputs.length === 0) return err([]);
|
|
@@ -909,7 +991,7 @@ async function collectFirstOkParallelAsync(inputs) {
|
|
|
909
991
|
for (const entry of settled) if (entry.status === "fulfilled") {
|
|
910
992
|
const result = entry.value;
|
|
911
993
|
if (result.isErr()) errors.push(result.error);
|
|
912
|
-
else if (!result.isOk()) errors.push(
|
|
994
|
+
else if (!result.isOk()) errors.push(new InvalidResultStateError("collectFirstOkParallelAsync"));
|
|
913
995
|
} else errors.push(entry.reason);
|
|
914
996
|
return err(errors);
|
|
915
997
|
});
|
|
@@ -919,8 +1001,8 @@ async function collectFirstOkParallelAsync(inputs) {
|
|
|
919
1001
|
//#endregion
|
|
920
1002
|
//#region src/core/collectAllErrors.ts
|
|
921
1003
|
/**
|
|
922
|
-
*
|
|
923
|
-
*
|
|
1004
|
+
* Combines a list of Results.
|
|
1005
|
+
* Return Ok(values) only if all are Ok, otherwise Err([errors]).
|
|
924
1006
|
*/
|
|
925
1007
|
function collectAllErrors(results) {
|
|
926
1008
|
const values = [];
|
|
@@ -934,7 +1016,7 @@ function collectAllErrors(results) {
|
|
|
934
1016
|
errors.push(result.error);
|
|
935
1017
|
continue;
|
|
936
1018
|
}
|
|
937
|
-
throw new
|
|
1019
|
+
throw new InvalidResultStateError("collectAllErrors");
|
|
938
1020
|
}
|
|
939
1021
|
return errors.length === 0 ? ok(values) : err(errors);
|
|
940
1022
|
}
|
|
@@ -942,7 +1024,7 @@ function collectAllErrors(results) {
|
|
|
942
1024
|
//#endregion
|
|
943
1025
|
//#region src/core/partition.ts
|
|
944
1026
|
/**
|
|
945
|
-
*
|
|
1027
|
+
* Partitions Results into Ok values and Err errors.
|
|
946
1028
|
*/
|
|
947
1029
|
function partition(results) {
|
|
948
1030
|
const oks = [];
|
|
@@ -956,7 +1038,7 @@ function partition(results) {
|
|
|
956
1038
|
errs.push(result.error);
|
|
957
1039
|
continue;
|
|
958
1040
|
}
|
|
959
|
-
throw new
|
|
1041
|
+
throw new InvalidResultStateError("partition");
|
|
960
1042
|
}
|
|
961
1043
|
return [oks, errs];
|
|
962
1044
|
}
|
|
@@ -964,9 +1046,9 @@ function partition(results) {
|
|
|
964
1046
|
//#endregion
|
|
965
1047
|
//#region src/core/flatten.ts
|
|
966
1048
|
/**
|
|
967
|
-
*
|
|
1049
|
+
* Flattens a nested Result.
|
|
968
1050
|
* Result<Result<T, E>, E> → Result<T, E>
|
|
969
|
-
*
|
|
1051
|
+
* Corresponds to Rust `flatten`.
|
|
970
1052
|
*/
|
|
971
1053
|
function flatten(result) {
|
|
972
1054
|
if (result.isOk()) return result.value;
|
|
@@ -976,19 +1058,19 @@ function flatten(result) {
|
|
|
976
1058
|
//#endregion
|
|
977
1059
|
//#region src/core/toPromise.ts
|
|
978
1060
|
/**
|
|
979
|
-
*
|
|
1061
|
+
* Converts a Result to a Promise.
|
|
980
1062
|
* Ok → resolve(value), Err → reject(error)
|
|
981
1063
|
*/
|
|
982
1064
|
function toPromise(result) {
|
|
983
1065
|
if (result.isOk()) return Promise.resolve(result.value);
|
|
984
1066
|
if (result.isErr()) return Promise.reject(result.error);
|
|
985
|
-
throw new
|
|
1067
|
+
throw new InvalidResultStateError("toPromise");
|
|
986
1068
|
}
|
|
987
1069
|
|
|
988
1070
|
//#endregion
|
|
989
1071
|
//#region src/core/toNullable.ts
|
|
990
1072
|
/**
|
|
991
|
-
*
|
|
1073
|
+
* Converts a Result to `T | null`.
|
|
992
1074
|
* Ok → value, Err → null
|
|
993
1075
|
*/
|
|
994
1076
|
function toNullable(result) {
|
|
@@ -999,8 +1081,8 @@ function toNullable(result) {
|
|
|
999
1081
|
//#endregion
|
|
1000
1082
|
//#region src/core/isOk.ts
|
|
1001
1083
|
/**
|
|
1002
|
-
*
|
|
1003
|
-
* Pure function
|
|
1084
|
+
* Checks if a Result is Ok.
|
|
1085
|
+
* Pure function alternative to the instance method.
|
|
1004
1086
|
*/
|
|
1005
1087
|
function isOk(result) {
|
|
1006
1088
|
return result.isOk();
|
|
@@ -1009,8 +1091,8 @@ function isOk(result) {
|
|
|
1009
1091
|
//#endregion
|
|
1010
1092
|
//#region src/core/isErr.ts
|
|
1011
1093
|
/**
|
|
1012
|
-
*
|
|
1013
|
-
* Pure function
|
|
1094
|
+
* Checks if a Result is Err.
|
|
1095
|
+
* Pure function alternative to the instance method.
|
|
1014
1096
|
*/
|
|
1015
1097
|
function isErr(result) {
|
|
1016
1098
|
return result.isErr();
|
|
@@ -1019,8 +1101,8 @@ function isErr(result) {
|
|
|
1019
1101
|
//#endregion
|
|
1020
1102
|
//#region src/core/contains.ts
|
|
1021
1103
|
/**
|
|
1022
|
-
*
|
|
1023
|
-
*
|
|
1104
|
+
* Checks if the Result contains a specific value.
|
|
1105
|
+
* Corresponds to Rust `contains`.
|
|
1024
1106
|
*/
|
|
1025
1107
|
function contains(result, value) {
|
|
1026
1108
|
if (!result.isOk()) return false;
|
|
@@ -1030,8 +1112,8 @@ function contains(result, value) {
|
|
|
1030
1112
|
//#endregion
|
|
1031
1113
|
//#region src/core/containsErr.ts
|
|
1032
1114
|
/**
|
|
1033
|
-
*
|
|
1034
|
-
*
|
|
1115
|
+
* Checks if the Result contains a specific error.
|
|
1116
|
+
* Analogous to `contains` for the Err case.
|
|
1035
1117
|
*/
|
|
1036
1118
|
function containsErr(result, error) {
|
|
1037
1119
|
if (!result.isErr()) return false;
|
|
@@ -1039,9 +1121,25 @@ function containsErr(result, error) {
|
|
|
1039
1121
|
}
|
|
1040
1122
|
|
|
1041
1123
|
//#endregion
|
|
1124
|
+
exports.ERR_EXPECT_ERR = ERR_EXPECT_ERR;
|
|
1125
|
+
exports.ERR_EXPECT_OK = ERR_EXPECT_OK;
|
|
1126
|
+
exports.ERR_INVALID_STATE = ERR_INVALID_STATE;
|
|
1127
|
+
exports.ERR_MATCH_ON_OK = ERR_MATCH_ON_OK;
|
|
1128
|
+
exports.ERR_TASK_YIELD_NOT_RESULT = ERR_TASK_YIELD_NOT_RESULT;
|
|
1129
|
+
exports.ERR_UNWRAP_ERR_ON_OK = ERR_UNWRAP_ERR_ON_OK;
|
|
1130
|
+
exports.ERR_UNWRAP_ON_ERR = ERR_UNWRAP_ON_ERR;
|
|
1042
1131
|
exports.Err = Err;
|
|
1132
|
+
exports.ExpectErrError = ExpectErrError;
|
|
1133
|
+
exports.ExpectOkError = ExpectOkError;
|
|
1134
|
+
exports.InvalidResultStateError = InvalidResultStateError;
|
|
1135
|
+
exports.MatchOnOkError = MatchOnOkError;
|
|
1043
1136
|
exports.Ok = Ok;
|
|
1044
1137
|
exports.Result = Result;
|
|
1138
|
+
exports.ResultError = ResultError;
|
|
1139
|
+
exports.ResultTypeError = ResultTypeError;
|
|
1140
|
+
exports.TaskYieldNotResultError = TaskYieldNotResultError;
|
|
1141
|
+
exports.UnwrapErrOnOkError = UnwrapErrOnOkError;
|
|
1142
|
+
exports.UnwrapOnErrError = UnwrapOnErrError;
|
|
1045
1143
|
exports.all = all;
|
|
1046
1144
|
exports.and = and;
|
|
1047
1145
|
exports.bimap = bimap;
|