Skip to content

Commit 217475c

Browse files
committed
feat: add inspect, inspect_apply, inspect_err methods
1 parent 379159f commit 217475c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

ResultContainer/__init__.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,21 @@ class Result:
726726
otherwise False.
727727
- If function call fails, then raises exception.
728728
729+
inspect(ok_func, *args, **kwargs):
730+
If `Ok(value)`, then evaluates `ok_func(value)`.
731+
Returns original Result instance.
732+
- If ok_func fails, raises an exception.
733+
734+
inspect_apply(ok_func, *args, **kwargs):
735+
If `Ok(value)`, then evaluates `ok_func(value)`.
736+
Returns original Result instance.
737+
- If ok_func fails, no exception is raised .
738+
739+
inspect_err(err_func, *args, **kwargs):
740+
If `Err(e)`, then evaluates `err_func(e)`.
741+
Returns original Result instance.
742+
- If err_func fails, raises an exception.
743+
729744
apply(ok_func, *args, **kwargs):
730745
Maps a function to the Result to return a new Result.
731746
For the Ok(value) variant, returns `Ok(ok_func(value, *args, **kwargs))`.
@@ -1055,6 +1070,24 @@ def is_Ok_and(self, bool_ok_func, *args, **kwargs) -> bool:
10551070
def is_Err_and(self, bool_err_func, *args, **kwargs) -> bool:
10561071
return not self._success and bool_err_func(self._val, *args, **kwargs)
10571072

1073+
def inspect(self, ok_func, *args, **kwargs):
1074+
if self._success:
1075+
ok_func(self._val, *args, **kwargs)
1076+
return self
1077+
1078+
def inspect_apply(self, ok_func, *args, **kwargs):
1079+
if self._success:
1080+
try:
1081+
ok_func(self._val, *args, **kwargs)
1082+
except Exception:
1083+
pass
1084+
return self
1085+
1086+
def inspect_err(self, err_func, *args, **kwargs):
1087+
if not self._success:
1088+
err_func(self._val, *args, **kwargs)
1089+
return self
1090+
10581091
def apply(self, ok_func, *args, **kwargs):
10591092
if self._success:
10601093
try:

0 commit comments

Comments
 (0)