Skip to main content

rs_enums is a Python module inspired by Rust's powerful Option and Result enums.

Project description

rs_enums

Version Pylint Status Pytest Status Contributing Guidelines

rs_enums is a Python module inspired by Rust’s Option and Result enums. It provides functional programming constructs for handling optional values (Option) and results (Result) with success or error outcomes. This module enables safer and more expressive code by allowing you to work with values that may or may not exist, or operations that may succeed or fail.

Features include:

  • Option: Represents a value that may or may not be present (Some or None).
  • Result: Represents an operation that can either succeed (Ok) or fail (Err).
  • Common methods like is_some(), is_none(), is_ok(), and is_err() are included for easy handling of these types.

Installation

Install via pip:

pip install rs_enums

Result and Option Types

Option

The Option type represents an optional value that can either be present (Some) or absent (None). It provides methods to safely handle values that may not exist.

Features:

  • Some: Represents a value that is present.
  • is_some(): Checks if the value is present.
  • is_none(): Checks if the value is absent.
  • unwrap(): Returns the value if present; raises an error if absent.
  • expect(message): Returns the value if present; raises an error with a custom message if absent.

Result

The Result type represents a value that can either be successful (Ok) or erroneous (Err). It is useful for handling operations that may succeed or fail.

Features:

  • Ok: Represents a successful value.
  • Err: Represents an error value.
  • is_ok(): Checks if the result is successful.
  • is_err(): Checks if the result is erroneous.
  • unwrap(): Returns the successful value if present; raises an error if erroneous.
  • expect(message): Returns the successful value if present; raises an error with a custom message if erroneous.

Usage Example

from rs_enums.option import Option
from rs_enums.result import Result

# Example of Option
opt = Option(42)
if opt.is_some():
    print(opt.unwrap())  # Output: 42

# Example of Result
result = Result.new(value="Success")
if result.is_ok():
    print(result.unwrap())  # Output: Success

License

This project is licensed under the Apache-2.0 license.

Contributing

We welcome contributions! Please see our CONTRIBUTING.md for guidelines on how to contribute to this project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rs_enums-0.1.2b0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rs_enums-0.1.2b0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file rs_enums-0.1.2b0.tar.gz.

File metadata

  • Download URL: rs_enums-0.1.2b0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.2

File hashes

Hashes for rs_enums-0.1.2b0.tar.gz
Algorithm Hash digest
SHA256 7ccace3e1bb733170291eab95e3c08d3386bd13d6b6dd86b9c4712625e82659e
MD5 605e4737983bfd51d03853c3dfa52c73
BLAKE2b-256 4a8bbadf25017559a365eddc846605755af2175d8cb31a8ebf8d7c4691552a81

See more details on using hashes here.

File details

Details for the file rs_enums-0.1.2b0-py3-none-any.whl.

File metadata

  • Download URL: rs_enums-0.1.2b0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.2

File hashes

Hashes for rs_enums-0.1.2b0-py3-none-any.whl
Algorithm Hash digest
SHA256 cf7d36858e20a4a4ef588e1838063732fe7d8304b1e13384c40fa20bd7f451ab
MD5 dd6834bffe1011334800a81b5779f253
BLAKE2b-256 abf875185a5134a0c1253d3ca87df10f866d9b360abcfecb3f6bd8e134040332

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page