1 stable release
1.0.1 | Aug 31, 2024 |
---|---|
1.0.0 |
|
#841 in Rust patterns
39 downloads per month
4KB
string_error_wrap
Crate to simplify making error types wrapping String
Useful when a library uses String
errors but your app
uses something like anyhow
.
To use the crate add the crate to the project using cargo
(either manually in Cargo.toml
or using cargo add string_error_wrap
)
Then you can define a new String
-wrapping error type like this
(replacing Name with what you want the name of the type to be):
string_error_wrap::wrapper_gen!(Name);
This type implements Error
, Debug
, Clone
and From<String>
.
It is intended to be used using Result::map_err(YourError::from)
,
which will convert any errors from a String to this type, but may of course
also be used in other ways.
lib.rs
:
string_error_wrap
Crate for easily creating error types wrapping String.
This can be extremely useful for dealing with libraries using String as an error type in some functions when you use something like anyhow.
Usage
// generates error type
string_error_wrap::wrapper!(SomeError);
fn main() -> Result<(), SomeError> {
// other_function returns a Result<T, String>
// but that can be converted into Result<T, SomeError>
// allowing the use of ?
other_function().map_err(SomeError::from)?;
}