mm-std 0.4.8__py3-none-any.whl → 0.4.10__py3-none-any.whl
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.
mm_std/result.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
from __future__ import annotations
|
2
2
|
|
3
|
+
import sys
|
3
4
|
from collections.abc import Awaitable, Callable
|
4
5
|
from typing import Any, Protocol, TypeGuard, TypeVar, cast
|
5
6
|
|
@@ -44,15 +45,61 @@ class Result[T]:
|
|
44
45
|
"""
|
45
46
|
return self.exception is not None
|
46
47
|
|
47
|
-
def unwrap(self) -> T:
|
48
|
+
def unwrap(self, message_prefix: str | None = None, include_error: bool = True) -> T:
|
48
49
|
"""
|
49
|
-
Returns the success value.
|
50
|
-
|
50
|
+
Returns the success value if the Result is Ok, otherwise raises a RuntimeError.
|
51
|
+
|
52
|
+
Args:
|
53
|
+
message_prefix: Optional custom prefix for the error message if the Result is an error.
|
54
|
+
If not provided, a default message will be used.
|
55
|
+
include_error: If True, appends the internal error message from the Result to the final exception message.
|
56
|
+
|
57
|
+
Raises:
|
58
|
+
RuntimeError: If the Result is an error.
|
59
|
+
|
60
|
+
Returns:
|
61
|
+
The success value of type T.
|
51
62
|
"""
|
52
63
|
if not self.is_ok():
|
53
|
-
|
64
|
+
# Use the provided message or a default fallback
|
65
|
+
error_message = message_prefix or "Called unwrap() on a failure value"
|
66
|
+
# Optionally append the error detail
|
67
|
+
if include_error:
|
68
|
+
error_message = f"{error_message}: {self.error}"
|
69
|
+
# Raise with the final constructed message
|
70
|
+
raise RuntimeError(error_message)
|
71
|
+
# Return the success value if present
|
54
72
|
return cast(T, self.value)
|
55
73
|
|
74
|
+
def unwrap_or_exit(
|
75
|
+
self,
|
76
|
+
message_prefix: str | None = None,
|
77
|
+
include_error: bool = True,
|
78
|
+
exit_code: int = 1,
|
79
|
+
) -> T:
|
80
|
+
"""
|
81
|
+
Returns the success value if the Result is Ok, otherwise prints an error to stderr and exits.
|
82
|
+
|
83
|
+
Args:
|
84
|
+
message_prefix: Optional custom prefix for the error message.
|
85
|
+
include_error: If True, includes the internal error message in the printed message.
|
86
|
+
exit_code: The exit code to use when terminating the program on error.
|
87
|
+
|
88
|
+
Returns:
|
89
|
+
The success value of type T.
|
90
|
+
|
91
|
+
Exits:
|
92
|
+
Exits the program with the specified exit code if the result is an error.
|
93
|
+
"""
|
94
|
+
if self.is_ok():
|
95
|
+
return cast(T, self.value)
|
96
|
+
|
97
|
+
error_message = message_prefix or "Called unwrap_or_exit() on a failure value"
|
98
|
+
if include_error:
|
99
|
+
error_message = f"{error_message}: {self.error}"
|
100
|
+
print(error_message, file=sys.stderr) # noqa: T201
|
101
|
+
sys.exit(exit_code)
|
102
|
+
|
56
103
|
def unwrap_or(self, default: T) -> T:
|
57
104
|
"""
|
58
105
|
Returns the success value if available, otherwise returns the given default.
|
@@ -12,7 +12,7 @@ mm_std/net.py,sha256=qdRCBIDneip6FaPNe5mx31UtYVmzqam_AoUF7ydEyjA,590
|
|
12
12
|
mm_std/print_.py,sha256=zB7sVbSSF8RffMxvnOdbKCXjCKtKzKV3R68pBri4NkQ,1638
|
13
13
|
mm_std/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
14
14
|
mm_std/random_.py,sha256=OuUX4VJeSd13NZBya4qrGpR2TfN7_87tfebOY6DBUnI,1113
|
15
|
-
mm_std/result.py,sha256=
|
15
|
+
mm_std/result.py,sha256=ltq9lJ-mZYRWuwGfhVtsfb4CX9Gw-tOUKyj5cEoyHiE,10706
|
16
16
|
mm_std/str.py,sha256=BEjJ1p5O4-uSYK0h-enasSSDdwzkBbiwdQ4_dsrlEE8,3257
|
17
17
|
mm_std/toml.py,sha256=CNznWKR0bpOxS6e3VB5LGS-Oa9lW-wterkcPUFtPcls,610
|
18
18
|
mm_std/types_.py,sha256=9FGd2q47a8M9QQgsWJR1Kq34jLxBAkYSoJuwih4PPqg,257
|
@@ -28,6 +28,6 @@ mm_std/http/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
28
28
|
mm_std/http/http_request.py,sha256=VnjZKrfaXQfMxrHRUqQ-Sxtr5Qf9FXBiJ-mmJTCzNkY,3709
|
29
29
|
mm_std/http/http_request_sync.py,sha256=bqCBilbe4ZJ9vkhuBQeU5UMTJh6BtvtUwjieEodu6rw,1542
|
30
30
|
mm_std/http/http_response.py,sha256=vrRv7Rjr5_kTUGllfW5oJ8ZqkcfiXc9T8PlBGAr-TBM,3884
|
31
|
-
mm_std-0.4.
|
32
|
-
mm_std-0.4.
|
33
|
-
mm_std-0.4.
|
31
|
+
mm_std-0.4.10.dist-info/METADATA,sha256=yFl2gf7vpu82NE6dY7uZdRmM0msSPOEOpK6SPCmF9CQ,447
|
32
|
+
mm_std-0.4.10.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
33
|
+
mm_std-0.4.10.dist-info/RECORD,,
|
File without changes
|