@@ -146,6 +146,12 @@ def unwrap_or_raise(self, e: object) -> T:
146
146
"""
147
147
return self ._value
148
148
149
+ def unwrap_or_return (self ) -> T :
150
+ """
151
+ Return the value.
152
+ """
153
+ return self ._value
154
+
149
155
def map (self , op : Callable [[T ], U ]) -> Ok [U ]:
150
156
"""
151
157
The contained result is `Ok`, so return `Ok` with original value mapped to
@@ -358,6 +364,12 @@ def unwrap_or_raise(self, e: Type[TBE]) -> NoReturn:
358
364
"""
359
365
raise e (self ._value )
360
366
367
+ def unwrap_or_return (self ) -> NoReturn :
368
+ """
369
+ The contained result is ``Err``, raise DoException with self.
370
+ """
371
+ raise DoException (self )
372
+
361
373
def map (self , op : object ) -> Err [E ]:
362
374
"""
363
375
Return `Err` with the same value
@@ -464,6 +476,24 @@ def result(self) -> Result[Any, Any]:
464
476
return self ._result
465
477
466
478
479
+ def returns_result () -> Callable [[Callable [P , Result [R , E ]]], Callable [P , Result [R , E ]]]:
480
+ """
481
+ Make a decorator to turn a function into one that allows using unwrap_or_return.
482
+ """
483
+ def decorator (f : Callable [P , Result [R , E ]]) -> Callable [P , Result [R , E ]]:
484
+ @functools .wraps (f )
485
+ def wrapper (* args : P .args , ** kwargs : P .kwargs ) -> Result [R , E ]:
486
+ try :
487
+ return f (* args , ** kwargs )
488
+ except DoException as e :
489
+ out : Err [E ] = e .err # type: ignore
490
+ return out
491
+
492
+ return wrapper
493
+
494
+ return decorator
495
+
496
+
467
497
def as_result (
468
498
* exceptions : Type [TBE ],
469
499
) -> Callable [[Callable [P , R ]], Callable [P , Result [R , TBE ]]]:
0 commit comments