Crates.io | partial-result |
lib.rs | partial-result |
version | 0.1.0 |
source | src |
created_at | 2024-01-18 19:22:39.931479 |
updated_at | 2024-01-18 19:22:39.931479 |
description | A library for results that return success for non-critical errors. |
homepage | |
repository | https://gitlab.com/manenko/partial-result |
max_upload_size | |
id | 1104480 |
size | 9,432 |
partial-result
The partial-result
crate is a Rust library that provides a type representing a partial success, i.e., a result that can contain a failure. This is useful when you need to return a result and a failure, where the failure represents a non-fatal error.
Add this to your Cargo.toml
:
[dependencies]
partial-result = "0.1.0"
Then run cargo build
to download and compile the crate.
Here's a basic example of how to use the PartialResult
type:
use partial_result::{
PartialResult,
PartialResultExt,
PartialSuccess,
};
#[derive(Debug)]
enum CriticalError {
WeAreDoomed(String),
EverythingIsLost(String),
}
#[derive(Debug)]
enum NonCriticalError {
SomethingWentWrong(String),
SomethingElseWentWrong(String),
}
fn do_something() -> PartialResult<u32, NonCriticalError, CriticalError> {
let value = 42;
let failure = NonCriticalError::SomethingWentWrong("Something went wrong".to_string());
PartialResult::partial_success(value, failure)
}
fn main() -> Result<(), CriticalError> {
let result = do_something()?;
println!("Result: {}", result.value);
result.failure.map(|e| println!("WARN: there was an issue during the computation: {:?}", e));
Ok(())
}
This project is licensed under the MIT License - see the LICENSE file for details.