@sapphire/result 2.8.0-next.f3515ea3 → 2.8.1-next.11799975
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/CHANGELOG.md +7 -0
- package/dist/cjs/index.cjs +48 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +115 -1
- package/dist/esm/index.d.mts +115 -1
- package/dist/esm/index.mjs +48 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/iife/index.global.js +48 -0
- package/dist/iife/index.global.js.map +1 -1
- package/package.json +2 -2
package/dist/cjs/index.d.cts
CHANGED
|
@@ -3,6 +3,7 @@ type If<Value extends boolean, TrueResult, FalseResult> = Value extends true ? T
|
|
|
3
3
|
|
|
4
4
|
declare const ValueProperty$1: unique symbol;
|
|
5
5
|
declare const SuccessProperty: unique symbol;
|
|
6
|
+
declare const UnwrapSafeProperty: unique symbol;
|
|
6
7
|
/**
|
|
7
8
|
* A type used to express computations that can fail, it can be used for returning and propagating errors. This is a
|
|
8
9
|
* type union with the variants `Ok(T)`, representing success and containing a value, and `Err(E)`, representing error
|
|
@@ -441,6 +442,7 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
441
442
|
* @seealso {@link unwrapOrElse}
|
|
442
443
|
* @seealso {@link unwrapErr}
|
|
443
444
|
* @seealso {@link unwrapRaw}
|
|
445
|
+
* @seealso {@link unwrapSafe}
|
|
444
446
|
*
|
|
445
447
|
* @example
|
|
446
448
|
* ```typescript
|
|
@@ -468,6 +470,7 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
468
470
|
* @seealso {@link unwrapOr}
|
|
469
471
|
* @seealso {@link unwrapOrElse}
|
|
470
472
|
* @seealso {@link unwrapRaw}
|
|
473
|
+
* @seealso {@link unwrapSafe}
|
|
471
474
|
*
|
|
472
475
|
* @example
|
|
473
476
|
* ```typescript
|
|
@@ -496,6 +499,7 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
496
499
|
* @seealso {@link unwrapOrElse}
|
|
497
500
|
* @seealso {@link unwrapErr}
|
|
498
501
|
* @seealso {@link unwrapRaw}
|
|
502
|
+
* @seealso {@link unwrapSafe}
|
|
499
503
|
*
|
|
500
504
|
* @param defaultValue The default value.
|
|
501
505
|
*
|
|
@@ -519,6 +523,7 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
519
523
|
* @seealso {@link unwrapOr}
|
|
520
524
|
* @seealso {@link unwrapErr}
|
|
521
525
|
* @seealso {@link unwrapRaw}
|
|
526
|
+
* @seealso {@link unwrapSafe}
|
|
522
527
|
*
|
|
523
528
|
* @param op The predicate.
|
|
524
529
|
*
|
|
@@ -541,6 +546,7 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
541
546
|
* @seealso {@link unwrapOr}
|
|
542
547
|
* @seealso {@link unwrapOrElse}
|
|
543
548
|
* @seealso {@link unwrapErr}
|
|
549
|
+
* @seealso {@link unwrapSafe}
|
|
544
550
|
*
|
|
545
551
|
* @example
|
|
546
552
|
* ```typescript
|
|
@@ -558,6 +564,20 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
558
564
|
* ```
|
|
559
565
|
*/
|
|
560
566
|
unwrapRaw(): If<Success, T, never>;
|
|
567
|
+
/**
|
|
568
|
+
* Returns the contained `Ok` value or yelds the contained `Err` value.
|
|
569
|
+
* Emulates Rust's `?` operator in `safeTry`'s body. See also {@link Result.safeTry}.
|
|
570
|
+
*
|
|
571
|
+
* If used outside of a `safeTry`'s' body, throws the contained error.
|
|
572
|
+
* @seealso {@link unwrap}
|
|
573
|
+
* @seealso {@link unwrapOr}
|
|
574
|
+
* @seealso {@link unwrapErr}
|
|
575
|
+
* @seealso {@link unwrapRaw}
|
|
576
|
+
* @seealso {@link unwrapSafe}
|
|
577
|
+
*
|
|
578
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_safe}
|
|
579
|
+
*/
|
|
580
|
+
unwrapSafe(): Generator<Err<E, T>, T>;
|
|
561
581
|
/**
|
|
562
582
|
* Returns `result` if the result is `Ok`, otherwise returns the `Err` value of itself.
|
|
563
583
|
* @param result The result to check.
|
|
@@ -839,6 +859,14 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
839
859
|
*/
|
|
840
860
|
[Symbol.iterator](): Generator<T>;
|
|
841
861
|
get [Symbol.toStringTag](): If<Success, 'Ok', 'Err'>;
|
|
862
|
+
/**
|
|
863
|
+
* This function, in combination with `[$]`, is intended to emulate
|
|
864
|
+
* Rust's ? operator.
|
|
865
|
+
*
|
|
866
|
+
* @see {@link Result.safeTry}
|
|
867
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.safeTry}
|
|
868
|
+
*/
|
|
869
|
+
get [UnwrapSafeProperty](): Generator<Err<E, T>, T>;
|
|
842
870
|
static ok<T = undefined, E = any>(this: void, value?: T): Ok<T, E>;
|
|
843
871
|
static err<E = undefined, T = any>(this: void, value?: E): Err<E, T>;
|
|
844
872
|
/**
|
|
@@ -919,6 +947,88 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
919
947
|
* @returns A new {@link Result}.
|
|
920
948
|
*/
|
|
921
949
|
static any<const Entries extends readonly AnyResult[]>(this: void, results: Entries): Result<UnwrapOk<Entries[number]>, UnwrapErrArray<Entries>>;
|
|
950
|
+
/**
|
|
951
|
+
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
952
|
+
* whichever comes first.
|
|
953
|
+
*
|
|
954
|
+
* This function, in combination with `[$]`, is intended to emulate
|
|
955
|
+
* Rust's ? operator.
|
|
956
|
+
*
|
|
957
|
+
* @example
|
|
958
|
+
* ```typescript
|
|
959
|
+
* const result = Result.safeTry(function* ({ $ }) {
|
|
960
|
+
* const first = yield* ok(1)[$];
|
|
961
|
+
* const second = yield* ok(1)[$];
|
|
962
|
+
*
|
|
963
|
+
* return ok(first + second);
|
|
964
|
+
* });
|
|
965
|
+
*
|
|
966
|
+
* result.match({
|
|
967
|
+
* ok: (value) => value, // 2
|
|
968
|
+
* err: (error) => {}
|
|
969
|
+
* });
|
|
970
|
+
*```
|
|
971
|
+
*
|
|
972
|
+
* @example
|
|
973
|
+
* ```typescript
|
|
974
|
+
* const resultAsync = Result.safeTry(async function* ({ $async }) {
|
|
975
|
+
* const first = yield* $async(Result.fromAsync(() => Promise.resolve(1)));
|
|
976
|
+
* const second = yield* ok(1)[$];
|
|
977
|
+
*
|
|
978
|
+
* return ok(first + second);
|
|
979
|
+
* });
|
|
980
|
+
*
|
|
981
|
+
* resultAsync.match({
|
|
982
|
+
* ok: (value) => value, // 2
|
|
983
|
+
* err: (error) => {}
|
|
984
|
+
* });
|
|
985
|
+
* ```
|
|
986
|
+
* @param body - What is evaluated. In body, `yield* result[$]` works as
|
|
987
|
+
* Rust's `result?` expression.
|
|
988
|
+
* @returns The first occurence of either an yielded Err or a returned Result.
|
|
989
|
+
*/
|
|
990
|
+
static safeTry<T, E>(body: (options: SafeTryOptions) => Generator<Err<E>, Result<T, E>>): Result<T, E>;
|
|
991
|
+
/**
|
|
992
|
+
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
993
|
+
* whichever comes first.
|
|
994
|
+
*
|
|
995
|
+
* This function, in combination with `[$]`, is intended to emulate
|
|
996
|
+
* Rust's ? operator.
|
|
997
|
+
*
|
|
998
|
+
* @example
|
|
999
|
+
* ```typescript
|
|
1000
|
+
* const result = Result.safeTry(function* ({ $ }) {
|
|
1001
|
+
* const first = yield* ok(1)[$];
|
|
1002
|
+
* const second = yield* ok(1)[$];
|
|
1003
|
+
*
|
|
1004
|
+
* return ok(first + second);
|
|
1005
|
+
* });
|
|
1006
|
+
*
|
|
1007
|
+
* result.match({
|
|
1008
|
+
* ok: (value) => value, // 2
|
|
1009
|
+
* err: (error) => {}
|
|
1010
|
+
* });
|
|
1011
|
+
*```
|
|
1012
|
+
*
|
|
1013
|
+
* @example
|
|
1014
|
+
* ```typescript
|
|
1015
|
+
* const resultAsync = Result.safeTry(async function* ({ $async }) {
|
|
1016
|
+
* const first = yield* $async(Result.fromAsync(() => Promise.resolve(1)));
|
|
1017
|
+
* const second = yield* ok(1)[$];
|
|
1018
|
+
*
|
|
1019
|
+
* return ok(first + second);
|
|
1020
|
+
* });
|
|
1021
|
+
*
|
|
1022
|
+
* resultAsync.match({
|
|
1023
|
+
* ok: (value) => value, // 2
|
|
1024
|
+
* err: (error) => {}
|
|
1025
|
+
* });
|
|
1026
|
+
* ```
|
|
1027
|
+
* @param body - What is evaluated. In body, `yield* result[$]` works as
|
|
1028
|
+
* Rust's `result?` expression.
|
|
1029
|
+
* @returns The first occurence of either an yielded Err or a returned Result.
|
|
1030
|
+
*/
|
|
1031
|
+
static safeTry<T, E>(body: (options: SafeTryOptions) => AsyncGenerator<Err<E>, Result<T, E>>): Promise<Result<T, E>>;
|
|
922
1032
|
}
|
|
923
1033
|
declare namespace Result {
|
|
924
1034
|
type Ok<T, E = any> = Result<T, E, true>;
|
|
@@ -944,6 +1054,10 @@ type UnwrapOk<T extends AnyResult> = Result.UnwrapOk<T>;
|
|
|
944
1054
|
type UnwrapErr<T extends AnyResult> = Result.UnwrapErr<T>;
|
|
945
1055
|
type UnwrapOkArray<T extends readonly AnyResult[] | []> = Result.UnwrapOkArray<T>;
|
|
946
1056
|
type UnwrapErrArray<T extends readonly AnyResult[] | []> = Result.UnwrapErrArray<T>;
|
|
1057
|
+
interface SafeTryOptions {
|
|
1058
|
+
$: typeof UnwrapSafeProperty;
|
|
1059
|
+
$async: <T, E>(result: Promise<Result<T, E>>) => AsyncGenerator<Err<E>, T>;
|
|
1060
|
+
}
|
|
947
1061
|
|
|
948
1062
|
declare const ValueProperty: unique symbol;
|
|
949
1063
|
declare const ExistsProperty: unique symbol;
|
|
@@ -1793,4 +1907,4 @@ declare class ResultError<E> extends Error {
|
|
|
1793
1907
|
get name(): string;
|
|
1794
1908
|
}
|
|
1795
1909
|
|
|
1796
|
-
export { type AnyOption, type AnyResult, type Err, type None, type Ok, Option, OptionError, type OptionResolvable, Result, ResultError, type ResultResolvable, type Some, type UnwrapErr, type UnwrapErrArray, type UnwrapOk, type UnwrapOkArray, type UnwrapSome, type UnwrapSomeArray, err, none, ok, some };
|
|
1910
|
+
export { type AnyOption, type AnyResult, type Err, type None, type Ok, Option, OptionError, type OptionResolvable, Result, ResultError, type ResultResolvable, type SafeTryOptions, type Some, type UnwrapErr, type UnwrapErrArray, type UnwrapOk, type UnwrapOkArray, type UnwrapSome, type UnwrapSomeArray, err, none, ok, some };
|
package/dist/esm/index.d.mts
CHANGED
|
@@ -3,6 +3,7 @@ type If<Value extends boolean, TrueResult, FalseResult> = Value extends true ? T
|
|
|
3
3
|
|
|
4
4
|
declare const ValueProperty$1: unique symbol;
|
|
5
5
|
declare const SuccessProperty: unique symbol;
|
|
6
|
+
declare const UnwrapSafeProperty: unique symbol;
|
|
6
7
|
/**
|
|
7
8
|
* A type used to express computations that can fail, it can be used for returning and propagating errors. This is a
|
|
8
9
|
* type union with the variants `Ok(T)`, representing success and containing a value, and `Err(E)`, representing error
|
|
@@ -441,6 +442,7 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
441
442
|
* @seealso {@link unwrapOrElse}
|
|
442
443
|
* @seealso {@link unwrapErr}
|
|
443
444
|
* @seealso {@link unwrapRaw}
|
|
445
|
+
* @seealso {@link unwrapSafe}
|
|
444
446
|
*
|
|
445
447
|
* @example
|
|
446
448
|
* ```typescript
|
|
@@ -468,6 +470,7 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
468
470
|
* @seealso {@link unwrapOr}
|
|
469
471
|
* @seealso {@link unwrapOrElse}
|
|
470
472
|
* @seealso {@link unwrapRaw}
|
|
473
|
+
* @seealso {@link unwrapSafe}
|
|
471
474
|
*
|
|
472
475
|
* @example
|
|
473
476
|
* ```typescript
|
|
@@ -496,6 +499,7 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
496
499
|
* @seealso {@link unwrapOrElse}
|
|
497
500
|
* @seealso {@link unwrapErr}
|
|
498
501
|
* @seealso {@link unwrapRaw}
|
|
502
|
+
* @seealso {@link unwrapSafe}
|
|
499
503
|
*
|
|
500
504
|
* @param defaultValue The default value.
|
|
501
505
|
*
|
|
@@ -519,6 +523,7 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
519
523
|
* @seealso {@link unwrapOr}
|
|
520
524
|
* @seealso {@link unwrapErr}
|
|
521
525
|
* @seealso {@link unwrapRaw}
|
|
526
|
+
* @seealso {@link unwrapSafe}
|
|
522
527
|
*
|
|
523
528
|
* @param op The predicate.
|
|
524
529
|
*
|
|
@@ -541,6 +546,7 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
541
546
|
* @seealso {@link unwrapOr}
|
|
542
547
|
* @seealso {@link unwrapOrElse}
|
|
543
548
|
* @seealso {@link unwrapErr}
|
|
549
|
+
* @seealso {@link unwrapSafe}
|
|
544
550
|
*
|
|
545
551
|
* @example
|
|
546
552
|
* ```typescript
|
|
@@ -558,6 +564,20 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
558
564
|
* ```
|
|
559
565
|
*/
|
|
560
566
|
unwrapRaw(): If<Success, T, never>;
|
|
567
|
+
/**
|
|
568
|
+
* Returns the contained `Ok` value or yelds the contained `Err` value.
|
|
569
|
+
* Emulates Rust's `?` operator in `safeTry`'s body. See also {@link Result.safeTry}.
|
|
570
|
+
*
|
|
571
|
+
* If used outside of a `safeTry`'s' body, throws the contained error.
|
|
572
|
+
* @seealso {@link unwrap}
|
|
573
|
+
* @seealso {@link unwrapOr}
|
|
574
|
+
* @seealso {@link unwrapErr}
|
|
575
|
+
* @seealso {@link unwrapRaw}
|
|
576
|
+
* @seealso {@link unwrapSafe}
|
|
577
|
+
*
|
|
578
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_safe}
|
|
579
|
+
*/
|
|
580
|
+
unwrapSafe(): Generator<Err<E, T>, T>;
|
|
561
581
|
/**
|
|
562
582
|
* Returns `result` if the result is `Ok`, otherwise returns the `Err` value of itself.
|
|
563
583
|
* @param result The result to check.
|
|
@@ -839,6 +859,14 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
839
859
|
*/
|
|
840
860
|
[Symbol.iterator](): Generator<T>;
|
|
841
861
|
get [Symbol.toStringTag](): If<Success, 'Ok', 'Err'>;
|
|
862
|
+
/**
|
|
863
|
+
* This function, in combination with `[$]`, is intended to emulate
|
|
864
|
+
* Rust's ? operator.
|
|
865
|
+
*
|
|
866
|
+
* @see {@link Result.safeTry}
|
|
867
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.safeTry}
|
|
868
|
+
*/
|
|
869
|
+
get [UnwrapSafeProperty](): Generator<Err<E, T>, T>;
|
|
842
870
|
static ok<T = undefined, E = any>(this: void, value?: T): Ok<T, E>;
|
|
843
871
|
static err<E = undefined, T = any>(this: void, value?: E): Err<E, T>;
|
|
844
872
|
/**
|
|
@@ -919,6 +947,88 @@ declare class Result<T, E, const Success extends boolean = boolean> {
|
|
|
919
947
|
* @returns A new {@link Result}.
|
|
920
948
|
*/
|
|
921
949
|
static any<const Entries extends readonly AnyResult[]>(this: void, results: Entries): Result<UnwrapOk<Entries[number]>, UnwrapErrArray<Entries>>;
|
|
950
|
+
/**
|
|
951
|
+
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
952
|
+
* whichever comes first.
|
|
953
|
+
*
|
|
954
|
+
* This function, in combination with `[$]`, is intended to emulate
|
|
955
|
+
* Rust's ? operator.
|
|
956
|
+
*
|
|
957
|
+
* @example
|
|
958
|
+
* ```typescript
|
|
959
|
+
* const result = Result.safeTry(function* ({ $ }) {
|
|
960
|
+
* const first = yield* ok(1)[$];
|
|
961
|
+
* const second = yield* ok(1)[$];
|
|
962
|
+
*
|
|
963
|
+
* return ok(first + second);
|
|
964
|
+
* });
|
|
965
|
+
*
|
|
966
|
+
* result.match({
|
|
967
|
+
* ok: (value) => value, // 2
|
|
968
|
+
* err: (error) => {}
|
|
969
|
+
* });
|
|
970
|
+
*```
|
|
971
|
+
*
|
|
972
|
+
* @example
|
|
973
|
+
* ```typescript
|
|
974
|
+
* const resultAsync = Result.safeTry(async function* ({ $async }) {
|
|
975
|
+
* const first = yield* $async(Result.fromAsync(() => Promise.resolve(1)));
|
|
976
|
+
* const second = yield* ok(1)[$];
|
|
977
|
+
*
|
|
978
|
+
* return ok(first + second);
|
|
979
|
+
* });
|
|
980
|
+
*
|
|
981
|
+
* resultAsync.match({
|
|
982
|
+
* ok: (value) => value, // 2
|
|
983
|
+
* err: (error) => {}
|
|
984
|
+
* });
|
|
985
|
+
* ```
|
|
986
|
+
* @param body - What is evaluated. In body, `yield* result[$]` works as
|
|
987
|
+
* Rust's `result?` expression.
|
|
988
|
+
* @returns The first occurence of either an yielded Err or a returned Result.
|
|
989
|
+
*/
|
|
990
|
+
static safeTry<T, E>(body: (options: SafeTryOptions) => Generator<Err<E>, Result<T, E>>): Result<T, E>;
|
|
991
|
+
/**
|
|
992
|
+
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
993
|
+
* whichever comes first.
|
|
994
|
+
*
|
|
995
|
+
* This function, in combination with `[$]`, is intended to emulate
|
|
996
|
+
* Rust's ? operator.
|
|
997
|
+
*
|
|
998
|
+
* @example
|
|
999
|
+
* ```typescript
|
|
1000
|
+
* const result = Result.safeTry(function* ({ $ }) {
|
|
1001
|
+
* const first = yield* ok(1)[$];
|
|
1002
|
+
* const second = yield* ok(1)[$];
|
|
1003
|
+
*
|
|
1004
|
+
* return ok(first + second);
|
|
1005
|
+
* });
|
|
1006
|
+
*
|
|
1007
|
+
* result.match({
|
|
1008
|
+
* ok: (value) => value, // 2
|
|
1009
|
+
* err: (error) => {}
|
|
1010
|
+
* });
|
|
1011
|
+
*```
|
|
1012
|
+
*
|
|
1013
|
+
* @example
|
|
1014
|
+
* ```typescript
|
|
1015
|
+
* const resultAsync = Result.safeTry(async function* ({ $async }) {
|
|
1016
|
+
* const first = yield* $async(Result.fromAsync(() => Promise.resolve(1)));
|
|
1017
|
+
* const second = yield* ok(1)[$];
|
|
1018
|
+
*
|
|
1019
|
+
* return ok(first + second);
|
|
1020
|
+
* });
|
|
1021
|
+
*
|
|
1022
|
+
* resultAsync.match({
|
|
1023
|
+
* ok: (value) => value, // 2
|
|
1024
|
+
* err: (error) => {}
|
|
1025
|
+
* });
|
|
1026
|
+
* ```
|
|
1027
|
+
* @param body - What is evaluated. In body, `yield* result[$]` works as
|
|
1028
|
+
* Rust's `result?` expression.
|
|
1029
|
+
* @returns The first occurence of either an yielded Err or a returned Result.
|
|
1030
|
+
*/
|
|
1031
|
+
static safeTry<T, E>(body: (options: SafeTryOptions) => AsyncGenerator<Err<E>, Result<T, E>>): Promise<Result<T, E>>;
|
|
922
1032
|
}
|
|
923
1033
|
declare namespace Result {
|
|
924
1034
|
type Ok<T, E = any> = Result<T, E, true>;
|
|
@@ -944,6 +1054,10 @@ type UnwrapOk<T extends AnyResult> = Result.UnwrapOk<T>;
|
|
|
944
1054
|
type UnwrapErr<T extends AnyResult> = Result.UnwrapErr<T>;
|
|
945
1055
|
type UnwrapOkArray<T extends readonly AnyResult[] | []> = Result.UnwrapOkArray<T>;
|
|
946
1056
|
type UnwrapErrArray<T extends readonly AnyResult[] | []> = Result.UnwrapErrArray<T>;
|
|
1057
|
+
interface SafeTryOptions {
|
|
1058
|
+
$: typeof UnwrapSafeProperty;
|
|
1059
|
+
$async: <T, E>(result: Promise<Result<T, E>>) => AsyncGenerator<Err<E>, T>;
|
|
1060
|
+
}
|
|
947
1061
|
|
|
948
1062
|
declare const ValueProperty: unique symbol;
|
|
949
1063
|
declare const ExistsProperty: unique symbol;
|
|
@@ -1793,4 +1907,4 @@ declare class ResultError<E> extends Error {
|
|
|
1793
1907
|
get name(): string;
|
|
1794
1908
|
}
|
|
1795
1909
|
|
|
1796
|
-
export { type AnyOption, type AnyResult, type Err, type None, type Ok, Option, OptionError, type OptionResolvable, Result, ResultError, type ResultResolvable, type Some, type UnwrapErr, type UnwrapErrArray, type UnwrapOk, type UnwrapOkArray, type UnwrapSome, type UnwrapSomeArray, err, none, ok, some };
|
|
1910
|
+
export { type AnyOption, type AnyResult, type Err, type None, type Ok, Option, OptionError, type OptionResolvable, Result, ResultError, type ResultResolvable, type SafeTryOptions, type Some, type UnwrapErr, type UnwrapErrArray, type UnwrapOk, type UnwrapOkArray, type UnwrapSome, type UnwrapSomeArray, err, none, ok, some };
|
package/dist/esm/index.mjs
CHANGED
|
@@ -39,6 +39,7 @@ var ResultError = _ResultError;
|
|
|
39
39
|
// src/lib/Result.ts
|
|
40
40
|
var ValueProperty = Symbol.for("@sapphire/result:Result.value");
|
|
41
41
|
var SuccessProperty = Symbol.for("@sapphire/result:Result.success");
|
|
42
|
+
var UnwrapSafeProperty = Symbol.for("@sapphire/result:Result.safeUnwrap");
|
|
42
43
|
var _a, _b;
|
|
43
44
|
var _Result = class _Result {
|
|
44
45
|
constructor(value, success) {
|
|
@@ -465,6 +466,7 @@ var _Result = class _Result {
|
|
|
465
466
|
* @seealso {@link unwrapOrElse}
|
|
466
467
|
* @seealso {@link unwrapErr}
|
|
467
468
|
* @seealso {@link unwrapRaw}
|
|
469
|
+
* @seealso {@link unwrapSafe}
|
|
468
470
|
*
|
|
469
471
|
* @example
|
|
470
472
|
* ```typescript
|
|
@@ -495,6 +497,7 @@ var _Result = class _Result {
|
|
|
495
497
|
* @seealso {@link unwrapOr}
|
|
496
498
|
* @seealso {@link unwrapOrElse}
|
|
497
499
|
* @seealso {@link unwrapRaw}
|
|
500
|
+
* @seealso {@link unwrapSafe}
|
|
498
501
|
*
|
|
499
502
|
* @example
|
|
500
503
|
* ```typescript
|
|
@@ -526,6 +529,7 @@ var _Result = class _Result {
|
|
|
526
529
|
* @seealso {@link unwrapOrElse}
|
|
527
530
|
* @seealso {@link unwrapErr}
|
|
528
531
|
* @seealso {@link unwrapRaw}
|
|
532
|
+
* @seealso {@link unwrapSafe}
|
|
529
533
|
*
|
|
530
534
|
* @param defaultValue The default value.
|
|
531
535
|
*
|
|
@@ -551,6 +555,7 @@ var _Result = class _Result {
|
|
|
551
555
|
* @seealso {@link unwrapOr}
|
|
552
556
|
* @seealso {@link unwrapErr}
|
|
553
557
|
* @seealso {@link unwrapRaw}
|
|
558
|
+
* @seealso {@link unwrapSafe}
|
|
554
559
|
*
|
|
555
560
|
* @param op The predicate.
|
|
556
561
|
*
|
|
@@ -575,6 +580,7 @@ var _Result = class _Result {
|
|
|
575
580
|
* @seealso {@link unwrapOr}
|
|
576
581
|
* @seealso {@link unwrapOrElse}
|
|
577
582
|
* @seealso {@link unwrapErr}
|
|
583
|
+
* @seealso {@link unwrapSafe}
|
|
578
584
|
*
|
|
579
585
|
* @example
|
|
580
586
|
* ```typescript
|
|
@@ -595,6 +601,26 @@ var _Result = class _Result {
|
|
|
595
601
|
if (this.isErr()) throw this[ValueProperty];
|
|
596
602
|
return this[ValueProperty];
|
|
597
603
|
}
|
|
604
|
+
/**
|
|
605
|
+
* Returns the contained `Ok` value or yelds the contained `Err` value.
|
|
606
|
+
* Emulates Rust's `?` operator in `safeTry`'s body. See also {@link Result.safeTry}.
|
|
607
|
+
*
|
|
608
|
+
* If used outside of a `safeTry`'s' body, throws the contained error.
|
|
609
|
+
* @seealso {@link unwrap}
|
|
610
|
+
* @seealso {@link unwrapOr}
|
|
611
|
+
* @seealso {@link unwrapErr}
|
|
612
|
+
* @seealso {@link unwrapRaw}
|
|
613
|
+
* @seealso {@link unwrapSafe}
|
|
614
|
+
*
|
|
615
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_safe}
|
|
616
|
+
*/
|
|
617
|
+
*unwrapSafe() {
|
|
618
|
+
if (this.isOk()) {
|
|
619
|
+
return this[ValueProperty];
|
|
620
|
+
}
|
|
621
|
+
yield this;
|
|
622
|
+
throw new ResultError("Should not be used outside of a safe try generator", this[ValueProperty]);
|
|
623
|
+
}
|
|
598
624
|
/**
|
|
599
625
|
* Returns `result` if the result is `Ok`, otherwise returns the `Err` value of itself.
|
|
600
626
|
* @param result The result to check.
|
|
@@ -869,6 +895,16 @@ var _Result = class _Result {
|
|
|
869
895
|
get [Symbol.toStringTag]() {
|
|
870
896
|
return this.match({ ok: /* @__PURE__ */ __name(() => "Ok", "ok"), err: /* @__PURE__ */ __name(() => "Err", "err") });
|
|
871
897
|
}
|
|
898
|
+
/**
|
|
899
|
+
* This function, in combination with `[$]`, is intended to emulate
|
|
900
|
+
* Rust's ? operator.
|
|
901
|
+
*
|
|
902
|
+
* @see {@link Result.safeTry}
|
|
903
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.safeTry}
|
|
904
|
+
*/
|
|
905
|
+
get [UnwrapSafeProperty]() {
|
|
906
|
+
return this.unwrapSafe();
|
|
907
|
+
}
|
|
872
908
|
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
873
909
|
static ok(value) {
|
|
874
910
|
return new _Result(value, true);
|
|
@@ -988,6 +1024,13 @@ var _Result = class _Result {
|
|
|
988
1024
|
}
|
|
989
1025
|
return err(errors);
|
|
990
1026
|
}
|
|
1027
|
+
static safeTry(body) {
|
|
1028
|
+
const n = body({ $: UnwrapSafeProperty, $async: unwrapSafeAsync }).next();
|
|
1029
|
+
if (n instanceof Promise) {
|
|
1030
|
+
return n.then((r) => r.value);
|
|
1031
|
+
}
|
|
1032
|
+
return n.value;
|
|
1033
|
+
}
|
|
991
1034
|
};
|
|
992
1035
|
__name(_Result, "Result");
|
|
993
1036
|
var Result = _Result;
|
|
@@ -996,6 +1039,11 @@ function resolve(value) {
|
|
|
996
1039
|
return Result.isResult(value) ? value : ok(value);
|
|
997
1040
|
}
|
|
998
1041
|
__name(resolve, "resolve");
|
|
1042
|
+
async function* unwrapSafeAsync(result) {
|
|
1043
|
+
const _result = await result;
|
|
1044
|
+
return yield* _result.unwrapSafe();
|
|
1045
|
+
}
|
|
1046
|
+
__name(unwrapSafeAsync, "unwrapSafeAsync");
|
|
999
1047
|
|
|
1000
1048
|
// src/lib/Option.ts
|
|
1001
1049
|
var ValueProperty2 = Symbol.for("@sapphire/result:Option.value");
|