mm-std 0.3.27__py3-none-any.whl → 0.3.28__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/data_result.py CHANGED
@@ -87,35 +87,85 @@ class DataResult(Generic[T]):
87
87
  """
88
88
  Transforms the success value using the provided function if this is a success result.
89
89
  If this is an error result, returns a new error result with the same error message.
90
+ If the function raises an exception, returns a new error result with the exception message.
90
91
 
91
92
  Args:
92
93
  fn: A function that transforms the success value from type T to type U.
93
94
 
94
95
  Returns:
95
- A new DataResult with the transformed success value or the original error.
96
+ A new DataResult with the transformed success value or an error.
96
97
  """
97
98
  if self.is_err():
98
99
  return DataResult[U](err=self.err, data=self.data)
99
100
 
100
- mapped_ok = fn(self.unwrap())
101
- return DataResult[U](ok=mapped_ok, data=self.data)
101
+ try:
102
+ mapped_ok = fn(self.unwrap())
103
+ return DataResult[U](ok=mapped_ok, data=self.data)
104
+ except Exception as e:
105
+ return DataResult[U](err=f"Error in map: {e!s}", data={"original_data": self.data, "original_ok": self.ok})
102
106
 
103
107
  async def map_async(self, fn: Callable[[T], Awaitable[U]]) -> DataResult[U]:
104
108
  """
105
109
  Asynchronously transforms the success value using the provided async function if this is a success result.
106
110
  If this is an error result, returns a new error result with the same error message.
111
+ If the function raises an exception, returns a new error result with the exception message.
107
112
 
108
113
  Args:
109
114
  fn: An async function that transforms the success value from type T to type U.
110
115
 
111
116
  Returns:
112
- A new DataResult with the transformed success value or the original error.
117
+ A new DataResult with the transformed success value or an error.
113
118
  """
114
119
  if self.is_err():
115
120
  return DataResult[U](err=self.err, data=self.data)
116
121
 
117
- mapped_ok = await fn(self.unwrap())
118
- return DataResult[U](ok=mapped_ok, data=self.data)
122
+ try:
123
+ mapped_ok = await fn(self.unwrap())
124
+ return DataResult[U](ok=mapped_ok, data=self.data)
125
+ except Exception as e:
126
+ return DataResult[U](err=f"Error in map_async: {e!s}", data={"original_data": self.data, "original_ok": self.ok})
127
+
128
+ def and_then(self, fn: Callable[[T], DataResult[U]]) -> DataResult[U]:
129
+ """
130
+ Applies the function to the success value if this is a success result.
131
+ If this is an error result, returns a new error result with the same error message.
132
+
133
+ Unlike map, the function must return a DataResult.
134
+
135
+ Args:
136
+ fn: A function that takes the success value and returns a new DataResult.
137
+
138
+ Returns:
139
+ The result of the function application or the original error.
140
+ """
141
+ if self.is_err():
142
+ return DataResult[U](err=self.err, data=self.data)
143
+
144
+ try:
145
+ return fn(self.unwrap())
146
+ except Exception as e:
147
+ return DataResult[U](err=f"Error in and_then: {e!s}", data={"original_data": self.data, "original_ok": self.ok})
148
+
149
+ async def and_then_async(self, fn: Callable[[T], Awaitable[DataResult[U]]]) -> DataResult[U]:
150
+ """
151
+ Asynchronously applies the function to the success value if this is a success result.
152
+ If this is an error result, returns a new error result with the same error message.
153
+
154
+ Unlike map_async, the function must return a DataResult.
155
+
156
+ Args:
157
+ fn: An async function that takes the success value and returns a new DataResult.
158
+
159
+ Returns:
160
+ The result of the function application or the original error.
161
+ """
162
+ if self.is_err():
163
+ return DataResult[U](err=self.err, data=self.data)
164
+
165
+ try:
166
+ return await fn(self.unwrap())
167
+ except Exception as e:
168
+ return DataResult[U](err=f"Error in and_then_async: {e!s}", data={"original_data": self.data, "original_ok": self.ok})
119
169
 
120
170
  def __repr__(self) -> str:
121
171
  """
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mm-std
3
- Version: 0.3.27
3
+ Version: 0.3.28
4
4
  Requires-Python: >=3.12
5
5
  Requires-Dist: aiohttp-socks~=0.10.1
6
6
  Requires-Dist: aiohttp~=3.11.16
@@ -2,7 +2,7 @@ mm_std/__init__.py,sha256=ePkjykjImp2Wu10lFzK3Fl1HpGqqXFb_HZK_BSiU3MM,3316
2
2
  mm_std/command.py,sha256=ze286wjUjg0QSTgIu-2WZks53_Vclg69UaYYgPpQvCU,1283
3
3
  mm_std/config.py,sha256=4ox4D2CgGR76bvZ2n2vGQOYUDagFnlKEDb87to5zpxE,1871
4
4
  mm_std/crypto.py,sha256=jdk0_TCmeU0pPXMyz9xH6kQHSjjZ9GcGClBwQps5vBo,340
5
- mm_std/data_result.py,sha256=--rg0YPaJkszCmbt9TO-BthdGdYrCRARA8iII9LVocE,5871
5
+ mm_std/data_result.py,sha256=AMG6ttFTa9wxOS2U3kgPI2Z1shntdhF-7KxvfYYILoo,8104
6
6
  mm_std/date.py,sha256=976eEkSONuNqHQBgSRu8hrtH23tJqztbmHFHLdbP2TY,1879
7
7
  mm_std/dict.py,sha256=6GkhJPXD0LiJDxPcYe6jPdEDw-MN7P7mKu6U5XxwYDk,675
8
8
  mm_std/env.py,sha256=5zaR9VeIfObN-4yfgxoFeU5IM1GDeZZj9SuYf7t9sOA,125
@@ -29,6 +29,6 @@ mm_std/concurrency/sync_task_runner.py,sha256=s5JPlLYLGQGHIxy4oDS-PN7O9gcy-yPZFo
29
29
  mm_std/http/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
30
30
  mm_std/http/http_request.py,sha256=mJak332gv9NvY0JLO3hNrBxroQGS1HlNBzd1yq4Dy24,3694
31
31
  mm_std/http/response.py,sha256=vvv5COTjJula9t33mFyrruhrFC4dr_Uy0jDKj6t1JxM,2923
32
- mm_std-0.3.27.dist-info/METADATA,sha256=H30ViZKdZolKRVDR9PxBk7OoQZWofyy-EqjrUpT2NkQ,415
33
- mm_std-0.3.27.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
34
- mm_std-0.3.27.dist-info/RECORD,,
32
+ mm_std-0.3.28.dist-info/METADATA,sha256=NbbHHlhKM4WMRWCSpEvDdQhfDiH9L6-TUQQBP6Px4Co,415
33
+ mm_std-0.3.28.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
34
+ mm_std-0.3.28.dist-info/RECORD,,