happy-rusty 1.3.2 → 1.5.0

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/src/enum/core.ts CHANGED
@@ -26,8 +26,10 @@ export interface Option<T> {
26
26
 
27
27
  /**
28
28
  * [object Option].
29
+ *
30
+ * @private
29
31
  */
30
- [Symbol.toStringTag]: 'Option',
32
+ readonly [Symbol.toStringTag]: 'Option',
31
33
 
32
34
  /**
33
35
  * Identify `Some` or `None`.
@@ -60,6 +62,11 @@ export interface Option<T> {
60
62
  */
61
63
  isSomeAnd(predicate: (value: T) => boolean): boolean;
62
64
 
65
+ /**
66
+ * Asynchronous version of `isSomeAnd`.
67
+ */
68
+ isSomeAndAsync(predicate: (value: T) => Promise<boolean>): Promise<boolean>;
69
+
63
70
  // #endregion
64
71
 
65
72
  // #region Extracting the contained value
@@ -93,6 +100,11 @@ export interface Option<T> {
93
100
  */
94
101
  unwrapOrElse(fn: () => T): T;
95
102
 
103
+ /**
104
+ * Asynchronous version of `unwrapOrElse`.
105
+ */
106
+ unwrapOrElseAsync(fn: () => Promise<T>): Promise<T>;
107
+
96
108
  // #endregion
97
109
 
98
110
  // #region Transforming contained values
@@ -228,6 +240,11 @@ export interface Option<T> {
228
240
  */
229
241
  andThen<U>(fn: (value: T) => Option<U>): Option<U>;
230
242
 
243
+ /**
244
+ * Asynchronous version of `andThen`.
245
+ */
246
+ andThenAsync<U>(fn: (value: T) => AsyncOption<U>): AsyncOption<U>;
247
+
231
248
  /**
232
249
  * Returns the Option if it contains a value, otherwise returns `other`.
233
250
  * This can be used for providing a fallback `Option`.
@@ -244,6 +261,11 @@ export interface Option<T> {
244
261
  */
245
262
  orElse(fn: () => Option<T>): Option<T>;
246
263
 
264
+ /**
265
+ * Asynchronous version of `orElse`.
266
+ */
267
+ orElseAsync(fn: () => AsyncOption<T>): AsyncOption<T>;
268
+
247
269
  /**
248
270
  * Returns `Some` if exactly one of `this`, `other` is `Some`, otherwise returns `None`.
249
271
  * This can be thought of as an exclusive or operation on `Option` values.
@@ -300,8 +322,10 @@ export interface Result<T, E> {
300
322
 
301
323
  /**
302
324
  * [object Result].
325
+ *
326
+ * @private
303
327
  */
304
- [Symbol.toStringTag]: 'Result',
328
+ readonly [Symbol.toStringTag]: 'Result',
305
329
 
306
330
  /**
307
331
  * Identify `Ok` or `Err`.
@@ -334,12 +358,22 @@ export interface Result<T, E> {
334
358
  */
335
359
  isOkAnd(predicate: (value: T) => boolean): boolean;
336
360
 
361
+ /**
362
+ * Asynchronous version of `isOkAnd`.
363
+ */
364
+ isOkAndAsync(predicate: (value: T) => Promise<boolean>): Promise<boolean>;
365
+
337
366
  /**
338
367
  * Returns `true` if the result is `Err` and the provided predicate returns `true` for the contained error.
339
368
  * @param predicate - A function that takes the `Err` value and returns a boolean.
340
369
  */
341
370
  isErrAnd(predicate: (error: E) => boolean): boolean;
342
371
 
372
+ /**
373
+ * Asynchronous version of `isErrAnd`.
374
+ */
375
+ isErrAndAsync(predicate: (error: E) => Promise<boolean>): Promise<boolean>;
376
+
343
377
  // #endregion
344
378
 
345
379
  // #region Extracting the contained value
@@ -373,6 +407,11 @@ export interface Result<T, E> {
373
407
  */
374
408
  unwrapOrElse(fn: (error: E) => T): T;
375
409
 
410
+ /**
411
+ * Asynchronous version of `unwrapOrElse`.
412
+ */
413
+ unwrapOrElseAsync(fn: (error: E) => Promise<T>): Promise<T>;
414
+
376
415
  /**
377
416
  * These methods extract the contained value in a `Result<T, E>` when it is the `Err` variant.
378
417
  */
@@ -504,6 +543,11 @@ export interface Result<T, E> {
504
543
  */
505
544
  andThen<U>(fn: (value: T) => Result<U, E>): Result<U, E>;
506
545
 
546
+ /**
547
+ * Asynchronous version of `andThen`.
548
+ */
549
+ andThenAsync<U>(fn: (value: T) => AsyncResult<U, E>): AsyncResult<U, E>;
550
+
507
551
  /**
508
552
  * Calls the provided function with the contained error if `this` is `Err`, otherwise returns `this` as `Ok`.
509
553
  * @typeParam F - The type of the error returned by the function.
@@ -512,6 +556,11 @@ export interface Result<T, E> {
512
556
  */
513
557
  orElse<F>(fn: (error: E) => Result<T, F>): Result<T, F>;
514
558
 
559
+ /**
560
+ * Asynchronous version of `orElse`.
561
+ */
562
+ orElseAsync<F>(fn: (error: E) => AsyncResult<T, F>): AsyncResult<T, F>;
563
+
515
564
  // #endregion
516
565
 
517
566
  /**
@@ -566,4 +615,21 @@ export interface Result<T, E> {
566
615
  * Custom `toString` implementation that uses the `Result`'s contained value.
567
616
  */
568
617
  toString(): string;
569
- }
618
+ }
619
+
620
+ /**
621
+ * Represents an asynchronous operation that yields an `Option<T>`.
622
+ * This is a promise that resolves to either `Some(T)` if the value is present, or `None` if the value is absent.
623
+ *
624
+ * @typeParam T - The type of the value that may be contained within the `Option`.
625
+ */
626
+ export type AsyncOption<T> = Promise<Option<T>>;
627
+
628
+ /**
629
+ * Represents an asynchronous operation that yields a `Result<T, E>`.
630
+ * This is a promise that resolves to `Ok(T)` if the operation was successful, or `Err(E)` if there was an error.
631
+ *
632
+ * @typeParam T - The type of the value that is produced by a successful operation.
633
+ * @typeParam E - The type of the error that may be produced by a failed operation.
634
+ */
635
+ export type AsyncResult<T, E> = Promise<Result<T, E>>;
@@ -2,24 +2,21 @@
2
2
  * Exports some commonly used types.
3
3
  */
4
4
 
5
- import type { Option, Result } from './core.ts';
5
+ import type { AsyncResult, Result } from './core.ts';
6
6
 
7
7
  /**
8
- * Represents an asynchronous operation that yields an `Option<T>`.
9
- * This is a promise that resolves to either `Some(T)` if the value is present, or `None` if the value is absent.
8
+ * Similar to Rust's `Result<(), E>`.
10
9
  *
11
- * @typeParam T - The type of the value that may be contained within the `Option`.
10
+ * @typeParam E - The type of the error that may be produced by a failed operation.
12
11
  */
13
- export type AsyncOption<T> = Promise<Option<T>>;
12
+ export type VoidResult<E> = Result<void, E>;
14
13
 
15
14
  /**
16
- * Represents an asynchronous operation that yields a `Result<T, E>`.
17
- * This is a promise that resolves to `Ok(T)` if the operation was successful, or `Err(E)` if there was an error.
15
+ * `VoidResult<E>` wrapped by `Promise`.
18
16
  *
19
- * @typeParam T - The type of the value that is produced by a successful operation.
20
17
  * @typeParam E - The type of the error that may be produced by a failed operation.
21
18
  */
22
- export type AsyncResult<T, E> = Promise<Result<T, E>>;
19
+ export type AsyncVoidResult<E> = Promise<VoidResult<E>>;
23
20
 
24
21
  /**
25
22
  * Represents a synchronous operation that yields a `Result<T, Error>`.
@@ -35,4 +32,14 @@ export type IOResult<T> = Result<T, Error>;
35
32
  *
36
33
  * @typeParam T - The type of the value that is produced by a successful I/O operation.
37
34
  */
38
- export type AsyncIOResult<T> = AsyncResult<T, Error>;
35
+ export type AsyncIOResult<T> = AsyncResult<T, Error>;
36
+
37
+ /**
38
+ * Similar to Rust's `Result<(), Error>`.
39
+ */
40
+ export type VoidIOResult = IOResult<void>;
41
+
42
+ /**
43
+ * `VoidIOResult` wrapped by `Promise`.
44
+ */
45
+ export type AsyncVoidIOResult = AsyncIOResult<void>;
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/no-unused-vars */
2
- import type { Option, Result } from './core.ts';
2
+ import type { AsyncOption, AsyncResult, Option, Result } from './core.ts';
3
3
  import { OptionKindSymbol, ResultKindSymbol } from './symbols.ts';
4
4
  import { isOption, isResult } from './utils.ts';
5
5
 
@@ -14,14 +14,26 @@ export interface None extends Option<never> {
14
14
 
15
15
  readonly [OptionKindSymbol]: 'None';
16
16
 
17
+ isSome(): false;
18
+ isNone(): true;
19
+ isSomeAnd(predicate: (value: never) => boolean): false;
20
+ isSomeAndAsync(predicate: (value: never) => Promise<boolean>): Promise<false>;
21
+
22
+ expect(msg: string): never;
23
+ unwrap(): never;
17
24
  unwrapOr<T>(defaultValue: T): T;
18
25
  unwrapOrElse<T>(fn: () => T): T;
26
+ unwrapOrElseAsync<T>(fn: () => Promise<T>): Promise<T>;
19
27
 
28
+ okOr<E>(error: E): Result<never, E>;
29
+ okOrElse<E>(err: () => E): Result<never, E>;
20
30
  transpose(): Result<None, never>;
21
31
 
22
32
  filter(predicate: (value: never) => boolean): None;
23
33
  flatten(): None;
24
34
  map<U>(fn: (value: never) => U): None;
35
+ mapOr<U>(defaultValue: U, fn: (value: never) => U): U;
36
+ mapOrElse<U>(defaultFn: () => U, fn: (value: never) => U): U;
25
37
 
26
38
  zip<U>(other: Option<U>): None;
27
39
  zipWith<U, R>(other: Option<U>, fn: (value: never, otherValue: U) => R): None;
@@ -29,10 +41,14 @@ export interface None extends Option<never> {
29
41
 
30
42
  and<U>(other: Option<U>): None;
31
43
  andThen<U>(fn: (value: never) => Option<U>): None;
44
+ andThenAsync<U>(fn: (value: never) => AsyncOption<U>): Promise<None>;
32
45
  or<T>(other: Option<T>): Option<T>;
33
46
  orElse<T>(fn: () => Option<T>): Option<T>;
47
+ orElseAsync<T>(fn: () => AsyncOption<T>): AsyncOption<T>;
34
48
  xor<T>(other: Option<T>): Option<T>;
35
49
 
50
+ inspect(fn: (value: never) => void): this;
51
+
36
52
  eq<T>(other: Option<T>): boolean;
37
53
  }
38
54
 
@@ -66,6 +82,9 @@ export function Some<T>(value: T): Option<T> {
66
82
  isSomeAnd(predicate: (value: T) => boolean): boolean {
67
83
  return predicate(value);
68
84
  },
85
+ isSomeAndAsync(predicate: (value: T) => Promise<boolean>): Promise<boolean> {
86
+ return predicate(value);
87
+ },
69
88
 
70
89
  expect(_msg: string): T {
71
90
  return value;
@@ -79,6 +98,9 @@ export function Some<T>(value: T): Option<T> {
79
98
  unwrapOrElse(_fn: () => T): T {
80
99
  return value;
81
100
  },
101
+ unwrapOrElseAsync(_fn: () => Promise<T>): Promise<T> {
102
+ return Promise.resolve(value);
103
+ },
82
104
 
83
105
  okOr<E>(_error: E): Result<T, E> {
84
106
  return Ok(value);
@@ -137,12 +159,18 @@ export function Some<T>(value: T): Option<T> {
137
159
  andThen<U>(fn: (value: T) => Option<U>): Option<U> {
138
160
  return fn(value);
139
161
  },
162
+ andThenAsync<U>(fn: (value: T) => AsyncOption<U>): AsyncOption<U> {
163
+ return fn(value);
164
+ },
140
165
  or(_other: Option<T>): Option<T> {
141
166
  return some;
142
167
  },
143
168
  orElse(_fn: () => Option<T>): Option<T> {
144
169
  return some;
145
170
  },
171
+ orElseAsync(_fn: () => AsyncOption<T>): AsyncOption<T> {
172
+ return Promise.resolve(some);
173
+ },
146
174
  xor(other: Option<T>): Option<T> {
147
175
  assertOption(other);
148
176
  return other.isSome() ? None : some;
@@ -183,6 +211,9 @@ export const None = Object.freeze<None>({
183
211
  isSomeAnd(_predicate: (value: never) => boolean): false {
184
212
  return false;
185
213
  },
214
+ isSomeAndAsync(_predicate: (value: never) => Promise<boolean>): Promise<false> {
215
+ return Promise.resolve(false);
216
+ },
186
217
 
187
218
  expect(msg: string): never {
188
219
  throw new TypeError(msg);
@@ -196,6 +227,9 @@ export const None = Object.freeze<None>({
196
227
  unwrapOrElse<T>(fn: () => T): T {
197
228
  return fn();
198
229
  },
230
+ unwrapOrElseAsync<T>(fn: () => Promise<T>): Promise<T> {
231
+ return fn();
232
+ },
199
233
 
200
234
  okOr<E>(error: E): Result<never, E> {
201
235
  return Err(error);
@@ -240,6 +274,9 @@ export const None = Object.freeze<None>({
240
274
  andThen<U>(_fn: (value: never) => Option<U>): None {
241
275
  return None;
242
276
  },
277
+ andThenAsync<U>(_fn: (value: never) => AsyncOption<U>): Promise<None> {
278
+ return Promise.resolve(None);
279
+ },
243
280
  or<T>(other: Option<T>): Option<T> {
244
281
  assertOption(other);
245
282
  return other;
@@ -247,6 +284,9 @@ export const None = Object.freeze<None>({
247
284
  orElse<T>(fn: () => Option<T>): Option<T> {
248
285
  return fn();
249
286
  },
287
+ orElseAsync<T>(fn: () => AsyncOption<T>): AsyncOption<T> {
288
+ return fn();
289
+ },
250
290
  xor<T>(other: Option<T>): Option<T> {
251
291
  assertOption(other);
252
292
  return other.isSome() ? other : None;
@@ -283,7 +323,12 @@ export const None = Object.freeze<None>({
283
323
  * }
284
324
  * ```
285
325
  */
286
- export function Ok<T, E>(value: T): Result<T, E> {
326
+ export function Ok<T, E>(value: T): Result<T, E>;
327
+ /**
328
+ * Because javascript does not have a `()` type, use `void` instead.
329
+ */
330
+ export function Ok<E>(): Result<void, E>;
331
+ export function Ok<T, E>(value?: T): Result<T, E> {
287
332
  const ok: Result<T, E> = {
288
333
  [Symbol.toStringTag]: 'Result',
289
334
  [ResultKindSymbol]: 'Ok',
@@ -295,23 +340,32 @@ export function Ok<T, E>(value: T): Result<T, E> {
295
340
  return false;
296
341
  },
297
342
  isOkAnd(predicate: (value: T) => boolean): boolean {
298
- return predicate(value);
343
+ return predicate(value as T);
344
+ },
345
+ isOkAndAsync(predicate: (value: T) => Promise<boolean>): Promise<boolean> {
346
+ return predicate(value as T);
299
347
  },
300
348
  isErrAnd(_predicate: (error: E) => boolean): false {
301
349
  return false;
302
350
  },
351
+ isErrAndAsync(_predicate: (error: E) => Promise<boolean>): Promise<false> {
352
+ return Promise.resolve(false);
353
+ },
303
354
 
304
355
  expect(_msg: string): T {
305
- return value;
356
+ return value as T;
306
357
  },
307
358
  unwrap(): T {
308
- return value;
359
+ return value as T;
309
360
  },
310
361
  unwrapOr(_defaultValue: T): T {
311
- return value;
362
+ return value as T;
312
363
  },
313
364
  unwrapOrElse(_fn: (error: E) => T): T {
314
- return value;
365
+ return value as T;
366
+ },
367
+ unwrapOrElseAsync(_fn: (error: E) => Promise<T>): Promise<T> {
368
+ return Promise.resolve(value as T);
315
369
  },
316
370
 
317
371
  expectErr(msg: string): E {
@@ -322,7 +376,7 @@ export function Ok<T, E>(value: T): Result<T, E> {
322
376
  },
323
377
 
324
378
  ok(): Option<T> {
325
- return Some(value);
379
+ return Some(value as T);
326
380
  },
327
381
  err(): None {
328
382
  return None;
@@ -334,16 +388,16 @@ export function Ok<T, E>(value: T): Result<T, E> {
334
388
  },
335
389
 
336
390
  map<U>(fn: (value: T) => U): Result<U, E> {
337
- return Ok(fn(value));
391
+ return Ok(fn(value as T));
338
392
  },
339
393
  mapErr<F>(_fn: (error: E) => F): Result<T, F> {
340
- return Ok(value);
394
+ return Ok(value as T);
341
395
  },
342
396
  mapOr<U>(_defaultValue: U, fn: (value: T) => U): U {
343
- return fn(value);
397
+ return fn(value as T);
344
398
  },
345
399
  mapOrElse<U>(_defaultFn: (error: E) => U, fn: (value: T) => U): U {
346
- return fn(value);
400
+ return fn(value as T);
347
401
  },
348
402
  flatten<T>(): Result<T, E> {
349
403
  const r = value as Result<T, E>;
@@ -359,14 +413,20 @@ export function Ok<T, E>(value: T): Result<T, E> {
359
413
  return ok as unknown as Result<T, F>;
360
414
  },
361
415
  andThen<U>(fn: (value: T) => Result<U, E>): Result<U, E> {
362
- return fn(value);
416
+ return fn(value as T);
417
+ },
418
+ andThenAsync<U>(fn: (value: T) => AsyncResult<U, E>): AsyncResult<U, E> {
419
+ return fn(value as T);
363
420
  },
364
421
  orElse<F>(_fn: (error: E) => Result<T, F>): Result<T, F> {
365
422
  return ok as unknown as Result<T, F>;
366
423
  },
424
+ orElseAsync<F>(_fn: (error: E) => AsyncResult<T, F>): AsyncResult<T, F> {
425
+ return Promise.resolve(ok as unknown as Result<T, F>);
426
+ },
367
427
 
368
428
  inspect(fn: (value: T) => void): Result<T, E> {
369
- fn(value);
429
+ fn(value as T);
370
430
  return ok;
371
431
  },
372
432
  inspectErr(_fn: (error: E) => void): Result<T, E> {
@@ -424,9 +484,15 @@ export function Err<T, E>(error: E): Result<T, E> {
424
484
  isOkAnd(_predicate: (value: T) => boolean): false {
425
485
  return false;
426
486
  },
487
+ isOkAndAsync(_predicate: (value: T) => Promise<boolean>): Promise<boolean> {
488
+ return Promise.resolve(false);
489
+ },
427
490
  isErrAnd(predicate: (error: E) => boolean): boolean {
428
491
  return predicate(error);
429
492
  },
493
+ isErrAndAsync(predicate: (error: E) => Promise<boolean>): Promise<boolean> {
494
+ return predicate(error);
495
+ },
430
496
 
431
497
  expect(msg: string): T {
432
498
  throw new TypeError(`${ msg }: ${ error }`);
@@ -440,6 +506,9 @@ export function Err<T, E>(error: E): Result<T, E> {
440
506
  unwrapOrElse(fn: (error: E) => T): T {
441
507
  return fn(error);
442
508
  },
509
+ unwrapOrElseAsync(fn: (error: E) => Promise<T>): Promise<T> {
510
+ return fn(error);
511
+ },
443
512
 
444
513
  expectErr(_msg: string): E {
445
514
  return error;
@@ -484,9 +553,15 @@ export function Err<T, E>(error: E): Result<T, E> {
484
553
  andThen<U>(_fn: (value: T) => Result<U, E>): Result<U, E> {
485
554
  return err as unknown as Result<U, E>;
486
555
  },
556
+ andThenAsync<U>(_fn: (value: T) => AsyncResult<U, E>): AsyncResult<U, E> {
557
+ return Promise.resolve(err as unknown as Result<U, E>);
558
+ },
487
559
  orElse<F>(fn: (error: E) => Result<T, F>): Result<T, F> {
488
560
  return fn(error);
489
561
  },
562
+ orElseAsync<F>(fn: (error: E) => AsyncResult<T, F>): AsyncResult<T, F> {
563
+ return fn(error);
564
+ },
490
565
 
491
566
  inspect(_fn: (value: T) => void): Result<T, E> {
492
567
  return err;