|
1 | 1 | # ResultContainer: Rust Result Enums in Python |
2 | 2 |
|
3 | 3 | <p align="left"> |
4 | | - <img src="https://github.com/ScottBoyce-Python/ResultContainer/actions/workflows/python-pytest.yml/badge.svg" alt="Build Status" height="20"> |
| 4 | + <img src="https://github.com/ScottBoyce-Python/ResultContainer/actions/workflows/ResultContainer-pytest.yml/badge.svg" alt="Build Status" height="20"> |
5 | 5 | </p> |
6 | 6 |
|
7 | 7 |
|
| 8 | + |
8 | 9 | The `ResultContainer` module simplifies complex error handling into clean, readable, and maintainable code structures. Error handling in Python can often become unwieldy, with deeply nested `try/except` blocks and scattered error management. The `ResultContainer` is used for situations when errors are expected and are easily handled. Inspired by [Rust’s Result<Ok, Err>](https://doc.rust-lang.org/std/result/enum.Result.html) enum, `ResultContainer` introduces a clean and Pythonic way to encapsulate success (`Ok`) and failure (`Err`) outcomes. |
9 | 10 |
|
10 | 11 | The `ResultContainer.Result` enum wraps a value in an `Ok` variant, until there is an exception or error raised, and then it is converted to the `Err` variant. The `Err` variant wraps a `ResultContainer.ResultErr` object that contains the error messages and traceback information. The `Result` object includes similar methods to the Rust Result Enum for inquiry about the state, mapping functions, and passing attributes/methods to the containing `value`. |
@@ -88,6 +89,7 @@ There are methods built into `Result` to check if an error has been raised, or t |
88 | 89 |
|
89 | 90 | ```python |
90 | 91 | # Only the first argument is required for all constructors |
| 92 | +from ResultContainer import Result, Ok, Err |
91 | 93 |
|
92 | 94 | # Main object signature: |
93 | 95 | res = Result(value, success, error_msg, error_code, error_code_group, add_traceback, deepcopy) # Construct either Ok or Er |
@@ -184,6 +186,7 @@ z = x + y # z = Ok([1, 2, 3, 4, 5, 6, 7]) |
184 | 186 | ### Wrapping Objects |
185 | 187 |
|
186 | 188 | ```python |
| 189 | +from ResultContainer import Result, Ok, Err |
187 | 190 | from datetime import datetime, timedelta |
188 | 191 |
|
189 | 192 | # Wrap a datetime.datetime object |
@@ -259,6 +262,7 @@ y = x.raises() # Raises the following exception: |
259 | 262 | ### Passing Functions and Chaining Operations |
260 | 263 |
|
261 | 264 | ```python |
| 265 | +from ResultContainer import Result, Ok, Err |
262 | 266 | from math import sqrt |
263 | 267 | # to use an external function, like sqrt |
264 | 268 | # It must be passed to either apply or map or extracted with expect. |
|
0 commit comments