@sapphire/result 2.7.4-next.ee9cdb94 → 2.8.0-next.6abc5522

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.
@@ -42,6 +42,7 @@ var SapphireResult = (function (exports) {
42
42
  // src/lib/Result.ts
43
43
  var ValueProperty = Symbol.for("@sapphire/result:Result.value");
44
44
  var SuccessProperty = Symbol.for("@sapphire/result:Result.success");
45
+ var UnwrapSafeProperty = Symbol.for("@sapphire/result:Result.safeUnwrap");
45
46
  var _a, _b;
46
47
  var _Result = class _Result {
47
48
  constructor(value, success) {
@@ -468,6 +469,7 @@ var SapphireResult = (function (exports) {
468
469
  * @seealso {@link unwrapOrElse}
469
470
  * @seealso {@link unwrapErr}
470
471
  * @seealso {@link unwrapRaw}
472
+ * @seealso {@link unwrapSafe}
471
473
  *
472
474
  * @example
473
475
  * ```typescript
@@ -498,6 +500,7 @@ var SapphireResult = (function (exports) {
498
500
  * @seealso {@link unwrapOr}
499
501
  * @seealso {@link unwrapOrElse}
500
502
  * @seealso {@link unwrapRaw}
503
+ * @seealso {@link unwrapSafe}
501
504
  *
502
505
  * @example
503
506
  * ```typescript
@@ -529,6 +532,7 @@ var SapphireResult = (function (exports) {
529
532
  * @seealso {@link unwrapOrElse}
530
533
  * @seealso {@link unwrapErr}
531
534
  * @seealso {@link unwrapRaw}
535
+ * @seealso {@link unwrapSafe}
532
536
  *
533
537
  * @param defaultValue The default value.
534
538
  *
@@ -554,6 +558,7 @@ var SapphireResult = (function (exports) {
554
558
  * @seealso {@link unwrapOr}
555
559
  * @seealso {@link unwrapErr}
556
560
  * @seealso {@link unwrapRaw}
561
+ * @seealso {@link unwrapSafe}
557
562
  *
558
563
  * @param op The predicate.
559
564
  *
@@ -578,6 +583,7 @@ var SapphireResult = (function (exports) {
578
583
  * @seealso {@link unwrapOr}
579
584
  * @seealso {@link unwrapOrElse}
580
585
  * @seealso {@link unwrapErr}
586
+ * @seealso {@link unwrapSafe}
581
587
  *
582
588
  * @example
583
589
  * ```typescript
@@ -598,6 +604,26 @@ var SapphireResult = (function (exports) {
598
604
  if (this.isErr()) throw this[ValueProperty];
599
605
  return this[ValueProperty];
600
606
  }
607
+ /**
608
+ * Returns the contained `Ok` value or yelds the contained `Err` value.
609
+ * Emulates Rust's `?` operator in `safeTry`'s body. See also {@link Result.safeTry}.
610
+ *
611
+ * If used outside of a `safeTry`'s' body, throws the contained error.
612
+ * @seealso {@link unwrap}
613
+ * @seealso {@link unwrapOr}
614
+ * @seealso {@link unwrapErr}
615
+ * @seealso {@link unwrapRaw}
616
+ * @seealso {@link unwrapSafe}
617
+ *
618
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_safe}
619
+ */
620
+ *unwrapSafe() {
621
+ if (this.isOk()) {
622
+ return this[ValueProperty];
623
+ }
624
+ yield this;
625
+ throw new ResultError("Should not be used outside of a safe try generator", this[ValueProperty]);
626
+ }
601
627
  /**
602
628
  * Returns `result` if the result is `Ok`, otherwise returns the `Err` value of itself.
603
629
  * @param result The result to check.
@@ -872,6 +898,16 @@ var SapphireResult = (function (exports) {
872
898
  get [Symbol.toStringTag]() {
873
899
  return this.match({ ok: /* @__PURE__ */ __name(() => "Ok", "ok"), err: /* @__PURE__ */ __name(() => "Err", "err") });
874
900
  }
901
+ /**
902
+ * This function, in combination with `[$]`, is intended to emulate
903
+ * Rust's ? operator.
904
+ *
905
+ * @see {@link Result.safeTry}
906
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.safeTry}
907
+ */
908
+ get [UnwrapSafeProperty]() {
909
+ return this.unwrapSafe();
910
+ }
875
911
  // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
876
912
  static ok(value) {
877
913
  return new _Result(value, true);
@@ -991,6 +1027,13 @@ var SapphireResult = (function (exports) {
991
1027
  }
992
1028
  return err(errors);
993
1029
  }
1030
+ static safeTry(body) {
1031
+ const n = body({ $: UnwrapSafeProperty, $async: unwrapSafeAsync }).next();
1032
+ if (n instanceof Promise) {
1033
+ return n.then((r) => r.value);
1034
+ }
1035
+ return n.value;
1036
+ }
994
1037
  };
995
1038
  __name(_Result, "Result");
996
1039
  var Result = _Result;
@@ -999,6 +1042,11 @@ var SapphireResult = (function (exports) {
999
1042
  return Result.isResult(value) ? value : ok(value);
1000
1043
  }
1001
1044
  __name(resolve, "resolve");
1045
+ async function* unwrapSafeAsync(result) {
1046
+ const _result = await result;
1047
+ return yield* _result.unwrapSafe();
1048
+ }
1049
+ __name(unwrapSafeAsync, "unwrapSafeAsync");
1002
1050
 
1003
1051
  // src/lib/Option.ts
1004
1052
  var ValueProperty2 = Symbol.for("@sapphire/result:Option.value");