@sapphire/result 2.6.7-pr-725.55b1ff7b.0 → 2.7.0-next.4b7c3e6c
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 +1 -2
- package/dist/cjs/index.cjs +1774 -540
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +274 -358
- package/dist/esm/index.d.mts +274 -358
- package/dist/esm/index.mjs +1769 -537
- package/dist/esm/index.mjs.map +1 -1
- package/dist/iife/index.global.js +1774 -540
- package/dist/iife/index.global.js.map +1 -1
- package/package.json +13 -12
package/dist/esm/index.mjs
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
3
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
-
var __publicField = (obj, key, value) =>
|
|
5
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
-
return value;
|
|
7
|
-
};
|
|
4
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
8
5
|
|
|
9
6
|
// src/lib/common/utils.ts
|
|
10
7
|
function isFunction(input) {
|
|
11
8
|
return typeof input === "function";
|
|
12
9
|
}
|
|
13
10
|
__name(isFunction, "isFunction");
|
|
11
|
+
function returnThis() {
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
__name(returnThis, "returnThis");
|
|
15
|
+
|
|
16
|
+
// src/lib/OptionError.ts
|
|
17
|
+
var _OptionError = class _OptionError extends Error {
|
|
18
|
+
get name() {
|
|
19
|
+
return this.constructor.name;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
__name(_OptionError, "OptionError");
|
|
23
|
+
var OptionError = _OptionError;
|
|
14
24
|
|
|
15
|
-
// src/lib/
|
|
25
|
+
// src/lib/ResultError.ts
|
|
16
26
|
var _ResultError = class _ResultError extends Error {
|
|
17
27
|
constructor(message, value) {
|
|
18
28
|
super(message);
|
|
@@ -26,629 +36,1851 @@ var _ResultError = class _ResultError extends Error {
|
|
|
26
36
|
__name(_ResultError, "ResultError");
|
|
27
37
|
var ResultError = _ResultError;
|
|
28
38
|
|
|
29
|
-
// src/lib/Result
|
|
30
|
-
var
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
// src/lib/Result.ts
|
|
40
|
+
var ValueProperty = Symbol.for("@sapphire/result:Result.value");
|
|
41
|
+
var SuccessProperty = Symbol.for("@sapphire/result:Result.success");
|
|
42
|
+
var _a, _b;
|
|
43
|
+
var _Result = class _Result {
|
|
44
|
+
constructor(value, success) {
|
|
45
|
+
__publicField(this, _b);
|
|
46
|
+
__publicField(this, _a);
|
|
47
|
+
this[ValueProperty] = value;
|
|
48
|
+
this[SuccessProperty] = success;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Returns `true` if the result is `Ok`.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const x = ok(-3);
|
|
56
|
+
* assert.equal(x.isOk(), true);
|
|
57
|
+
* ```
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const x = err('Some error message');
|
|
61
|
+
* assert.equal(x.isOk(), false);
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok}
|
|
65
|
+
*/
|
|
35
66
|
isOk() {
|
|
36
|
-
return
|
|
67
|
+
return this[SuccessProperty];
|
|
37
68
|
}
|
|
38
69
|
isOkAnd(cb) {
|
|
39
|
-
return cb(this
|
|
40
|
-
}
|
|
70
|
+
return this.isOk() && cb(this[ValueProperty]);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Returns `true` if the result is `Err`.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const x = ok(-3);
|
|
78
|
+
* assert.equal(x.isErr(), false);
|
|
79
|
+
* ```
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* const x = err('Some error message');
|
|
83
|
+
* assert.equal(x.isErr(), true);
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err}
|
|
87
|
+
*/
|
|
41
88
|
isErr() {
|
|
42
|
-
return
|
|
43
|
-
}
|
|
44
|
-
isErrAnd() {
|
|
45
|
-
return false;
|
|
89
|
+
return !this[SuccessProperty];
|
|
46
90
|
}
|
|
91
|
+
isErrAnd(cb) {
|
|
92
|
+
return this.isErr() && cb(this[ValueProperty]);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Converts from `Result<T, E>` to `Option<T>`.
|
|
96
|
+
*
|
|
97
|
+
* Converts itself into an `Option<T>`, and discarding the error, if any.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* const x: Result<number, string> = ok(2);
|
|
102
|
+
* assert.equal(x.ok(), some(2));
|
|
103
|
+
* ```
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const x: Result<number, string> = err('Some error message');
|
|
107
|
+
* assert.equal(x.ok(), none);
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.ok}
|
|
111
|
+
*/
|
|
47
112
|
ok() {
|
|
48
|
-
return
|
|
49
|
-
}
|
|
113
|
+
return this.match({ ok: /* @__PURE__ */ __name((value) => some(value), "ok"), err: /* @__PURE__ */ __name(() => none, "err") });
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Converts from `Result<T, E>` to `Option<E>`.
|
|
117
|
+
*
|
|
118
|
+
* Converts itself into an `Option<E>`, and discarding the successful value, if any.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* const x: Result<number, string> = ok(2);
|
|
123
|
+
* assert.equal(x.err(), none);
|
|
124
|
+
* ```
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* const x: Result<number, string> = err('Some error message');
|
|
128
|
+
* assert.equal(x.err(), 'Some error message');
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.err}
|
|
132
|
+
*/
|
|
50
133
|
err() {
|
|
51
|
-
return
|
|
52
|
-
}
|
|
134
|
+
return this.match({ ok: /* @__PURE__ */ __name(() => none, "ok"), err: /* @__PURE__ */ __name((error) => some(error), "err") });
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value, leaving an `Err` value
|
|
138
|
+
* untouched.
|
|
139
|
+
* @param cb The predicate.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* const x: Result<number, string> = ok(2);
|
|
144
|
+
* assert.equal(x.map((value) => value * 2), ok(4));
|
|
145
|
+
* ```
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* const x: Result<number, string> = err('Some error message');
|
|
149
|
+
* assert.equal(x.map((value) => value * 2), err('Some error message'));
|
|
150
|
+
* ```
|
|
151
|
+
*
|
|
152
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map}
|
|
153
|
+
*/
|
|
53
154
|
map(cb) {
|
|
54
|
-
return
|
|
55
|
-
}
|
|
155
|
+
return this.match({ ok: /* @__PURE__ */ __name((value) => ok(cb(value)), "ok"), err: returnThis });
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Ok` value, leaving an `Err` value
|
|
159
|
+
* untouched.
|
|
160
|
+
*
|
|
161
|
+
* Unlike {@link map}, this method does not wrap the returned value inside `Ok`, but instead, it returns the
|
|
162
|
+
* returned value.
|
|
163
|
+
* @param cb The predicate.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```typescript
|
|
167
|
+
* const x: Result<number, string> = ok(2);
|
|
168
|
+
* assert.equal(x.mapInto((value) => ok(value * value)), ok(4));
|
|
169
|
+
* ```
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* const x: Result<number, string> = ok(0);
|
|
173
|
+
* assert.equal(
|
|
174
|
+
* x.mapInto((value) => (value === 0 ? err('zero is not divisible') : ok(1 / value))),
|
|
175
|
+
* err('zero is not divisible')
|
|
176
|
+
* );
|
|
177
|
+
* ```
|
|
178
|
+
* @example
|
|
179
|
+
* ```typescript
|
|
180
|
+
* const x: Result<number, string> = err('Some error message');
|
|
181
|
+
* assert.equal(x.mapInto((value) => ok(4)), err('Some error message'));
|
|
182
|
+
* ```
|
|
183
|
+
*
|
|
184
|
+
* @note This is an extension not supported in Rust
|
|
185
|
+
*/
|
|
56
186
|
mapInto(cb) {
|
|
57
|
-
return
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
187
|
+
return this.match({ ok: /* @__PURE__ */ __name((value) => cb(value), "ok"), err: returnThis });
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Returns the provided default (if `Err`), or applies a function to the contained value (if `Ok`),
|
|
191
|
+
*
|
|
192
|
+
* Arguments passed to `mapOr` are eagerly evaluated; if you are passing the result of a function call, it is
|
|
193
|
+
* recommended to use `mapOrElse`, which is lazily evaluated.
|
|
194
|
+
* @param defaultValue The default value to use.
|
|
195
|
+
* @param cb The predicate.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```typescript
|
|
199
|
+
* const x = ok('hello');
|
|
200
|
+
* assert.equal(x.mapOr(42, (value) => value.length), 5);
|
|
201
|
+
* ```
|
|
202
|
+
* @example
|
|
203
|
+
* ```typescript
|
|
204
|
+
* const x = err('Some error message');
|
|
205
|
+
* assert.equal(x.mapOr(42, (value) => value.length), 42);
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
208
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or}
|
|
209
|
+
*/
|
|
210
|
+
mapOr(defaultValue, cb) {
|
|
211
|
+
return this.match({ ok: /* @__PURE__ */ __name((value) => cb(value), "ok"), err: /* @__PURE__ */ __name(() => defaultValue, "err") });
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Maps a `Result<T, E>` to `U` by applying fallback function default to a contained `Err` value, or function `cb`
|
|
215
|
+
* to a contained `Ok` value.
|
|
216
|
+
*
|
|
217
|
+
* This function can be used to unpack a successful result while handling an error.
|
|
218
|
+
* @param op The predicate that is run on `Err`.
|
|
219
|
+
* @param cb The predicate that is run on `Ok`.
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```typescript
|
|
223
|
+
* const x: Result<string, string> = ok('hello');
|
|
224
|
+
* assert.equal(x.mapOrElse((error) => error.length, (value) => value.length), 5);
|
|
225
|
+
* ```
|
|
226
|
+
* @example
|
|
227
|
+
* ```typescript
|
|
228
|
+
* const x: Result<string, string> = err('Some error message');
|
|
229
|
+
* assert.equal(x.mapOrElse((error) => error.length, (value) => value.length), 18);
|
|
230
|
+
* ```
|
|
231
|
+
*
|
|
232
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or_else}
|
|
233
|
+
*/
|
|
234
|
+
mapOrElse(op, cb) {
|
|
235
|
+
return this.match({ ok: /* @__PURE__ */ __name((value) => cb(value), "ok"), err: /* @__PURE__ */ __name((error) => op(error), "err") });
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value
|
|
239
|
+
* untouched.
|
|
240
|
+
*
|
|
241
|
+
* This function can be used to pass through a successful result while handling an error.
|
|
242
|
+
* @param cb The predicate.
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* ```typescript
|
|
246
|
+
* const x: Result<number, Error> = ok(2);
|
|
247
|
+
* assert.equal(x.mapErr((error) => error.message), ok(2));
|
|
248
|
+
* ```
|
|
249
|
+
* @example
|
|
250
|
+
* ```typescript
|
|
251
|
+
* const x: Result<number, Error> = err(new Error('Some error message'));
|
|
252
|
+
* assert.equal(x.mapErr((error) => error.message), err('Some error message'));
|
|
253
|
+
* ```
|
|
254
|
+
*
|
|
255
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_err}
|
|
256
|
+
*/
|
|
257
|
+
mapErr(cb) {
|
|
258
|
+
return this.match({ ok: returnThis, err: /* @__PURE__ */ __name((error) => err(cb(error)), "err") });
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value
|
|
262
|
+
* untouched.
|
|
263
|
+
*
|
|
264
|
+
* This function can be used to pass through a successful result while handling an error.
|
|
265
|
+
*
|
|
266
|
+
* Unlike {@link mapErr}, this method does not wrap the returned value inside `Err`, but instead, it returns the
|
|
267
|
+
* returned value.
|
|
268
|
+
* @param cb The predicate.
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```typescript
|
|
272
|
+
* const x: Result<number, Error> = ok(2);
|
|
273
|
+
* assert.equal(x.mapErrInto((error) => err(error.message)), ok(2));
|
|
274
|
+
* ```
|
|
275
|
+
* @example
|
|
276
|
+
* ```typescript
|
|
277
|
+
* const x: Result<number, Error> = err(new Error('Some error message'));
|
|
278
|
+
* assert.equal(x.mapErrInto((error) => err(error.message)), err('Some error message'));
|
|
279
|
+
* ```
|
|
280
|
+
* @example
|
|
281
|
+
* ```typescript
|
|
282
|
+
* const x: Result<number, Error> = err(new Error('Some error message'));
|
|
283
|
+
* assert.equal(x.mapErrInto((error) => ok(4)), ok(4));
|
|
284
|
+
* ```
|
|
285
|
+
*
|
|
286
|
+
* @note This is an extension not supported in Rust
|
|
287
|
+
*/
|
|
288
|
+
mapErrInto(cb) {
|
|
289
|
+
return this.match({ ok: returnThis, err: /* @__PURE__ */ __name((error) => cb(error), "err") });
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Calls the provided closure with a reference to the contained value (if `Ok`).
|
|
293
|
+
* @param cb The predicate.
|
|
294
|
+
* @seealso {@link inspectAsync} for the awaitable version.
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* ```typescript
|
|
298
|
+
* ok(2).inspect(console.log);
|
|
299
|
+
* // Logs: 2
|
|
300
|
+
* ```
|
|
301
|
+
* @example
|
|
302
|
+
* ```typescript
|
|
303
|
+
* err('Some error message').inspect(console.log);
|
|
304
|
+
* // Doesn't log
|
|
305
|
+
* ```
|
|
306
|
+
*
|
|
307
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect}
|
|
308
|
+
*/
|
|
71
309
|
inspect(cb) {
|
|
72
|
-
|
|
310
|
+
if (this.isOk()) cb(this[ValueProperty]);
|
|
73
311
|
return this;
|
|
74
312
|
}
|
|
313
|
+
/**
|
|
314
|
+
* Calls the provided closure with a reference to the contained value (if `Ok`) and awaits it.
|
|
315
|
+
* @param cb The predicate.
|
|
316
|
+
* @seealso {@link inspect} for the sync version.
|
|
317
|
+
*
|
|
318
|
+
* @example
|
|
319
|
+
* ```typescript
|
|
320
|
+
* await ok(2).inspectAsync(console.log);
|
|
321
|
+
* // Logs: 2
|
|
322
|
+
* ```
|
|
323
|
+
* @example
|
|
324
|
+
* ```typescript
|
|
325
|
+
* await err('Some error message').inspectAsync(console.log);
|
|
326
|
+
* // Doesn't log
|
|
327
|
+
* ```
|
|
328
|
+
*
|
|
329
|
+
* @note This is an extension not supported in Rust
|
|
330
|
+
*/
|
|
75
331
|
async inspectAsync(cb) {
|
|
76
|
-
await cb(this
|
|
332
|
+
if (this.isOk()) await cb(this[ValueProperty]);
|
|
77
333
|
return this;
|
|
78
334
|
}
|
|
79
|
-
|
|
335
|
+
/**
|
|
336
|
+
* Calls the provided closure with a reference to the contained error (if `Err`).
|
|
337
|
+
* @param cb The predicate.
|
|
338
|
+
* @seealso {@link inspectErrAsync} for the awaitable version.
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* ```typescript
|
|
342
|
+
* ok(2).inspectErr(console.log);
|
|
343
|
+
* // Doesn't log
|
|
344
|
+
* ```
|
|
345
|
+
* @example
|
|
346
|
+
* ```typescript
|
|
347
|
+
* err('Some error message').inspectErr(console.log);
|
|
348
|
+
* // Logs: Some error message
|
|
349
|
+
* ```
|
|
350
|
+
*
|
|
351
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect_err}
|
|
352
|
+
*/
|
|
353
|
+
inspectErr(cb) {
|
|
354
|
+
if (this.isErr()) cb(this[ValueProperty]);
|
|
80
355
|
return this;
|
|
81
356
|
}
|
|
82
|
-
|
|
83
|
-
|
|
357
|
+
/**
|
|
358
|
+
* Calls the provided closure with a reference to the contained error (if `Err`) and awaits it.
|
|
359
|
+
* @param cb The predicate.
|
|
360
|
+
* @seealso {@link inspectErr} for the sync version.
|
|
361
|
+
*
|
|
362
|
+
* @example
|
|
363
|
+
* ```typescript
|
|
364
|
+
* await ok(2).inspectErrAsync(console.log);
|
|
365
|
+
* // Doesn't log
|
|
366
|
+
* ```
|
|
367
|
+
* @example
|
|
368
|
+
* ```typescript
|
|
369
|
+
* await err('Some error message').inspectErrAsync(console.log);
|
|
370
|
+
* // Logs: Some error message
|
|
371
|
+
* ```
|
|
372
|
+
*
|
|
373
|
+
* @note This is an extension not supported in Rust
|
|
374
|
+
*/
|
|
375
|
+
async inspectErrAsync(cb) {
|
|
376
|
+
if (this.isErr()) await cb(this[ValueProperty]);
|
|
377
|
+
return this;
|
|
84
378
|
}
|
|
379
|
+
/**
|
|
380
|
+
* Returns an iterator over the possibly contained value.
|
|
381
|
+
*
|
|
382
|
+
* The iterator yields one value if the result is `Ok`, otherwise none.
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* ```typescript
|
|
386
|
+
* const x = ok(7);
|
|
387
|
+
* for (const value of x.iter()) {
|
|
388
|
+
* console.log(value);
|
|
389
|
+
* }
|
|
390
|
+
* // Logs 7
|
|
391
|
+
* ```
|
|
392
|
+
* @example
|
|
393
|
+
* ```typescript
|
|
394
|
+
* const x = err('Nothing!');
|
|
395
|
+
* for (const value of x.iter()) {
|
|
396
|
+
* console.log(value);
|
|
397
|
+
* }
|
|
398
|
+
* // Doesn't log
|
|
399
|
+
* ```
|
|
400
|
+
*
|
|
401
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.iter}
|
|
402
|
+
*/
|
|
85
403
|
*iter() {
|
|
86
|
-
yield this
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
404
|
+
if (this.isOk()) yield this[ValueProperty];
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Returns the contained `Ok` value.
|
|
408
|
+
*
|
|
409
|
+
* If the value is an `Err`, it throws a {@link ResultError} with the given message and the content of the `Err`.
|
|
410
|
+
* @param message The message for the error.
|
|
411
|
+
*
|
|
412
|
+
* @example
|
|
413
|
+
* ```typescript
|
|
414
|
+
* const x = ok(2);
|
|
415
|
+
* assert.equal(x.expect('Whoops!'), 2);
|
|
416
|
+
* ```
|
|
417
|
+
* @example
|
|
418
|
+
* ```typescript
|
|
419
|
+
* const x = err('Emergency failure');
|
|
420
|
+
* assert.throws(() => x.expect('Whoops!'), {
|
|
421
|
+
* name: 'ResultError',
|
|
422
|
+
* message: 'Whoops',
|
|
423
|
+
* value: 'Emergency failure'
|
|
424
|
+
* });
|
|
425
|
+
* ```
|
|
426
|
+
*
|
|
427
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.expect}
|
|
428
|
+
*/
|
|
429
|
+
expect(message) {
|
|
430
|
+
if (this.isErr()) throw new ResultError(message, this[ValueProperty]);
|
|
431
|
+
return this[ValueProperty];
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Returns the contained `Err` value.
|
|
435
|
+
*
|
|
436
|
+
* If the value is an `Ok`, it throws a {@link ResultError} with the given message and the content of the `Ok`.
|
|
437
|
+
* @param message The message for the error.
|
|
438
|
+
*
|
|
439
|
+
* @example
|
|
440
|
+
* ```typescript
|
|
441
|
+
* const x = ok(2);
|
|
442
|
+
* assert.throws(() => x.expectErr('Whoops!'), {
|
|
443
|
+
* name: 'ResultError',
|
|
444
|
+
* message: 'Whoops',
|
|
445
|
+
* value: 2
|
|
446
|
+
* });
|
|
447
|
+
* ```
|
|
448
|
+
* @example
|
|
449
|
+
* ```typescript
|
|
450
|
+
* const x = err('Emergency failure');
|
|
451
|
+
* assert.equal(x.expectErr('Whoops!'), 'Emergency failure');
|
|
452
|
+
* ```
|
|
453
|
+
*
|
|
454
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.expect_err}
|
|
455
|
+
*/
|
|
91
456
|
expectErr(message) {
|
|
92
|
-
throw new ResultError(message, this
|
|
93
|
-
|
|
457
|
+
if (this.isOk()) throw new ResultError(message, this[ValueProperty]);
|
|
458
|
+
return this[ValueProperty];
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Returns the contained `Ok` value.
|
|
462
|
+
*
|
|
463
|
+
* If the value is an `Err`, it throws a {@link ResultError} with the message, and the content of the `Err`.
|
|
464
|
+
* @seealso {@link unwrapOr}
|
|
465
|
+
* @seealso {@link unwrapOrElse}
|
|
466
|
+
* @seealso {@link unwrapErr}
|
|
467
|
+
* @seealso {@link unwrapRaw}
|
|
468
|
+
*
|
|
469
|
+
* @example
|
|
470
|
+
* ```typescript
|
|
471
|
+
* const x = ok(2);
|
|
472
|
+
* assert.equal(x.unwrap(), 2);
|
|
473
|
+
* ```
|
|
474
|
+
* @example
|
|
475
|
+
* ```typescript
|
|
476
|
+
* const x = err('Emergency failure');
|
|
477
|
+
* assert.throws(() => x.unwrap(), {
|
|
478
|
+
* name: 'ResultError',
|
|
479
|
+
* message: 'Unwrap failed',
|
|
480
|
+
* value: 'Emergency failure'
|
|
481
|
+
* });
|
|
482
|
+
* ```
|
|
483
|
+
*
|
|
484
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap}
|
|
485
|
+
*/
|
|
94
486
|
unwrap() {
|
|
95
|
-
|
|
96
|
-
|
|
487
|
+
if (this.isErr()) throw new ResultError("Unwrap failed", this[ValueProperty]);
|
|
488
|
+
return this[ValueProperty];
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Returns the contained `Err` value.
|
|
492
|
+
*
|
|
493
|
+
* If the value is an `Ok`, it throws a {@link ResultError} with the message, and the content of the `Ok`.
|
|
494
|
+
* @seealso {@link unwrap}
|
|
495
|
+
* @seealso {@link unwrapOr}
|
|
496
|
+
* @seealso {@link unwrapOrElse}
|
|
497
|
+
* @seealso {@link unwrapRaw}
|
|
498
|
+
*
|
|
499
|
+
* @example
|
|
500
|
+
* ```typescript
|
|
501
|
+
* const x = ok(2);
|
|
502
|
+
* assert.throws(() => x.unwrapErr(), {
|
|
503
|
+
* name: 'ResultError',
|
|
504
|
+
* message: 'Unwrap failed',
|
|
505
|
+
* value: 2
|
|
506
|
+
* });
|
|
507
|
+
* ```
|
|
508
|
+
* @example
|
|
509
|
+
* ```typescript
|
|
510
|
+
* const x = err('Emergency failure');
|
|
511
|
+
* assert.equal(x.unwrapErr(), 'Emergency failure');
|
|
512
|
+
* ```
|
|
513
|
+
*
|
|
514
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_err}
|
|
515
|
+
*/
|
|
97
516
|
unwrapErr() {
|
|
98
|
-
throw new ResultError("Unwrap failed", this
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
517
|
+
if (this.isOk()) throw new ResultError("Unwrap failed", this[ValueProperty]);
|
|
518
|
+
return this[ValueProperty];
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Returns the contained `Ok` value or the provided default.
|
|
522
|
+
*
|
|
523
|
+
* Arguments passed to `unwrapOr` are eagerly evaluated; if you are passing the result of a function call, it is
|
|
524
|
+
* recommended to use {@link unwrapOrElse}, which is lazily evaluated.
|
|
525
|
+
* @seealso {@link unwrap}
|
|
526
|
+
* @seealso {@link unwrapOrElse}
|
|
527
|
+
* @seealso {@link unwrapErr}
|
|
528
|
+
* @seealso {@link unwrapRaw}
|
|
529
|
+
*
|
|
530
|
+
* @param defaultValue The default value.
|
|
531
|
+
*
|
|
532
|
+
* @example
|
|
533
|
+
* ```typescript
|
|
534
|
+
* const x: Result<number, string> = ok(9);
|
|
535
|
+
* assert.equal(x.unwrapOr(2), 9);
|
|
536
|
+
* ```
|
|
537
|
+
* @example
|
|
538
|
+
* ```typescript
|
|
539
|
+
* const x: Result<number, string> = err('Error');
|
|
540
|
+
* assert.equal(x.unwrapOr(2), 2);
|
|
541
|
+
* ```
|
|
542
|
+
*
|
|
543
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or}
|
|
544
|
+
*/
|
|
545
|
+
unwrapOr(defaultValue) {
|
|
546
|
+
return this.match({ ok: /* @__PURE__ */ __name((value) => value, "ok"), err: /* @__PURE__ */ __name(() => defaultValue, "err") });
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Returns the contained `Ok` value or computes it from a closure.
|
|
550
|
+
* @seealso {@link unwrap}
|
|
551
|
+
* @seealso {@link unwrapOr}
|
|
552
|
+
* @seealso {@link unwrapErr}
|
|
553
|
+
* @seealso {@link unwrapRaw}
|
|
554
|
+
*
|
|
555
|
+
* @param op The predicate.
|
|
556
|
+
*
|
|
557
|
+
* @example
|
|
558
|
+
* ```typescript
|
|
559
|
+
* const count = (x: string) => x.length;
|
|
560
|
+
*
|
|
561
|
+
* assert.equal(ok(2).unwrapOrElse(count), 2);
|
|
562
|
+
* assert.equal(err('hello').unwrapOrElse(count), 5);
|
|
563
|
+
* ```
|
|
564
|
+
*
|
|
565
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or_else}
|
|
566
|
+
*/
|
|
567
|
+
unwrapOrElse(op) {
|
|
568
|
+
return this.match({ ok: /* @__PURE__ */ __name((value) => value, "ok"), err: /* @__PURE__ */ __name((error) => op(error), "err") });
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Returns the contained `Ok` value.
|
|
572
|
+
*
|
|
573
|
+
* If the value is an `Err`, it throws the contained error.
|
|
574
|
+
* @seealso {@link unwrap}
|
|
575
|
+
* @seealso {@link unwrapOr}
|
|
576
|
+
* @seealso {@link unwrapOrElse}
|
|
577
|
+
* @seealso {@link unwrapErr}
|
|
578
|
+
*
|
|
579
|
+
* @example
|
|
580
|
+
* ```typescript
|
|
581
|
+
* const x = ok(2);
|
|
582
|
+
* assert.equal(x.unwrapRaw(), 2);
|
|
583
|
+
* ```
|
|
584
|
+
* @example
|
|
585
|
+
* ```typescript
|
|
586
|
+
* const x = err('Emergency failure');
|
|
587
|
+
* assert.throws(() => x.unwrapRaw(), {
|
|
588
|
+
* name: 'Error',
|
|
589
|
+
* message: 'Unwrap failed',
|
|
590
|
+
* value: 'Emergency failure'
|
|
591
|
+
* });
|
|
592
|
+
* ```
|
|
593
|
+
*/
|
|
106
594
|
unwrapRaw() {
|
|
107
|
-
|
|
108
|
-
|
|
595
|
+
if (this.isErr()) throw this[ValueProperty];
|
|
596
|
+
return this[ValueProperty];
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Returns `result` if the result is `Ok`, otherwise returns the `Err` value of itself.
|
|
600
|
+
* @param result The result to check.
|
|
601
|
+
*
|
|
602
|
+
* @example
|
|
603
|
+
* ```typescript
|
|
604
|
+
* const x: Result<number, string> = ok(2);
|
|
605
|
+
* const y: Result<string, string> = err('Late error');
|
|
606
|
+
* assert.equal(x.and(y), err('Late error'));
|
|
607
|
+
* ```
|
|
608
|
+
* @example
|
|
609
|
+
* ```typescript
|
|
610
|
+
* const x: Result<number, string> = err('Early error');
|
|
611
|
+
* const y: Result<string, string> = err('Late error');
|
|
612
|
+
* assert.equal(x.and(y), err('Early error'));
|
|
613
|
+
* ```
|
|
614
|
+
* @example
|
|
615
|
+
* ```typescript
|
|
616
|
+
* const x: Result<number, string> = ok(2);
|
|
617
|
+
* const y: Result<string, string> = ok('Hello');
|
|
618
|
+
* assert.equal(x.and(y), ok('Hello'));
|
|
619
|
+
* ```
|
|
620
|
+
*
|
|
621
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and}
|
|
622
|
+
*/
|
|
109
623
|
and(result) {
|
|
110
|
-
return result;
|
|
111
|
-
}
|
|
624
|
+
return this.match({ ok: /* @__PURE__ */ __name(() => result, "ok"), err: returnThis });
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
|
|
628
|
+
*
|
|
629
|
+
* This function can be used for control flow based on `Result` values.
|
|
630
|
+
* @param cb The predicate.
|
|
631
|
+
*
|
|
632
|
+
* @example
|
|
633
|
+
* ```typescript
|
|
634
|
+
* function fractionOf4(value: number) {
|
|
635
|
+
* return value === 0 ? err('overflowed') : ok(4 / value);
|
|
636
|
+
* }
|
|
637
|
+
*
|
|
638
|
+
* assert.equal(ok(2).andThen(fractionOf4), ok(4));
|
|
639
|
+
* assert.equal(ok(0).andThen(fractionOf4), err('overflowed'));
|
|
640
|
+
* assert.equal(err('not a number').andThen(fractionOf4), err('not a number'));
|
|
641
|
+
* ```
|
|
642
|
+
*
|
|
643
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then}
|
|
644
|
+
*/
|
|
112
645
|
andThen(cb) {
|
|
113
|
-
return
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
646
|
+
return this.match({ ok: /* @__PURE__ */ __name((value) => cb(value), "ok"), err: returnThis });
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Return `result` if the result is `Err`, otherwise returns the `Ok` value of self.
|
|
650
|
+
*
|
|
651
|
+
* Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended
|
|
652
|
+
* to use {@link orElse}, which is lazily evaluated.
|
|
653
|
+
* @param result The result to check.
|
|
654
|
+
*
|
|
655
|
+
* @example
|
|
656
|
+
* ```typescript
|
|
657
|
+
* const x: Result<number, string> = ok(2);
|
|
658
|
+
* const y: Result<number, string> = err('Late error');
|
|
659
|
+
* assert.equal(x.or(y), ok(2));
|
|
660
|
+
* ```
|
|
661
|
+
* @example
|
|
662
|
+
* ```typescript
|
|
663
|
+
* const x: Result<number, string> = err('Early error');
|
|
664
|
+
* const y: Result<number, string> = ok(2);
|
|
665
|
+
* assert.equal(x.or(y), ok(2));
|
|
666
|
+
* ```
|
|
667
|
+
* @example
|
|
668
|
+
* ```typescript
|
|
669
|
+
* const x: Result<number, string> = err('Early error');
|
|
670
|
+
* const y: Result<number, string> = err('Late error');
|
|
671
|
+
* assert.equal(x.or(y), err('Late error'));
|
|
672
|
+
* ```
|
|
673
|
+
* @example
|
|
674
|
+
* ```typescript
|
|
675
|
+
* const x: Result<number, string> = ok(2);
|
|
676
|
+
* const y: Result<number, string> = ok(100);
|
|
677
|
+
* assert.equal(x.or(y), ok(2));
|
|
678
|
+
* ```
|
|
679
|
+
*
|
|
680
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.or}
|
|
681
|
+
*/
|
|
682
|
+
or(result) {
|
|
683
|
+
return this.match({ ok: returnThis, err: /* @__PURE__ */ __name(() => result, "err") });
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Calls `cb` if the result is `Err`, otherwise returns the `Ok` value of self.
|
|
687
|
+
*
|
|
688
|
+
* This function can be used for control flow based on result values.
|
|
689
|
+
* @param cb The predicate.
|
|
690
|
+
*
|
|
691
|
+
* @example
|
|
692
|
+
* ```typescript
|
|
693
|
+
* const square = (x: number): Result<number, string> => ok(x * x);
|
|
694
|
+
* const wrapErr = (x: number): Result<number, string> => err(x);
|
|
695
|
+
*
|
|
696
|
+
* assert.equal(ok(2).orElse(square).orElse(square), ok(2));
|
|
697
|
+
* assert.equal(ok(2).orElse(wrapErr).orElse(square), ok(2));
|
|
698
|
+
* assert.equal(err(3).orElse(square).orElse(wrapErr), ok(9));
|
|
699
|
+
* assert.equal(err(3).orElse(wrapErr).orElse(wrapErr), err(3));
|
|
700
|
+
* ```
|
|
701
|
+
*
|
|
702
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.or_else}
|
|
703
|
+
*/
|
|
704
|
+
orElse(cb) {
|
|
705
|
+
return this.match({ ok: returnThis, err: /* @__PURE__ */ __name((error) => cb(error), "err") });
|
|
120
706
|
}
|
|
121
707
|
contains(value) {
|
|
122
|
-
return this.
|
|
123
|
-
}
|
|
124
|
-
containsErr() {
|
|
125
|
-
return false;
|
|
708
|
+
return this.isOkAnd((inner) => inner === value);
|
|
126
709
|
}
|
|
710
|
+
containsErr(error) {
|
|
711
|
+
return this.isErrAnd((inner) => inner === error);
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Transposes a `Result` of an `Option` into an `Option` of a `Result`.
|
|
715
|
+
*
|
|
716
|
+
* `ok(none)` will be mapped to `none`. `ok(some(v))` and `err(e)` will be mapped to `some(ok(v))` and `some(err(e))`.
|
|
717
|
+
*
|
|
718
|
+
* @example
|
|
719
|
+
* ```typescript
|
|
720
|
+
* const x: Result<Option<number>, Error> = ok(some(5));
|
|
721
|
+
* const y: Option<Result<number, Error>> = some(ok(5));
|
|
722
|
+
* assert.equal(x.transpose(), y);
|
|
723
|
+
* ```
|
|
724
|
+
*
|
|
725
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.transpose}
|
|
726
|
+
*/
|
|
127
727
|
transpose() {
|
|
128
|
-
return this.
|
|
129
|
-
|
|
130
|
-
|
|
728
|
+
return this.match({
|
|
729
|
+
ok: /* @__PURE__ */ __name((value) => value.map((value2) => ok(value2)), "ok"),
|
|
730
|
+
err() {
|
|
731
|
+
return some(this);
|
|
732
|
+
}
|
|
131
733
|
});
|
|
132
734
|
}
|
|
735
|
+
/**
|
|
736
|
+
* Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
|
|
737
|
+
*
|
|
738
|
+
* @example
|
|
739
|
+
* ```typescript
|
|
740
|
+
* const x: Result<Result<string, number>, number> = ok(ok('Hello'));
|
|
741
|
+
* assert.equal(x.flatten(), ok('Hello'));
|
|
742
|
+
* ```
|
|
743
|
+
* @example
|
|
744
|
+
* ```typescript
|
|
745
|
+
* const x: Result<Result<string, number>, number> = ok(err(6));
|
|
746
|
+
* assert.equal(x.flatten(), err(6));
|
|
747
|
+
* ```
|
|
748
|
+
* @example
|
|
749
|
+
* ```typescript
|
|
750
|
+
* const x: Result<Result<string, number>, number> = err(6);
|
|
751
|
+
* assert.equal(x.flatten(), err(6));
|
|
752
|
+
* ```
|
|
753
|
+
*
|
|
754
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten}
|
|
755
|
+
*/
|
|
133
756
|
flatten() {
|
|
134
|
-
return this.value;
|
|
135
|
-
}
|
|
757
|
+
return this.match({ ok: /* @__PURE__ */ __name((value) => value, "ok"), err: returnThis });
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* Returns the `Ok` value if self is `Ok`, and the `Err` value if self is `Err`.
|
|
761
|
+
*
|
|
762
|
+
* @example
|
|
763
|
+
* ```typescript
|
|
764
|
+
* let x: Result<number, number> = ok(3);
|
|
765
|
+
* assert.equal(x.intoOkOrErr(), 3);
|
|
766
|
+
* ```
|
|
767
|
+
* @example
|
|
768
|
+
* ```typescript
|
|
769
|
+
* let x: Result<number, number> = err(4);
|
|
770
|
+
* assert.equal(x.intoOkOrErr(), 4);
|
|
771
|
+
* ```
|
|
772
|
+
*
|
|
773
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.into_ok_or_err}
|
|
774
|
+
*/
|
|
136
775
|
intoOkOrErr() {
|
|
137
|
-
return this
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
776
|
+
return this[ValueProperty];
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Returns a `Promise` object with the awaited value (if `Ok`) or the awaited error (if `Err`).
|
|
780
|
+
*
|
|
781
|
+
* @example
|
|
782
|
+
* ```typescript
|
|
783
|
+
* let x = ok(Promise.resolve(3));
|
|
784
|
+
* assert.equal(await x.intoPromise(), ok(3));
|
|
785
|
+
* ```
|
|
786
|
+
*
|
|
787
|
+
* @note This is an extension not supported in Rust
|
|
788
|
+
*/
|
|
789
|
+
intoPromise() {
|
|
790
|
+
return this.match({
|
|
791
|
+
ok: /* @__PURE__ */ __name(async (value) => ok(await value), "ok"),
|
|
792
|
+
// NOSONAR
|
|
793
|
+
err: /* @__PURE__ */ __name(async (error) => err(await error), "err")
|
|
794
|
+
// NOSONAR
|
|
795
|
+
});
|
|
141
796
|
}
|
|
797
|
+
/**
|
|
798
|
+
* Checks whether or not `other` equals with self.
|
|
799
|
+
* @param other The other result to compare.
|
|
800
|
+
*
|
|
801
|
+
* @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq}
|
|
802
|
+
*/
|
|
142
803
|
eq(other) {
|
|
143
|
-
return other.
|
|
144
|
-
}
|
|
804
|
+
return this.isOk() === other.isOk() && this[ValueProperty] === other[ValueProperty];
|
|
805
|
+
}
|
|
806
|
+
/**
|
|
807
|
+
* Checks whether or not `other` doesn't equal with self.
|
|
808
|
+
* @param other The other result to compare.
|
|
809
|
+
*
|
|
810
|
+
* @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#method.ne}
|
|
811
|
+
*/
|
|
145
812
|
ne(other) {
|
|
146
813
|
return !this.eq(other);
|
|
147
814
|
}
|
|
815
|
+
/**
|
|
816
|
+
* Runs `ok` function if self is `Ok`, otherwise runs `err` function.
|
|
817
|
+
* @param branches The branches to match.
|
|
818
|
+
*
|
|
819
|
+
* @example
|
|
820
|
+
* ```typescript
|
|
821
|
+
* const result = ok(4).match({
|
|
822
|
+
* ok: (v) => v,
|
|
823
|
+
* err: () => 0
|
|
824
|
+
* });
|
|
825
|
+
* assert.equal(result, 4);
|
|
826
|
+
* ```
|
|
827
|
+
* @example
|
|
828
|
+
* ```typescript
|
|
829
|
+
* const result = err('Hello').match({
|
|
830
|
+
* ok: (v) => v,
|
|
831
|
+
* err: () => 0
|
|
832
|
+
* });
|
|
833
|
+
* assert.equal(result, 0);
|
|
834
|
+
* ```
|
|
835
|
+
*/
|
|
148
836
|
match(branches) {
|
|
149
|
-
return branches.ok(this.
|
|
837
|
+
return this.isOk() ? branches.ok.call(this, this[ValueProperty]) : branches.err.call(this, this[ValueProperty]);
|
|
838
|
+
}
|
|
839
|
+
/**
|
|
840
|
+
* Returns an iterator over the possibly contained value.
|
|
841
|
+
*
|
|
842
|
+
* The iterator yields one value if the result is `Ok`, otherwise none.
|
|
843
|
+
*
|
|
844
|
+
* @example
|
|
845
|
+
* ```typescript
|
|
846
|
+
* const x = ok(7);
|
|
847
|
+
* for (const value of x) {
|
|
848
|
+
* console.log(value);
|
|
849
|
+
* }
|
|
850
|
+
* // Logs 7
|
|
851
|
+
* ```
|
|
852
|
+
* @example
|
|
853
|
+
* ```typescript
|
|
854
|
+
* const x = err('Nothing!');
|
|
855
|
+
* for (const value of x) {
|
|
856
|
+
* console.log(value);
|
|
857
|
+
* }
|
|
858
|
+
* // Doesn't log
|
|
859
|
+
* ```
|
|
860
|
+
*
|
|
861
|
+
* @see {@link IResult.iter}
|
|
862
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.iter}
|
|
863
|
+
*/
|
|
864
|
+
[(_b = ValueProperty, _a = SuccessProperty, Symbol.iterator)]() {
|
|
865
|
+
return this.iter();
|
|
866
|
+
}
|
|
867
|
+
get [Symbol.toStringTag]() {
|
|
868
|
+
return this.match({ ok: /* @__PURE__ */ __name(() => "Ok", "ok"), err: /* @__PURE__ */ __name(() => "Err", "err") });
|
|
869
|
+
}
|
|
870
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
871
|
+
static ok(value) {
|
|
872
|
+
return new _Result(value, true);
|
|
873
|
+
}
|
|
874
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
875
|
+
static err(value) {
|
|
876
|
+
return new _Result(value, false);
|
|
877
|
+
}
|
|
878
|
+
/**
|
|
879
|
+
* Checks if the `instance` object is an instance of `Result`, or if it is a `Result`-like object. This override
|
|
880
|
+
* exists to interoperate with other versions of this class, such as the one coming from another version of this
|
|
881
|
+
* library or from a different build.
|
|
882
|
+
*
|
|
883
|
+
* @param instance The instance to check.
|
|
884
|
+
* @returns Whether or not the instance is a `Result`.
|
|
885
|
+
*
|
|
886
|
+
* @example
|
|
887
|
+
* ```typescript
|
|
888
|
+
* import { Result } from '@sapphire/result';
|
|
889
|
+
* const { ok } = require('@sapphire/result');
|
|
890
|
+
*
|
|
891
|
+
* ok(2) instanceof Result; // true
|
|
892
|
+
* ```
|
|
893
|
+
*/
|
|
894
|
+
static [Symbol.hasInstance](instance) {
|
|
895
|
+
return typeof instance === "object" && instance !== null && ValueProperty in instance && SuccessProperty in instance;
|
|
896
|
+
}
|
|
897
|
+
/**
|
|
898
|
+
* @deprecated Use {@link Result.isResult} instead.
|
|
899
|
+
*
|
|
900
|
+
* Checks if the `instance` object is an instance of `Result`, or if it is a `Result`-like object.
|
|
901
|
+
*
|
|
902
|
+
* @param instance The instance to check.
|
|
903
|
+
* @returns true if the instance is a `Result` or a `Result`-like object, false otherwise.
|
|
904
|
+
*
|
|
905
|
+
* @example
|
|
906
|
+
* ```typescript
|
|
907
|
+
* import { Result } from '@sapphire/result';
|
|
908
|
+
* const { ok } = require('@sapphire/result');
|
|
909
|
+
*
|
|
910
|
+
* Result.isResult(ok(2)); // true
|
|
911
|
+
* ```
|
|
912
|
+
*/
|
|
913
|
+
static is(instance) {
|
|
914
|
+
return _Result[Symbol.hasInstance](instance);
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* Checks if the `instance` object is an instance of `Result`, or if it is a `Result`-like object.
|
|
918
|
+
*
|
|
919
|
+
* @param instance The instance to check.
|
|
920
|
+
* @returns true if the instance is a `Result` or a `Result`-like object, false otherwise.
|
|
921
|
+
*
|
|
922
|
+
* @example
|
|
923
|
+
* ```typescript
|
|
924
|
+
* import { Result } from '@sapphire/result';
|
|
925
|
+
* const { ok } = require('@sapphire/result');
|
|
926
|
+
*
|
|
927
|
+
* Result.isResult(ok(2)); // true
|
|
928
|
+
* ```
|
|
929
|
+
*/
|
|
930
|
+
static isResult(instance) {
|
|
931
|
+
return _Result[Symbol.hasInstance](instance);
|
|
932
|
+
}
|
|
933
|
+
/**
|
|
934
|
+
* Creates a {@link Result} out of a callback.
|
|
935
|
+
*
|
|
936
|
+
* @typeparam T The result's type.
|
|
937
|
+
* @typeparam E The error's type.
|
|
938
|
+
*/
|
|
939
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
940
|
+
static from(op) {
|
|
941
|
+
try {
|
|
942
|
+
return resolve(isFunction(op) ? op() : op);
|
|
943
|
+
} catch (error) {
|
|
944
|
+
return err(error);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
/**
|
|
948
|
+
* Creates a {@link Result} out of a promise or async callback.
|
|
949
|
+
*
|
|
950
|
+
* @typeparam T The result's type.
|
|
951
|
+
* @typeparam E The error's type.
|
|
952
|
+
*/
|
|
953
|
+
static async fromAsync(op) {
|
|
954
|
+
try {
|
|
955
|
+
return resolve(await (isFunction(op) ? op() : op));
|
|
956
|
+
} catch (error) {
|
|
957
|
+
return err(error);
|
|
958
|
+
}
|
|
150
959
|
}
|
|
151
|
-
|
|
152
|
-
|
|
960
|
+
/**
|
|
961
|
+
* Creates an {@link Ok} that is the combination of all collected {@link Ok} values as an array, or the first
|
|
962
|
+
* {@link Err} encountered.
|
|
963
|
+
*
|
|
964
|
+
* @param results An array of {@link Result}s.
|
|
965
|
+
* @returns A new {@link Result}.
|
|
966
|
+
*/
|
|
967
|
+
static all(results) {
|
|
968
|
+
const values = [];
|
|
969
|
+
for (const result of results) {
|
|
970
|
+
if (result.isErr()) return result;
|
|
971
|
+
values.push(result[ValueProperty]);
|
|
972
|
+
}
|
|
973
|
+
return ok(values);
|
|
974
|
+
}
|
|
975
|
+
/**
|
|
976
|
+
* Returns the first encountered {@link Ok}, or an {@link Err} that is the combination of all collected error values.
|
|
977
|
+
*
|
|
978
|
+
* @param results An array of {@link Result}s.
|
|
979
|
+
* @returns A new {@link Result}.
|
|
980
|
+
*/
|
|
981
|
+
static any(results) {
|
|
982
|
+
const errors = [];
|
|
983
|
+
for (const result of results) {
|
|
984
|
+
if (result.isOk()) return result;
|
|
985
|
+
errors.push(result[ValueProperty]);
|
|
986
|
+
}
|
|
987
|
+
return err(errors);
|
|
153
988
|
}
|
|
154
989
|
};
|
|
155
|
-
__name(
|
|
156
|
-
var
|
|
157
|
-
|
|
158
|
-
|
|
990
|
+
__name(_Result, "Result");
|
|
991
|
+
var Result = _Result;
|
|
992
|
+
var { ok, err } = Result;
|
|
993
|
+
function resolve(value) {
|
|
994
|
+
return Result.isResult(value) ? value : ok(value);
|
|
159
995
|
}
|
|
160
|
-
__name(
|
|
996
|
+
__name(resolve, "resolve");
|
|
161
997
|
|
|
162
|
-
// src/lib/Option
|
|
163
|
-
var
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
998
|
+
// src/lib/Option.ts
|
|
999
|
+
var ValueProperty2 = Symbol.for("@sapphire/result:Option.value");
|
|
1000
|
+
var ExistsProperty = Symbol.for("@sapphire/result:Option.exists");
|
|
1001
|
+
var _a2, _b2;
|
|
1002
|
+
var _Option = class _Option {
|
|
1003
|
+
constructor(value, exists) {
|
|
1004
|
+
__publicField(this, _b2);
|
|
1005
|
+
__publicField(this, _a2);
|
|
1006
|
+
this[ValueProperty2] = value;
|
|
1007
|
+
this[ExistsProperty] = exists;
|
|
1008
|
+
}
|
|
1009
|
+
/**
|
|
1010
|
+
* Returns `true` if the option is a `Some` value.
|
|
1011
|
+
*
|
|
1012
|
+
* @example
|
|
1013
|
+
* ```typescript
|
|
1014
|
+
* const x: Option<number> = some(2);
|
|
1015
|
+
* assert.equal(x.isSome(), true);
|
|
1016
|
+
* ```
|
|
1017
|
+
* @example
|
|
1018
|
+
* ```typescript
|
|
1019
|
+
* const x: Option<number> = none;
|
|
1020
|
+
* assert.equal(x.isSome(), false);
|
|
1021
|
+
* ```
|
|
1022
|
+
*
|
|
1023
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some}
|
|
1024
|
+
*/
|
|
168
1025
|
isSome() {
|
|
169
|
-
return
|
|
1026
|
+
return this[ExistsProperty];
|
|
170
1027
|
}
|
|
171
1028
|
isSomeAnd(cb) {
|
|
172
|
-
return cb(this
|
|
173
|
-
}
|
|
1029
|
+
return this.isSome() && cb(this[ValueProperty2]);
|
|
1030
|
+
}
|
|
1031
|
+
/**
|
|
1032
|
+
* Returns `true` if the option is a `None` value.
|
|
1033
|
+
*
|
|
1034
|
+
* @example
|
|
1035
|
+
* ```typescript
|
|
1036
|
+
* const x: Option<number> = some(2);
|
|
1037
|
+
* assert.equal(x.isNone(), false);
|
|
1038
|
+
* ```
|
|
1039
|
+
* @example
|
|
1040
|
+
* ```typescript
|
|
1041
|
+
* const x: Option<number> = none;
|
|
1042
|
+
* assert.equal(x.isNone(), true);
|
|
1043
|
+
* ```
|
|
1044
|
+
*
|
|
1045
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_none}
|
|
1046
|
+
*/
|
|
174
1047
|
isNone() {
|
|
175
|
-
return
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
1048
|
+
return !this[ExistsProperty];
|
|
1049
|
+
}
|
|
1050
|
+
/**
|
|
1051
|
+
* Returns the contained `Some` value.
|
|
1052
|
+
* @param message The message for the error.
|
|
1053
|
+
* If the value is an `Err`, it throws an {@link OptionError} with the given message.
|
|
1054
|
+
*
|
|
1055
|
+
* @example
|
|
1056
|
+
* ```typescript
|
|
1057
|
+
* const x: Option<string> = some(2);
|
|
1058
|
+
* assert.equal(x.expect('Whoops!'), 2);
|
|
1059
|
+
* ```
|
|
1060
|
+
* @example
|
|
1061
|
+
* ```typescript
|
|
1062
|
+
* const x: Option<string> = none;
|
|
1063
|
+
* assert.throws(() => x.expect('Whoops!'), {
|
|
1064
|
+
* name: 'OptionError',
|
|
1065
|
+
* message: 'Whoops'
|
|
1066
|
+
* });
|
|
1067
|
+
* ```
|
|
1068
|
+
*
|
|
1069
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.expect}
|
|
1070
|
+
*/
|
|
1071
|
+
expect(message) {
|
|
1072
|
+
if (this.isNone()) throw new OptionError(message);
|
|
1073
|
+
return this[ValueProperty2];
|
|
1074
|
+
}
|
|
1075
|
+
/**
|
|
1076
|
+
* Returns the contained `Some` value.
|
|
1077
|
+
*
|
|
1078
|
+
* If the value is an `Err`, it throws an {@link OptionError} with the message.
|
|
1079
|
+
* @seealso {@link unwrapOr}
|
|
1080
|
+
* @seealso {@link unwrapOrElse}
|
|
1081
|
+
*
|
|
1082
|
+
* @example
|
|
1083
|
+
* ```typescript
|
|
1084
|
+
* const x: Option<string> = some(2);
|
|
1085
|
+
* assert.equal(x.unwrap(), 2);
|
|
1086
|
+
* ```
|
|
1087
|
+
* @example
|
|
1088
|
+
* ```typescript
|
|
1089
|
+
* const x: Option<string> = none;
|
|
1090
|
+
* assert.throws(() => x.unwrap(), {
|
|
1091
|
+
* name: 'OptionError',
|
|
1092
|
+
* message: 'Unwrap failed'
|
|
1093
|
+
* });
|
|
1094
|
+
* ```
|
|
1095
|
+
*
|
|
1096
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap}
|
|
1097
|
+
*/
|
|
180
1098
|
unwrap() {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
1099
|
+
if (this.isNone()) throw new OptionError("Unwrap failed");
|
|
1100
|
+
return this[ValueProperty2];
|
|
1101
|
+
}
|
|
1102
|
+
/**
|
|
1103
|
+
* Returns the contained `Some` value or a provided default.
|
|
1104
|
+
*
|
|
1105
|
+
* Arguments passed to `unwrapOr` are eagerly evaluated; if you are passing the result of a function call, it is
|
|
1106
|
+
* recommended to use {@link unwrapOrElse}, which is lazily evaluated.
|
|
1107
|
+
*
|
|
1108
|
+
* @example
|
|
1109
|
+
* ```typescript
|
|
1110
|
+
* assert.equal(some(2).unwrapOr(0), 2);
|
|
1111
|
+
* ```
|
|
1112
|
+
* @example
|
|
1113
|
+
* ```typescript
|
|
1114
|
+
* assert.equal(none.unwrapOr(0), 0);
|
|
1115
|
+
* ```
|
|
1116
|
+
*
|
|
1117
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or}
|
|
1118
|
+
*/
|
|
1119
|
+
unwrapOr(defaultValue) {
|
|
1120
|
+
return this.match({ some: /* @__PURE__ */ __name((value) => value, "some"), none: /* @__PURE__ */ __name(() => defaultValue, "none") });
|
|
1121
|
+
}
|
|
1122
|
+
/**
|
|
1123
|
+
* Returns the contained Some value or computes it from a closure.
|
|
1124
|
+
*
|
|
1125
|
+
* @example
|
|
1126
|
+
* ```typescript
|
|
1127
|
+
* assert.equal(some(2).unwrapOrElse(() => 0), 2);
|
|
1128
|
+
* ```
|
|
1129
|
+
* @example
|
|
1130
|
+
* ```typescript
|
|
1131
|
+
* assert.equal(none.unwrapOrElse(() => 0), 0);
|
|
1132
|
+
* ```
|
|
1133
|
+
*
|
|
1134
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or_else}
|
|
1135
|
+
*/
|
|
1136
|
+
unwrapOrElse(cb) {
|
|
1137
|
+
return this.match({ some: /* @__PURE__ */ __name((value) => value, "some"), none: cb });
|
|
1138
|
+
}
|
|
1139
|
+
/**
|
|
1140
|
+
* Maps an `Option<T>` to `Option<U>` by applying a function to a contained value.
|
|
1141
|
+
* @param cb The predicate.
|
|
1142
|
+
*
|
|
1143
|
+
* @example
|
|
1144
|
+
* ```typescript
|
|
1145
|
+
* const maybeSomeString = some('Hello, world!');
|
|
1146
|
+
* const maybeSomeLength = maybeSomeString.map((value) => value.length);
|
|
1147
|
+
*
|
|
1148
|
+
* assert.equal(maybeSomeLength, some(13));
|
|
1149
|
+
* ```
|
|
1150
|
+
*
|
|
1151
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map}
|
|
1152
|
+
*/
|
|
189
1153
|
map(cb) {
|
|
190
|
-
return
|
|
191
|
-
}
|
|
1154
|
+
return this.match({ some: /* @__PURE__ */ __name((value) => some(cb(value)), "some"), none: returnThis });
|
|
1155
|
+
}
|
|
1156
|
+
/**
|
|
1157
|
+
* Maps a `Some<T>` to the returned `Option<U>` by applying a function to a contained value, leaving `None`
|
|
1158
|
+
* untouched.
|
|
1159
|
+
* @param cb The predicate.
|
|
1160
|
+
*
|
|
1161
|
+
* @example
|
|
1162
|
+
* ```typescript
|
|
1163
|
+
* const input: Option<string> = some('Hello, world!');
|
|
1164
|
+
* const result = input.mapInto((value) => some(value.length));
|
|
1165
|
+
*
|
|
1166
|
+
* assert.equal(result, some(13));
|
|
1167
|
+
* ```
|
|
1168
|
+
* @example
|
|
1169
|
+
* ```typescript
|
|
1170
|
+
* const input: Option<string> = none;
|
|
1171
|
+
* const result = input.mapInto((value) => some(value.length));
|
|
1172
|
+
*
|
|
1173
|
+
* assert.equal(result, none);
|
|
1174
|
+
* ```
|
|
1175
|
+
*
|
|
1176
|
+
* @note This is an extension not supported in Rust
|
|
1177
|
+
*/
|
|
192
1178
|
mapInto(cb) {
|
|
193
|
-
return
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
1179
|
+
return this.match({ some: /* @__PURE__ */ __name((value) => cb(value), "some"), none: returnThis });
|
|
1180
|
+
}
|
|
1181
|
+
/**
|
|
1182
|
+
* Returns the provided default result (if none), or applies a function to the contained value (if any).
|
|
1183
|
+
*
|
|
1184
|
+
* Arguments passed to `mapOr` are eagerly evaluated; if you are passing the result of a function call, it is
|
|
1185
|
+
* recommended to use {@link mapOrElse}, which is lazily evaluated.
|
|
1186
|
+
* @param defaultValue The default value.
|
|
1187
|
+
* @param cb The predicate.
|
|
1188
|
+
*
|
|
1189
|
+
* @example
|
|
1190
|
+
* ```typescript
|
|
1191
|
+
* const x: Option<string> = some('hello');
|
|
1192
|
+
* assert.equal(x.mapOr(42, (value) => value.length), 5);
|
|
1193
|
+
* ```
|
|
1194
|
+
* @example
|
|
1195
|
+
* ```typescript
|
|
1196
|
+
* const x: Option<string> = none;
|
|
1197
|
+
* assert.equal(x.mapOr(42, (value) => value.length), 42);
|
|
1198
|
+
* ```
|
|
1199
|
+
*
|
|
1200
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or}
|
|
1201
|
+
*/
|
|
1202
|
+
mapOr(defaultValue, cb) {
|
|
1203
|
+
return this.match({ some: /* @__PURE__ */ __name((value) => cb(value), "some"), none: /* @__PURE__ */ __name(() => defaultValue, "none") });
|
|
1204
|
+
}
|
|
1205
|
+
/**
|
|
1206
|
+
* Computes a default function result (if none), or applies a different function to the contained value (if any).
|
|
1207
|
+
* @param defaultValue The default value.
|
|
1208
|
+
* @param cb The predicate.
|
|
1209
|
+
*
|
|
1210
|
+
* @example
|
|
1211
|
+
* ```typescript
|
|
1212
|
+
* const x: Option<string> = some('hello');
|
|
1213
|
+
* assert.equal(x.mapOrElse(() => 42, (value) => value.length), 5);
|
|
1214
|
+
* ```
|
|
1215
|
+
* @example
|
|
1216
|
+
* ```typescript
|
|
1217
|
+
* const x: Option<string> = none;
|
|
1218
|
+
* assert.equal(x.mapOrElse(() => 42, (value) => value.length), 42);
|
|
1219
|
+
* ```
|
|
1220
|
+
*
|
|
1221
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or_else}
|
|
1222
|
+
*/
|
|
1223
|
+
mapOrElse(defaultValue, cb) {
|
|
1224
|
+
return this.match({ some: /* @__PURE__ */ __name((value) => cb(value), "some"), none: /* @__PURE__ */ __name(() => defaultValue(), "none") });
|
|
1225
|
+
}
|
|
1226
|
+
/**
|
|
1227
|
+
* Maps a `None` to the returned `Option<U>` by applying a function to a contained value, leaving `Some<T>`
|
|
1228
|
+
* untouched.
|
|
1229
|
+
* @param cb The predicate.
|
|
1230
|
+
*
|
|
1231
|
+
* @example
|
|
1232
|
+
* ```typescript
|
|
1233
|
+
* const input: Option<string> = some('Hello, world!');
|
|
1234
|
+
* const result = input.mapNoneInto(() => some(13));
|
|
1235
|
+
*
|
|
1236
|
+
* assert.equal(result, some('Hello, world!'));
|
|
1237
|
+
* ```
|
|
1238
|
+
* @example
|
|
1239
|
+
* ```typescript
|
|
1240
|
+
* const input: Option<string> = none;
|
|
1241
|
+
* const result = input.mapNoneInto(() => some(13));
|
|
1242
|
+
*
|
|
1243
|
+
* assert.equal(result, some(13));
|
|
1244
|
+
* ```
|
|
1245
|
+
*
|
|
1246
|
+
* @note This is an extension not supported in Rust
|
|
1247
|
+
*/
|
|
1248
|
+
mapNoneInto(cb) {
|
|
1249
|
+
return this.match({ some: returnThis, none: cb });
|
|
1250
|
+
}
|
|
1251
|
+
/**
|
|
1252
|
+
* Calls the provided closure with a reference to the contained value (if `Some`).
|
|
1253
|
+
* @param cb The predicate.
|
|
1254
|
+
* @seealso {@link inspectAsync} for the awaitable version.
|
|
1255
|
+
*
|
|
1256
|
+
* @example
|
|
1257
|
+
* ```typescript
|
|
1258
|
+
* some(2).inspect(console.log);
|
|
1259
|
+
* // Logs: 2
|
|
1260
|
+
* ```
|
|
1261
|
+
* @example
|
|
1262
|
+
* ```typescript
|
|
1263
|
+
* none.inspect(console.log);
|
|
1264
|
+
* // Doesn't log
|
|
1265
|
+
* ```
|
|
1266
|
+
*
|
|
1267
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.inspect}
|
|
1268
|
+
*/
|
|
204
1269
|
inspect(cb) {
|
|
205
|
-
|
|
1270
|
+
if (this.isSome()) cb(this[ValueProperty2]);
|
|
206
1271
|
return this;
|
|
207
1272
|
}
|
|
1273
|
+
/**
|
|
1274
|
+
* Calls the provided closure with a reference to the contained value (if `Some`).
|
|
1275
|
+
* @param cb The predicate.
|
|
1276
|
+
* @seealso {@link inspect} for the sync version.
|
|
1277
|
+
*
|
|
1278
|
+
* @example
|
|
1279
|
+
* ```typescript
|
|
1280
|
+
* await some(2).inspectAsync(console.log);
|
|
1281
|
+
* // Logs: 2
|
|
1282
|
+
* ```
|
|
1283
|
+
* @example
|
|
1284
|
+
* ```typescript
|
|
1285
|
+
* await none.inspectAsync(console.log);
|
|
1286
|
+
* // Doesn't log
|
|
1287
|
+
* ```
|
|
1288
|
+
*
|
|
1289
|
+
* @note This is an extension not supported in Rust
|
|
1290
|
+
*/
|
|
208
1291
|
async inspectAsync(cb) {
|
|
209
|
-
await cb(this
|
|
1292
|
+
if (this.isSome()) await cb(this[ValueProperty2]);
|
|
210
1293
|
return this;
|
|
211
1294
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
1295
|
+
/**
|
|
1296
|
+
* Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err)`.
|
|
1297
|
+
*
|
|
1298
|
+
* Arguments passed to `okOr` are eagerly evaluated; if you are passing the result of a function call, it is
|
|
1299
|
+
* recommended to use {@link okOrElse}, which is lazily evaluated.
|
|
1300
|
+
* @param err The error to be used.
|
|
1301
|
+
*
|
|
1302
|
+
* @example
|
|
1303
|
+
* ```typescript
|
|
1304
|
+
* const x: Option<string> = some('hello');
|
|
1305
|
+
* assert.equal(x.okOr(0), ok('hello'));
|
|
1306
|
+
* ```
|
|
1307
|
+
* @example
|
|
1308
|
+
* ```typescript
|
|
1309
|
+
* const x: Option<string> = none;
|
|
1310
|
+
* assert.equal(x.okOr(0), err(0));
|
|
1311
|
+
* ```
|
|
1312
|
+
*
|
|
1313
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or}
|
|
1314
|
+
*/
|
|
1315
|
+
okOr(error) {
|
|
1316
|
+
return this.match({ some: /* @__PURE__ */ __name((value) => ok(value), "some"), none: /* @__PURE__ */ __name(() => err(error), "none") });
|
|
1317
|
+
}
|
|
1318
|
+
/**
|
|
1319
|
+
* Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`.
|
|
1320
|
+
* @param cb The error to be used.
|
|
1321
|
+
*
|
|
1322
|
+
* @example
|
|
1323
|
+
* ```typescript
|
|
1324
|
+
* const x: Option<string> = some('hello');
|
|
1325
|
+
* assert.equal(x.okOrElse(() => 0), ok('hello'));
|
|
1326
|
+
* ```
|
|
1327
|
+
* @example
|
|
1328
|
+
* ```typescript
|
|
1329
|
+
* const x: Option<string> = none;
|
|
1330
|
+
* assert.equal(x.okOrElse(() => 0), err(0));
|
|
1331
|
+
* ```
|
|
1332
|
+
*
|
|
1333
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or_else}
|
|
1334
|
+
*/
|
|
1335
|
+
okOrElse(cb) {
|
|
1336
|
+
return this.match({ some: /* @__PURE__ */ __name((value) => ok(value), "some"), none: /* @__PURE__ */ __name(() => err(cb()), "none") });
|
|
1337
|
+
}
|
|
1338
|
+
/**
|
|
1339
|
+
* Returns an iterator over the possibly contained value.
|
|
1340
|
+
*
|
|
1341
|
+
* The iterator yields one value if the result is `Some`, otherwise none.
|
|
1342
|
+
*
|
|
1343
|
+
* @example
|
|
1344
|
+
* ```typescript
|
|
1345
|
+
* const x = some(7);
|
|
1346
|
+
* for (const value of x) {
|
|
1347
|
+
* console.log(value);
|
|
1348
|
+
* }
|
|
1349
|
+
* // Logs 7
|
|
1350
|
+
* ```
|
|
1351
|
+
* @example
|
|
1352
|
+
* ```typescript
|
|
1353
|
+
* const x = none;
|
|
1354
|
+
* for (const value of x) {
|
|
1355
|
+
* console.log(value);
|
|
1356
|
+
* }
|
|
1357
|
+
* // Doesn't log
|
|
1358
|
+
* ```
|
|
1359
|
+
*
|
|
1360
|
+
* @see {@link Option.iter}
|
|
1361
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.iter}
|
|
1362
|
+
*/
|
|
218
1363
|
*iter() {
|
|
219
|
-
yield this
|
|
220
|
-
}
|
|
1364
|
+
if (this.isSome()) yield this[ValueProperty2];
|
|
1365
|
+
}
|
|
1366
|
+
/**
|
|
1367
|
+
* Returns `None` if the option is `None`, otherwise returns `option`.
|
|
1368
|
+
* @param option The option.
|
|
1369
|
+
*
|
|
1370
|
+
* @example
|
|
1371
|
+
* ```typescript
|
|
1372
|
+
* const x: Option<number> = some(2);
|
|
1373
|
+
* const y: Option<string> = none;
|
|
1374
|
+
* assert.equal(x.and(y), none);
|
|
1375
|
+
* ```
|
|
1376
|
+
* @example
|
|
1377
|
+
* ```typescript
|
|
1378
|
+
* const x: Option<number> = none;
|
|
1379
|
+
* const y: Option<string> = some('foo');
|
|
1380
|
+
* assert.equal(x.and(y), none);
|
|
1381
|
+
* ```
|
|
1382
|
+
* @example
|
|
1383
|
+
* ```typescript
|
|
1384
|
+
* const x: Option<number> = some(2);
|
|
1385
|
+
* const y: Option<string> = some('foo');
|
|
1386
|
+
* assert.equal(x.and(y), some('foo'));
|
|
1387
|
+
* ```
|
|
1388
|
+
* @example
|
|
1389
|
+
* ```typescript
|
|
1390
|
+
* const x: Option<number> = none;
|
|
1391
|
+
* const y: Option<string> = none;
|
|
1392
|
+
* assert.equal(x.and(y), none);
|
|
1393
|
+
* ```
|
|
1394
|
+
*
|
|
1395
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.and}
|
|
1396
|
+
*/
|
|
221
1397
|
and(option) {
|
|
222
|
-
return option;
|
|
223
|
-
}
|
|
1398
|
+
return this.match({ some: /* @__PURE__ */ __name(() => option, "some"), none: returnThis });
|
|
1399
|
+
}
|
|
1400
|
+
/**
|
|
1401
|
+
* Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
|
|
1402
|
+
*
|
|
1403
|
+
* This function can be used for control flow based on `Result` values.
|
|
1404
|
+
* @param cb The predicate.
|
|
1405
|
+
*
|
|
1406
|
+
* @example
|
|
1407
|
+
* ```typescript
|
|
1408
|
+
* function fractionOf4(value: number) {
|
|
1409
|
+
* return value === 0 ? none : some(4 / value);
|
|
1410
|
+
* }
|
|
1411
|
+
*
|
|
1412
|
+
* assert.equal(some(2).andThen(fractionOf4), some(4));
|
|
1413
|
+
* assert.equal(some(0).andThen(fractionOf4), none);
|
|
1414
|
+
* assert.equal(none.andThen(fractionOf4), none);
|
|
1415
|
+
* ```
|
|
1416
|
+
*
|
|
1417
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then}
|
|
1418
|
+
*/
|
|
224
1419
|
andThen(cb) {
|
|
225
|
-
return
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
1420
|
+
return this.match({ some: /* @__PURE__ */ __name((value) => cb(value), "some"), none: returnThis });
|
|
1421
|
+
}
|
|
1422
|
+
/**
|
|
1423
|
+
* Returns the option if it contains a value, otherwise returns `option`.
|
|
1424
|
+
* @param option The option.
|
|
1425
|
+
*
|
|
1426
|
+
* @example
|
|
1427
|
+
* ```typescript
|
|
1428
|
+
* const x: Option<number> = some(2);
|
|
1429
|
+
* const y: Option<number> = none;
|
|
1430
|
+
* assert.equal(x.or(y), some(2));
|
|
1431
|
+
* ```
|
|
1432
|
+
* @example
|
|
1433
|
+
* ```typescript
|
|
1434
|
+
* const x: Option<number> = none;
|
|
1435
|
+
* const y: Option<number> = some(100);
|
|
1436
|
+
* assert.equal(x.or(y), some(100));
|
|
1437
|
+
* ```
|
|
1438
|
+
* @example
|
|
1439
|
+
* ```typescript
|
|
1440
|
+
* const x: Option<number> = some(2);
|
|
1441
|
+
* const y: Option<number> = some(100);
|
|
1442
|
+
* assert.equal(x.or(y), some(2));
|
|
1443
|
+
* ```
|
|
1444
|
+
* @example
|
|
1445
|
+
* ```typescript
|
|
1446
|
+
* const x: Option<number> = none;
|
|
1447
|
+
* const y: Option<number> = none;
|
|
1448
|
+
* assert.equal(x.or(y), none);
|
|
1449
|
+
* ```
|
|
1450
|
+
*
|
|
1451
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.or}
|
|
1452
|
+
*/
|
|
1453
|
+
or(option) {
|
|
1454
|
+
return this.match({ some: returnThis, none: /* @__PURE__ */ __name(() => option, "none") });
|
|
1455
|
+
}
|
|
1456
|
+
/**
|
|
1457
|
+
* Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
|
|
1458
|
+
*
|
|
1459
|
+
* This function can be used for control flow based on `Result` values.
|
|
1460
|
+
* @param cb The predicate.
|
|
1461
|
+
*
|
|
1462
|
+
* @example
|
|
1463
|
+
* ```typescript
|
|
1464
|
+
* const nobody = (): Option<string> => none;
|
|
1465
|
+
* const vikings = (): Option<string> => some('vikings');
|
|
1466
|
+
*
|
|
1467
|
+
* assert.equal(some('barbarians').orElse(vikings), some('barbarians'));
|
|
1468
|
+
* assert.equal(none.orElse(vikings), some('vikings'));
|
|
1469
|
+
* assert.equal(none.orElse(nobody), none);
|
|
1470
|
+
* ```
|
|
1471
|
+
*
|
|
1472
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.or_else}
|
|
1473
|
+
*/
|
|
1474
|
+
orElse(cb) {
|
|
1475
|
+
return this.match({ some: returnThis, none: /* @__PURE__ */ __name(() => cb(), "none") });
|
|
1476
|
+
}
|
|
1477
|
+
/**
|
|
1478
|
+
* Returns `Some` if exactly one of self or `option` is `Some`, otherwise returns `None`.
|
|
1479
|
+
* @param option The option to compare.
|
|
1480
|
+
*
|
|
1481
|
+
* @example
|
|
1482
|
+
* ```typescript
|
|
1483
|
+
* const x: Option<number> = some(2);
|
|
1484
|
+
* const y: Option<number> = none;
|
|
1485
|
+
* assert.equal(x.xor(y), some(2));
|
|
1486
|
+
* ```
|
|
1487
|
+
* @example
|
|
1488
|
+
* ```typescript
|
|
1489
|
+
* const x: Option<number> = none;
|
|
1490
|
+
* const y: Option<number> = some(2);
|
|
1491
|
+
* assert.equal(x.xor(y), some(2));
|
|
1492
|
+
* ```
|
|
1493
|
+
* @example
|
|
1494
|
+
* ```typescript
|
|
1495
|
+
* const x: Option<number> = some(2);
|
|
1496
|
+
* const y: Option<number> = some(2);
|
|
1497
|
+
* assert.equal(x.xor(y), none);
|
|
1498
|
+
* ```
|
|
1499
|
+
* @example
|
|
1500
|
+
* ```typescript
|
|
1501
|
+
* const x: Option<number> = none;
|
|
1502
|
+
* const y: Option<number> = none;
|
|
1503
|
+
* assert.equal(x.xor(y), none);
|
|
1504
|
+
* ```
|
|
1505
|
+
*
|
|
1506
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.xor}
|
|
1507
|
+
*/
|
|
233
1508
|
xor(option) {
|
|
234
|
-
return
|
|
1509
|
+
return this.match({
|
|
1510
|
+
some() {
|
|
1511
|
+
return option.isNone() ? this : none;
|
|
1512
|
+
},
|
|
1513
|
+
none: /* @__PURE__ */ __name(() => option, "none")
|
|
1514
|
+
});
|
|
235
1515
|
}
|
|
236
1516
|
filter(predicate) {
|
|
237
|
-
return
|
|
238
|
-
}
|
|
1517
|
+
return this.isSomeAnd(predicate) ? this : none;
|
|
1518
|
+
}
|
|
1519
|
+
/**
|
|
1520
|
+
* Returns `true` if the option is a `Some` value containing the given value.
|
|
1521
|
+
* @param value The value to compare.
|
|
1522
|
+
*
|
|
1523
|
+
* @example
|
|
1524
|
+
* ```typescript
|
|
1525
|
+
* const x: Option<number> = some(2);
|
|
1526
|
+
* assert.equal(x.contains(2), true);
|
|
1527
|
+
* ```
|
|
1528
|
+
* @example
|
|
1529
|
+
* ```typescript
|
|
1530
|
+
* const x: Option<number> = some(3);
|
|
1531
|
+
* assert.equal(x.contains(2), false);
|
|
1532
|
+
* ```
|
|
1533
|
+
* @example
|
|
1534
|
+
* ```typescript
|
|
1535
|
+
* const x: Option<number> = none;
|
|
1536
|
+
* assert.equal(x.contains(2), false);
|
|
1537
|
+
* ```
|
|
1538
|
+
*
|
|
1539
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.contains}
|
|
1540
|
+
*/
|
|
239
1541
|
contains(value) {
|
|
240
|
-
return this.
|
|
241
|
-
}
|
|
1542
|
+
return this.isSomeAnd((inner) => inner === value);
|
|
1543
|
+
}
|
|
1544
|
+
/**
|
|
1545
|
+
* Zips self with another `Option`.
|
|
1546
|
+
*
|
|
1547
|
+
* If self is `Some(s)` and `other` is `Some(o)`, this method returns `Some([s, o])`. Otherwise, `None` is returned.
|
|
1548
|
+
* @param other The option to zip self with.
|
|
1549
|
+
*
|
|
1550
|
+
* @example
|
|
1551
|
+
* ```typescript
|
|
1552
|
+
* const x = some(1);
|
|
1553
|
+
* const y = some('hi');
|
|
1554
|
+
* const z = none;
|
|
1555
|
+
*
|
|
1556
|
+
* assert.equal(x.zip(y), some([1, 'hi']));
|
|
1557
|
+
* assert.equal(x.zip(z), none);
|
|
1558
|
+
* ```
|
|
1559
|
+
*
|
|
1560
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.zip}
|
|
1561
|
+
*/
|
|
242
1562
|
zip(other) {
|
|
243
|
-
return other.
|
|
244
|
-
}
|
|
1563
|
+
return this.isSome() && other.isSome() ? some([this[ValueProperty2], other[ValueProperty2]]) : none;
|
|
1564
|
+
}
|
|
1565
|
+
/**
|
|
1566
|
+
* Zips self and another `Option` with function `f`.
|
|
1567
|
+
*
|
|
1568
|
+
* If self is `Some(s)` and other is `Some(o)`, this method returns `Some(f(s, o))`. Otherwise, `None` is returned.
|
|
1569
|
+
* @param other The option to zip self with.
|
|
1570
|
+
* @param f The function that computes the returned value.
|
|
1571
|
+
*
|
|
1572
|
+
* @example
|
|
1573
|
+
* ```typescript
|
|
1574
|
+
* class Point {
|
|
1575
|
+
* public readonly x: number;
|
|
1576
|
+
* public readonly y: number;
|
|
1577
|
+
*
|
|
1578
|
+
* public constructor(x: number, y: number) {
|
|
1579
|
+
* this.x = x;
|
|
1580
|
+
* this.y = y;
|
|
1581
|
+
* }
|
|
1582
|
+
* }
|
|
1583
|
+
*
|
|
1584
|
+
* const x = some(17.5);
|
|
1585
|
+
* const y = some(42.7);
|
|
1586
|
+
*
|
|
1587
|
+
* assert.equal(x.zipWith(y, (s, o) => new Point(s, o)), some(new Point(17.5, 42.7)));
|
|
1588
|
+
* assert.equal(x.zipWith(none, (s, o) => new Point(s, o)), none);
|
|
1589
|
+
* ```
|
|
1590
|
+
*
|
|
1591
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.zip_with}
|
|
1592
|
+
*/
|
|
245
1593
|
zipWith(other, f) {
|
|
246
|
-
return other.
|
|
247
|
-
}
|
|
1594
|
+
return this.isSome() && other.isSome() ? some(f(this[ValueProperty2], other[ValueProperty2])) : none;
|
|
1595
|
+
}
|
|
1596
|
+
/**
|
|
1597
|
+
* Unzips an option containing a tuple of two options.
|
|
1598
|
+
*
|
|
1599
|
+
* If self is `Some([a, b])` this method returns `[Some(a), Some(b)]`. Otherwise, `[None, None]` is returned.
|
|
1600
|
+
*
|
|
1601
|
+
* @example
|
|
1602
|
+
* ```typescript
|
|
1603
|
+
* const x: Option<[number, string]> = some([1, 'hi']);
|
|
1604
|
+
* assert.equal(x.unzip(), [some(1), some('hi')]);
|
|
1605
|
+
* ```
|
|
1606
|
+
* @example
|
|
1607
|
+
* ```typescript
|
|
1608
|
+
* const x: Option<[number, string]> = none;
|
|
1609
|
+
* assert.equal(x.unzip(), [none, none]);
|
|
1610
|
+
* ```
|
|
1611
|
+
*
|
|
1612
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unzip}
|
|
1613
|
+
*/
|
|
248
1614
|
unzip() {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
transpose() {
|
|
253
|
-
return this.value.match({
|
|
254
|
-
ok: (v) => createOk(createSome(v)),
|
|
255
|
-
err: (e) => createErr(e)
|
|
1615
|
+
return this.match({
|
|
1616
|
+
some: /* @__PURE__ */ __name(([value0, value1]) => [some(value0), some(value1)], "some"),
|
|
1617
|
+
none: /* @__PURE__ */ __name(() => [none, none], "none")
|
|
256
1618
|
});
|
|
257
1619
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
}
|
|
273
|
-
*[Symbol.iterator]() {
|
|
274
|
-
yield this.value;
|
|
275
|
-
}
|
|
276
|
-
};
|
|
277
|
-
__name(_OptionSome, "OptionSome");
|
|
278
|
-
var OptionSome = _OptionSome;
|
|
279
|
-
function createSome(value) {
|
|
280
|
-
return new OptionSome(value);
|
|
281
|
-
}
|
|
282
|
-
__name(createSome, "createSome");
|
|
283
|
-
|
|
284
|
-
// src/lib/Result/Err.ts
|
|
285
|
-
var _ResultErr = class _ResultErr {
|
|
286
|
-
constructor(error) {
|
|
287
|
-
__publicField(this, "error");
|
|
288
|
-
this.error = error;
|
|
289
|
-
}
|
|
290
|
-
isOk() {
|
|
291
|
-
return false;
|
|
292
|
-
}
|
|
293
|
-
isOkAnd() {
|
|
294
|
-
return false;
|
|
295
|
-
}
|
|
296
|
-
isErr() {
|
|
297
|
-
return true;
|
|
298
|
-
}
|
|
299
|
-
isErrAnd(cb) {
|
|
300
|
-
return cb(this.error);
|
|
301
|
-
}
|
|
302
|
-
ok() {
|
|
303
|
-
return createNone;
|
|
304
|
-
}
|
|
305
|
-
err() {
|
|
306
|
-
return createSome(this.error);
|
|
307
|
-
}
|
|
308
|
-
map() {
|
|
309
|
-
return this;
|
|
310
|
-
}
|
|
311
|
-
mapInto() {
|
|
312
|
-
return this;
|
|
313
|
-
}
|
|
314
|
-
mapOr(defaultValue) {
|
|
315
|
-
return defaultValue;
|
|
316
|
-
}
|
|
317
|
-
mapOrElse(op) {
|
|
318
|
-
return op(this.error);
|
|
319
|
-
}
|
|
320
|
-
mapErr(cb) {
|
|
321
|
-
return createErr(cb(this.error));
|
|
322
|
-
}
|
|
323
|
-
mapErrInto(cb) {
|
|
324
|
-
return cb(this.error);
|
|
325
|
-
}
|
|
326
|
-
inspect() {
|
|
327
|
-
return this;
|
|
328
|
-
}
|
|
329
|
-
inspectAsync() {
|
|
330
|
-
return Promise.resolve(this);
|
|
331
|
-
}
|
|
332
|
-
inspectErr(cb) {
|
|
333
|
-
cb(this.error);
|
|
334
|
-
return this;
|
|
335
|
-
}
|
|
336
|
-
async inspectErrAsync(cb) {
|
|
337
|
-
await cb(this.error);
|
|
338
|
-
return this;
|
|
339
|
-
}
|
|
340
|
-
*iter() {
|
|
341
|
-
}
|
|
342
|
-
expect(message) {
|
|
343
|
-
throw new ResultError(message, this.error);
|
|
344
|
-
}
|
|
345
|
-
expectErr() {
|
|
346
|
-
return this.error;
|
|
347
|
-
}
|
|
348
|
-
unwrap() {
|
|
349
|
-
throw new ResultError("Unwrap failed", this.error);
|
|
350
|
-
}
|
|
351
|
-
unwrapErr() {
|
|
352
|
-
return this.error;
|
|
353
|
-
}
|
|
354
|
-
unwrapOr(defaultValue) {
|
|
355
|
-
return defaultValue;
|
|
356
|
-
}
|
|
357
|
-
unwrapOrElse(op) {
|
|
358
|
-
return op(this.error);
|
|
359
|
-
}
|
|
360
|
-
unwrapRaw() {
|
|
361
|
-
throw this.error;
|
|
362
|
-
}
|
|
363
|
-
and() {
|
|
364
|
-
return this;
|
|
365
|
-
}
|
|
366
|
-
andThen() {
|
|
367
|
-
return this;
|
|
368
|
-
}
|
|
369
|
-
or(result) {
|
|
370
|
-
return result;
|
|
371
|
-
}
|
|
372
|
-
orElse(cb) {
|
|
373
|
-
return cb(this.error);
|
|
374
|
-
}
|
|
375
|
-
contains() {
|
|
376
|
-
return false;
|
|
377
|
-
}
|
|
378
|
-
containsErr(error) {
|
|
379
|
-
return this.error === error;
|
|
380
|
-
}
|
|
1620
|
+
/**
|
|
1621
|
+
* Transposes an `Option` of a `Result` into a `Result` of an `Option`.
|
|
1622
|
+
*
|
|
1623
|
+
* `none` will be mapped to `ok(none)`. `some(ok(v))` and `some(err(e))` will be mapped to `ok(some(v))` and `err(e)`.
|
|
1624
|
+
*
|
|
1625
|
+
* @example
|
|
1626
|
+
* ```typescript
|
|
1627
|
+
* const x: Option<Result<number, Error>> = some(ok(5));
|
|
1628
|
+
* const y: Result<Option<number>, Error> = ok(some(5));
|
|
1629
|
+
* assert.equal(x.transpose(), y);
|
|
1630
|
+
* ```
|
|
1631
|
+
*
|
|
1632
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.transpose}
|
|
1633
|
+
*/
|
|
381
1634
|
transpose() {
|
|
382
|
-
return
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
intoOkOrErr() {
|
|
388
|
-
return this.error;
|
|
389
|
-
}
|
|
390
|
-
async intoPromise() {
|
|
391
|
-
return createErr(await this.error);
|
|
392
|
-
}
|
|
393
|
-
eq(other) {
|
|
394
|
-
return other.isErrAnd((error) => this.error === error);
|
|
395
|
-
}
|
|
396
|
-
ne(other) {
|
|
397
|
-
return !this.eq(other);
|
|
398
|
-
}
|
|
399
|
-
match(branches) {
|
|
400
|
-
return branches.err(this.error);
|
|
401
|
-
}
|
|
402
|
-
*[Symbol.iterator]() {
|
|
403
|
-
}
|
|
404
|
-
};
|
|
405
|
-
__name(_ResultErr, "ResultErr");
|
|
406
|
-
var ResultErr = _ResultErr;
|
|
407
|
-
function createErr(x) {
|
|
408
|
-
return new ResultErr(x);
|
|
409
|
-
}
|
|
410
|
-
__name(createErr, "createErr");
|
|
411
|
-
|
|
412
|
-
// src/lib/Option/OptionError.ts
|
|
413
|
-
var _OptionError = class _OptionError extends Error {
|
|
414
|
-
get name() {
|
|
415
|
-
return this.constructor.name;
|
|
416
|
-
}
|
|
417
|
-
};
|
|
418
|
-
__name(_OptionError, "OptionError");
|
|
419
|
-
var OptionError = _OptionError;
|
|
420
|
-
|
|
421
|
-
// src/lib/Option/None.ts
|
|
422
|
-
var _OptionNone = class _OptionNone {
|
|
423
|
-
isSome() {
|
|
424
|
-
return false;
|
|
425
|
-
}
|
|
426
|
-
isSomeAnd() {
|
|
427
|
-
return false;
|
|
428
|
-
}
|
|
429
|
-
isNone() {
|
|
430
|
-
return true;
|
|
431
|
-
}
|
|
432
|
-
expect(message) {
|
|
433
|
-
throw new OptionError(message);
|
|
434
|
-
}
|
|
435
|
-
unwrap() {
|
|
436
|
-
throw new OptionError("Unwrap failed");
|
|
437
|
-
}
|
|
438
|
-
unwrapOr(defaultValue) {
|
|
439
|
-
return defaultValue;
|
|
440
|
-
}
|
|
441
|
-
unwrapOrElse(cb) {
|
|
442
|
-
return cb();
|
|
443
|
-
}
|
|
444
|
-
map() {
|
|
445
|
-
return this;
|
|
446
|
-
}
|
|
447
|
-
mapInto() {
|
|
448
|
-
return this;
|
|
449
|
-
}
|
|
450
|
-
mapOr(defaultValue) {
|
|
451
|
-
return defaultValue;
|
|
452
|
-
}
|
|
453
|
-
mapOrElse(defaultValue) {
|
|
454
|
-
return defaultValue();
|
|
455
|
-
}
|
|
456
|
-
mapNoneInto(cb) {
|
|
457
|
-
return cb();
|
|
458
|
-
}
|
|
459
|
-
inspect() {
|
|
460
|
-
return this;
|
|
461
|
-
}
|
|
462
|
-
inspectAsync() {
|
|
463
|
-
return Promise.resolve(this);
|
|
464
|
-
}
|
|
465
|
-
okOr(error) {
|
|
466
|
-
return createErr(error);
|
|
467
|
-
}
|
|
468
|
-
okOrElse(cb) {
|
|
469
|
-
return createErr(cb());
|
|
470
|
-
}
|
|
471
|
-
*iter() {
|
|
472
|
-
}
|
|
473
|
-
and() {
|
|
474
|
-
return this;
|
|
475
|
-
}
|
|
476
|
-
andThen() {
|
|
477
|
-
return this;
|
|
478
|
-
}
|
|
479
|
-
or(option) {
|
|
480
|
-
return option;
|
|
481
|
-
}
|
|
482
|
-
orElse(cb) {
|
|
483
|
-
return cb();
|
|
484
|
-
}
|
|
485
|
-
xor(option) {
|
|
486
|
-
return option.isSome() ? option : this;
|
|
487
|
-
}
|
|
488
|
-
filter() {
|
|
489
|
-
return this;
|
|
490
|
-
}
|
|
491
|
-
contains() {
|
|
492
|
-
return false;
|
|
493
|
-
}
|
|
494
|
-
zip() {
|
|
495
|
-
return this;
|
|
496
|
-
}
|
|
497
|
-
zipWith() {
|
|
498
|
-
return this;
|
|
499
|
-
}
|
|
500
|
-
unzip() {
|
|
501
|
-
return [this, this];
|
|
502
|
-
}
|
|
503
|
-
transpose() {
|
|
504
|
-
return createOk(this);
|
|
1635
|
+
return this.match({
|
|
1636
|
+
// @ts-expect-error Complex types
|
|
1637
|
+
some: /* @__PURE__ */ __name((result) => result.map(some), "some"),
|
|
1638
|
+
none: /* @__PURE__ */ __name(() => ok(none), "none")
|
|
1639
|
+
});
|
|
505
1640
|
}
|
|
1641
|
+
/**
|
|
1642
|
+
* Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
|
|
1643
|
+
*
|
|
1644
|
+
* @example
|
|
1645
|
+
* ```typescript
|
|
1646
|
+
* const x: Option<Option<number>> = some(some(6));
|
|
1647
|
+
* assert.equal(x.flatten(), some(6));
|
|
1648
|
+
* ```
|
|
1649
|
+
* @example
|
|
1650
|
+
* ```typescript
|
|
1651
|
+
* const x: Option<Option<number>> = some(none);
|
|
1652
|
+
* assert.equal(x.flatten(), none);
|
|
1653
|
+
* ```
|
|
1654
|
+
* @example
|
|
1655
|
+
* ```typescript
|
|
1656
|
+
* const x: Option<Option<number>> = none;
|
|
1657
|
+
* assert.equal(x.flatten(), none);
|
|
1658
|
+
* ```
|
|
1659
|
+
*
|
|
1660
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten}
|
|
1661
|
+
*/
|
|
506
1662
|
flatten() {
|
|
507
|
-
return this;
|
|
508
|
-
}
|
|
1663
|
+
return this.match({ some: /* @__PURE__ */ __name((inner) => inner, "some"), none: returnThis });
|
|
1664
|
+
}
|
|
1665
|
+
/**
|
|
1666
|
+
* Returns a `Promise` object with the awaited value (if `Some`).
|
|
1667
|
+
*
|
|
1668
|
+
* @example
|
|
1669
|
+
* ```typescript
|
|
1670
|
+
* let x = some(Promise.resolve(3));
|
|
1671
|
+
* assert.equal(await x.intoPromise(), some(3));
|
|
1672
|
+
* ```
|
|
1673
|
+
*
|
|
1674
|
+
* @note This is an extension not supported in Rust
|
|
1675
|
+
*/
|
|
509
1676
|
intoPromise() {
|
|
510
|
-
return
|
|
1677
|
+
return this.match({
|
|
1678
|
+
some: /* @__PURE__ */ __name(async (value) => some(await value), "some"),
|
|
1679
|
+
// NOSONAR
|
|
1680
|
+
none: /* @__PURE__ */ __name(() => Promise.resolve(none), "none")
|
|
1681
|
+
});
|
|
511
1682
|
}
|
|
1683
|
+
/**
|
|
1684
|
+
* Checks whether or not `other` equals with self.
|
|
1685
|
+
* @param other The other option to compare.
|
|
1686
|
+
*
|
|
1687
|
+
* @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq}
|
|
1688
|
+
*/
|
|
512
1689
|
eq(other) {
|
|
513
|
-
return other.
|
|
514
|
-
}
|
|
1690
|
+
return this.isSome() === other.isSome() && this[ValueProperty2] === other[ValueProperty2];
|
|
1691
|
+
}
|
|
1692
|
+
/**
|
|
1693
|
+
* Checks whether or not `other` doesn't equal with self.
|
|
1694
|
+
* @param other The other option to compare.
|
|
1695
|
+
*
|
|
1696
|
+
* @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#method.ne}
|
|
1697
|
+
*/
|
|
515
1698
|
ne(other) {
|
|
516
|
-
return
|
|
1699
|
+
return !this.eq(other);
|
|
517
1700
|
}
|
|
1701
|
+
/**
|
|
1702
|
+
* Runs `ok` function if self is `Ok`, otherwise runs `err` function.
|
|
1703
|
+
* @param branches The branches to match.
|
|
1704
|
+
*
|
|
1705
|
+
* @example
|
|
1706
|
+
* ```typescript
|
|
1707
|
+
* const option = some(4).match({
|
|
1708
|
+
* some: (v) => v,
|
|
1709
|
+
* none: () => 0
|
|
1710
|
+
* });
|
|
1711
|
+
* assert.equal(option, 4);
|
|
1712
|
+
* ```
|
|
1713
|
+
* @example
|
|
1714
|
+
* ```typescript
|
|
1715
|
+
* const option = none.match({
|
|
1716
|
+
* some: (v) => v,
|
|
1717
|
+
* none: () => 0
|
|
1718
|
+
* });
|
|
1719
|
+
* assert.equal(option, 0);
|
|
1720
|
+
* ```
|
|
1721
|
+
*/
|
|
518
1722
|
match(branches) {
|
|
519
|
-
return branches.none();
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
(
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
1723
|
+
return this.isSome() ? branches.some.call(this, this[ValueProperty2]) : branches.none.call(this);
|
|
1724
|
+
}
|
|
1725
|
+
/**
|
|
1726
|
+
* Returns an iterator over the possibly contained value.
|
|
1727
|
+
*
|
|
1728
|
+
* The iterator yields one value if the result is `Some`, otherwise none.
|
|
1729
|
+
*
|
|
1730
|
+
* @example
|
|
1731
|
+
* ```typescript
|
|
1732
|
+
* const x = some(7);
|
|
1733
|
+
* for (const value of x) {
|
|
1734
|
+
* console.log(value);
|
|
1735
|
+
* }
|
|
1736
|
+
* // Logs 7
|
|
1737
|
+
* ```
|
|
1738
|
+
* @example
|
|
1739
|
+
* ```typescript
|
|
1740
|
+
* const x = none;
|
|
1741
|
+
* for (const value of x) {
|
|
1742
|
+
* console.log(value);
|
|
1743
|
+
* }
|
|
1744
|
+
* // Doesn't log
|
|
1745
|
+
* ```
|
|
1746
|
+
*
|
|
1747
|
+
* @see {@link IOption.iter}
|
|
1748
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.iter}
|
|
1749
|
+
*/
|
|
1750
|
+
[(_b2 = ValueProperty2, _a2 = ExistsProperty, Symbol.iterator)]() {
|
|
1751
|
+
return this.iter();
|
|
1752
|
+
}
|
|
1753
|
+
get [Symbol.toStringTag]() {
|
|
1754
|
+
return this.match({ some: /* @__PURE__ */ __name(() => "Some", "some"), none: /* @__PURE__ */ __name(() => "None", "none") });
|
|
1755
|
+
}
|
|
1756
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
1757
|
+
static some(value) {
|
|
1758
|
+
return new _Option(value, true);
|
|
1759
|
+
}
|
|
1760
|
+
/**
|
|
1761
|
+
* Checks if the `instance` object is an instance of `Option`, or if it is a `Option`-like object. This override
|
|
1762
|
+
* exists to interoperate with other versions of this class, such as the one coming from another version of this
|
|
1763
|
+
* library or from a different build.
|
|
1764
|
+
*
|
|
1765
|
+
* @param instance The instance to check.
|
|
1766
|
+
* @returns Whether or not the instance is a `Option`.
|
|
1767
|
+
*
|
|
1768
|
+
* @example
|
|
1769
|
+
* ```typescript
|
|
1770
|
+
* import { Option } from '@sapphire/result';
|
|
1771
|
+
* const { some } = require('@sapphire/result');
|
|
1772
|
+
*
|
|
1773
|
+
* some(2) instanceof Option; // true
|
|
1774
|
+
* ```
|
|
1775
|
+
*/
|
|
1776
|
+
static [Symbol.hasInstance](instance) {
|
|
1777
|
+
return typeof instance === "object" && instance !== null && ValueProperty2 in instance && ExistsProperty in instance;
|
|
1778
|
+
}
|
|
1779
|
+
/**
|
|
1780
|
+
* @deprecated Use {@link Option.isOption} instead.
|
|
1781
|
+
*
|
|
1782
|
+
* Checks if the `instance` object is an instance of `Option`, or if it is a `Option`-like object.
|
|
1783
|
+
*
|
|
1784
|
+
* @param instance The instance to check.
|
|
1785
|
+
* @returns true if the instance is a `Option` or a `Option`-like object, false otherwise.
|
|
1786
|
+
*
|
|
1787
|
+
* @example
|
|
1788
|
+
* ```typescript
|
|
1789
|
+
* import { Option } from '@sapphire/result';
|
|
1790
|
+
* const { some } = require('@sapphire/result');
|
|
1791
|
+
*
|
|
1792
|
+
* Option.isOption(some(2)); // true
|
|
1793
|
+
* ```
|
|
1794
|
+
*/
|
|
1795
|
+
static is(instance) {
|
|
1796
|
+
return _Option[Symbol.hasInstance](instance);
|
|
1797
|
+
}
|
|
1798
|
+
/**
|
|
1799
|
+
* Checks if the `instance` object is an instance of `Option`, or if it is a `Option`-like object.
|
|
1800
|
+
*
|
|
1801
|
+
* @param instance The instance to check.
|
|
1802
|
+
* @returns true if the instance is a `Option` or a `Option`-like object, false otherwise.
|
|
1803
|
+
*
|
|
1804
|
+
* @example
|
|
1805
|
+
* ```typescript
|
|
1806
|
+
* import { Option } from '@sapphire/result';
|
|
1807
|
+
* const { some } = require('@sapphire/result');
|
|
1808
|
+
*
|
|
1809
|
+
* Option.isOption(some(2)); // true
|
|
1810
|
+
* ```
|
|
1811
|
+
*/
|
|
1812
|
+
static isOption(instance) {
|
|
1813
|
+
return _Option[Symbol.hasInstance](instance);
|
|
1814
|
+
}
|
|
1815
|
+
/**
|
|
1816
|
+
* Creates a {@link Result} out of a callback.
|
|
1817
|
+
*
|
|
1818
|
+
* @typeparam T The result's type.
|
|
1819
|
+
* @typeparam E The error's type.
|
|
1820
|
+
*/
|
|
1821
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
1822
|
+
static from(op) {
|
|
547
1823
|
try {
|
|
548
|
-
return
|
|
1824
|
+
return resolve2(isFunction(op) ? op() : op);
|
|
549
1825
|
} catch {
|
|
550
|
-
return
|
|
1826
|
+
return none;
|
|
551
1827
|
}
|
|
552
1828
|
}
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
1829
|
+
/**
|
|
1830
|
+
* Creates a {@link Result} out of a promise or async callback.
|
|
1831
|
+
*
|
|
1832
|
+
* @typeparam T The result's type.
|
|
1833
|
+
* @typeparam E The error's type.
|
|
1834
|
+
*/
|
|
1835
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
1836
|
+
static async fromAsync(op) {
|
|
556
1837
|
try {
|
|
557
|
-
return
|
|
1838
|
+
return resolve2(await (isFunction(op) ? op() : op));
|
|
558
1839
|
} catch {
|
|
559
|
-
return
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
Option2.fromAsync = fromAsync;
|
|
563
|
-
__name(fromAsync, "fromAsync");
|
|
564
|
-
function all(options) {
|
|
565
|
-
const values = [];
|
|
566
|
-
for (const option of options) {
|
|
567
|
-
if (option.isNone()) {
|
|
568
|
-
return option;
|
|
569
|
-
}
|
|
570
|
-
values.push(option.unwrap());
|
|
571
|
-
}
|
|
572
|
-
return (0, Option2.some)(values);
|
|
573
|
-
}
|
|
574
|
-
Option2.all = all;
|
|
575
|
-
__name(all, "all");
|
|
576
|
-
function any(options) {
|
|
577
|
-
for (const result of options) {
|
|
578
|
-
if (result.isSome()) {
|
|
579
|
-
return result;
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
return Option2.none;
|
|
583
|
-
}
|
|
584
|
-
Option2.any = any;
|
|
585
|
-
__name(any, "any");
|
|
586
|
-
Option2.none = createNone;
|
|
587
|
-
Option2.some = createSome;
|
|
588
|
-
})(Option || (Option = {}));
|
|
589
|
-
|
|
590
|
-
// src/lib/Result.ts
|
|
591
|
-
var Result;
|
|
592
|
-
((Result2) => {
|
|
593
|
-
function resolve(value) {
|
|
594
|
-
if (is(value))
|
|
595
|
-
return value;
|
|
596
|
-
return (0, Result2.ok)(value);
|
|
597
|
-
}
|
|
598
|
-
__name(resolve, "resolve");
|
|
599
|
-
function is(value) {
|
|
600
|
-
return value instanceof ResultOk || value instanceof ResultErr;
|
|
601
|
-
}
|
|
602
|
-
Result2.is = is;
|
|
603
|
-
__name(is, "is");
|
|
604
|
-
function from(op) {
|
|
605
|
-
if (!isFunction(op))
|
|
606
|
-
return resolve(op);
|
|
607
|
-
try {
|
|
608
|
-
return resolve(op());
|
|
609
|
-
} catch (error) {
|
|
610
|
-
return (0, Result2.err)(error);
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
Result2.from = from;
|
|
614
|
-
__name(from, "from");
|
|
615
|
-
async function fromAsync(op) {
|
|
616
|
-
try {
|
|
617
|
-
return resolve(await (isFunction(op) ? op() : op));
|
|
618
|
-
} catch (error) {
|
|
619
|
-
return (0, Result2.err)(error);
|
|
1840
|
+
return none;
|
|
620
1841
|
}
|
|
621
1842
|
}
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
1843
|
+
/**
|
|
1844
|
+
* Creates an {@link Ok} that is the combination of all collected {@link Ok} values as an array, or the first
|
|
1845
|
+
* {@link Err} encountered.
|
|
1846
|
+
*
|
|
1847
|
+
* @param results An array of {@link Result}s.
|
|
1848
|
+
* @returns A new {@link Result}.
|
|
1849
|
+
*/
|
|
1850
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
1851
|
+
static all(results) {
|
|
625
1852
|
const values = [];
|
|
626
1853
|
for (const result of results) {
|
|
627
|
-
if (result.
|
|
628
|
-
|
|
629
|
-
}
|
|
630
|
-
values.push(result.unwrap());
|
|
1854
|
+
if (result.isNone()) return result;
|
|
1855
|
+
values.push(result[ValueProperty2]);
|
|
631
1856
|
}
|
|
632
|
-
return (
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
1857
|
+
return some(values);
|
|
1858
|
+
}
|
|
1859
|
+
/**
|
|
1860
|
+
* Returns the first encountered {@link Some}, or a {@link None} if none was found.
|
|
1861
|
+
*
|
|
1862
|
+
* @param options An array of {@link Option}s.
|
|
1863
|
+
* @returns A new {@link Option}.
|
|
1864
|
+
*/
|
|
1865
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
1866
|
+
static any(results) {
|
|
638
1867
|
for (const result of results) {
|
|
639
|
-
if (result.
|
|
640
|
-
return result;
|
|
641
|
-
}
|
|
642
|
-
errors.push(result.unwrapErr());
|
|
1868
|
+
if (result.isSome()) return result;
|
|
643
1869
|
}
|
|
644
|
-
return
|
|
1870
|
+
return none;
|
|
645
1871
|
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
1872
|
+
};
|
|
1873
|
+
__name(_Option, "Option");
|
|
1874
|
+
__publicField(_Option, "none", new _Option(null, false));
|
|
1875
|
+
var Option = _Option;
|
|
1876
|
+
var { some, none } = Option;
|
|
1877
|
+
function resolve2(value) {
|
|
1878
|
+
if (value === null || value === void 0) return none;
|
|
1879
|
+
if (Option.isOption(value)) return value;
|
|
1880
|
+
return some(value);
|
|
1881
|
+
}
|
|
1882
|
+
__name(resolve2, "resolve");
|
|
651
1883
|
|
|
652
|
-
export { Option, OptionError, Result, ResultError,
|
|
653
|
-
//# sourceMappingURL=
|
|
1884
|
+
export { Option, OptionError, Result, ResultError, err, none, ok, some };
|
|
1885
|
+
//# sourceMappingURL=index.mjs.map
|
|
654
1886
|
//# sourceMappingURL=index.mjs.map
|