@sapphire/result 2.8.0-next.f3515ea3 → 2.8.1-next.8494bbf0

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 CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ # [@sapphire/result@2.8.0](https://github.com/sapphiredev/utilities/compare/@sapphire/result@2.7.3...@sapphire/result@2.8.0) - (2025-11-08)
6
+
7
+ ## 🚀 Features
8
+
9
+ - Add safeTry to emulate rust ? operator ([6abc552](https://github.com/sapphiredev/utilities/commit/6abc55224deb9de8f54896227b4906b133c5a2b6)) ([#917](https://github.com/sapphiredev/utilities/pull/917) by @feelsantiago)
10
+ - Improve result and options types ([f3515ea](https://github.com/sapphiredev/utilities/commit/f3515ea3b465eb8e4e0cdce4bd0c5189c67570c6)) ([#918](https://github.com/sapphiredev/utilities/pull/918) by @feelsantiago)
11
+
5
12
  # [@sapphire/result@2.7.3](https://github.com/sapphiredev/utilities/compare/@sapphire/result@2.7.2...@sapphire/result@2.7.3) - (2025-09-24)
6
13
 
7
14
  ## 🐛 Bug Fixes
@@ -41,6 +41,7 @@ var ResultError = _ResultError;
41
41
  // src/lib/Result.ts
42
42
  var ValueProperty = Symbol.for("@sapphire/result:Result.value");
43
43
  var SuccessProperty = Symbol.for("@sapphire/result:Result.success");
44
+ var UnwrapSafeProperty = Symbol.for("@sapphire/result:Result.safeUnwrap");
44
45
  var _a, _b;
45
46
  var _Result = class _Result {
46
47
  constructor(value, success) {
@@ -467,6 +468,7 @@ var _Result = class _Result {
467
468
  * @seealso {@link unwrapOrElse}
468
469
  * @seealso {@link unwrapErr}
469
470
  * @seealso {@link unwrapRaw}
471
+ * @seealso {@link unwrapSafe}
470
472
  *
471
473
  * @example
472
474
  * ```typescript
@@ -497,6 +499,7 @@ var _Result = class _Result {
497
499
  * @seealso {@link unwrapOr}
498
500
  * @seealso {@link unwrapOrElse}
499
501
  * @seealso {@link unwrapRaw}
502
+ * @seealso {@link unwrapSafe}
500
503
  *
501
504
  * @example
502
505
  * ```typescript
@@ -528,6 +531,7 @@ var _Result = class _Result {
528
531
  * @seealso {@link unwrapOrElse}
529
532
  * @seealso {@link unwrapErr}
530
533
  * @seealso {@link unwrapRaw}
534
+ * @seealso {@link unwrapSafe}
531
535
  *
532
536
  * @param defaultValue The default value.
533
537
  *
@@ -553,6 +557,7 @@ var _Result = class _Result {
553
557
  * @seealso {@link unwrapOr}
554
558
  * @seealso {@link unwrapErr}
555
559
  * @seealso {@link unwrapRaw}
560
+ * @seealso {@link unwrapSafe}
556
561
  *
557
562
  * @param op The predicate.
558
563
  *
@@ -577,6 +582,7 @@ var _Result = class _Result {
577
582
  * @seealso {@link unwrapOr}
578
583
  * @seealso {@link unwrapOrElse}
579
584
  * @seealso {@link unwrapErr}
585
+ * @seealso {@link unwrapSafe}
580
586
  *
581
587
  * @example
582
588
  * ```typescript
@@ -597,6 +603,26 @@ var _Result = class _Result {
597
603
  if (this.isErr()) throw this[ValueProperty];
598
604
  return this[ValueProperty];
599
605
  }
606
+ /**
607
+ * Returns the contained `Ok` value or yelds the contained `Err` value.
608
+ * Emulates Rust's `?` operator in `safeTry`'s body. See also {@link Result.safeTry}.
609
+ *
610
+ * If used outside of a `safeTry`'s' body, throws the contained error.
611
+ * @seealso {@link unwrap}
612
+ * @seealso {@link unwrapOr}
613
+ * @seealso {@link unwrapErr}
614
+ * @seealso {@link unwrapRaw}
615
+ * @seealso {@link unwrapSafe}
616
+ *
617
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_safe}
618
+ */
619
+ *unwrapSafe() {
620
+ if (this.isOk()) {
621
+ return this[ValueProperty];
622
+ }
623
+ yield this;
624
+ throw new ResultError("Should not be used outside of a safe try generator", this[ValueProperty]);
625
+ }
600
626
  /**
601
627
  * Returns `result` if the result is `Ok`, otherwise returns the `Err` value of itself.
602
628
  * @param result The result to check.
@@ -871,6 +897,16 @@ var _Result = class _Result {
871
897
  get [Symbol.toStringTag]() {
872
898
  return this.match({ ok: /* @__PURE__ */ __name(() => "Ok", "ok"), err: /* @__PURE__ */ __name(() => "Err", "err") });
873
899
  }
900
+ /**
901
+ * This function, in combination with `[$]`, is intended to emulate
902
+ * Rust's ? operator.
903
+ *
904
+ * @see {@link Result.safeTry}
905
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.safeTry}
906
+ */
907
+ get [UnwrapSafeProperty]() {
908
+ return this.unwrapSafe();
909
+ }
874
910
  // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
875
911
  static ok(value) {
876
912
  return new _Result(value, true);
@@ -990,6 +1026,13 @@ var _Result = class _Result {
990
1026
  }
991
1027
  return err(errors);
992
1028
  }
1029
+ static safeTry(body) {
1030
+ const n = body({ $: UnwrapSafeProperty, $async: unwrapSafeAsync }).next();
1031
+ if (n instanceof Promise) {
1032
+ return n.then((r) => r.value);
1033
+ }
1034
+ return n.value;
1035
+ }
993
1036
  };
994
1037
  __name(_Result, "Result");
995
1038
  var Result = _Result;
@@ -998,6 +1041,11 @@ function resolve(value) {
998
1041
  return Result.isResult(value) ? value : ok(value);
999
1042
  }
1000
1043
  __name(resolve, "resolve");
1044
+ async function* unwrapSafeAsync(result) {
1045
+ const _result = await result;
1046
+ return yield* _result.unwrapSafe();
1047
+ }
1048
+ __name(unwrapSafeAsync, "unwrapSafeAsync");
1001
1049
 
1002
1050
  // src/lib/Option.ts
1003
1051
  var ValueProperty2 = Symbol.for("@sapphire/result:Option.value");