mm-std 0.4.9__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
 
@@ -70,6 +71,35 @@ class Result[T]:
70
71
  # Return the success value if present
71
72
  return cast(T, self.value)
72
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
+
73
103
  def unwrap_or(self, default: T) -> T:
74
104
  """
75
105
  Returns the success value if available, otherwise returns the given default.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mm-std
3
- Version: 0.4.9
3
+ Version: 0.4.10
4
4
  Requires-Python: >=3.12
5
5
  Requires-Dist: aiohttp-socks~=0.10.1
6
6
  Requires-Dist: aiohttp~=3.11.16
@@ -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=wzRdm8hzcvhKBD95NgQS7MklRMQQjVpgbsDAcolCMpg,9655
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.9.dist-info/METADATA,sha256=W70MWq3UfeFan-QnCTqG_KArTapiIlNKqQN1bXZrozo,446
32
- mm_std-0.4.9.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
33
- mm_std-0.4.9.dist-info/RECORD,,
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,,