use std::borrow::Cow;
use super::*;
pub trait ResultExt<T> {
fn or_user_err(self, advice: &'static [&'static str]) -> Result<T, Error>;
fn wrap_user_err<S: Into<Cow<'static, str>> + 'static>(
self,
message: S,
advice: &'static [&'static str],
) -> Result<T, Error>;
fn or_system_err(self, advice: &'static [&'static str]) -> Result<T, Error>;
fn wrap_system_err<S: Into<Cow<'static, str>> + 'static>(
self,
message: S,
advice: &'static [&'static str],
) -> Result<T, Error>;
#[deprecated(
since = "0.2.3",
note = "We are updating the interface to match the Rust stdlib style. Please use `or_user_err` method instead."
)]
fn map_err_as_user(self, advice: &'static [&'static str]) -> Result<T, Error>;
#[deprecated(
since = "0.2.3",
note = "We are updating the interface to match the Rust stdlib style. Please use `wrap_user_err` method instead."
)]
fn wrap_err_as_user<S: Into<Cow<'static, str>> + 'static>(
self,
message: S,
advice: &'static [&'static str],
) -> Result<T, Error>;
#[deprecated(
since = "0.2.3",
note = "We are updating the interface to match the Rust stdlib style. Please use `or_system_err` method instead."
)]
fn map_err_as_system(self, advice: &'static [&'static str]) -> Result<T, Error>;
#[deprecated(
since = "0.2.3",
note = "We are updating the interface to match the Rust stdlib style. Please use `wrap_system_err` method instead."
)]
fn wrap_err_as_system<S: Into<Cow<'static, str>> + 'static>(
self,
message: S,
advice: &'static [&'static str],
) -> Result<T, Error>;
}
impl<T, E> ResultExt<T> for Result<T, E>
where
E: Into<Box<dyn std::error::Error + Send + Sync>> + 'static,
{
fn or_user_err(self, advice: &'static [&'static str]) -> Result<T, Error> {
self.map_err(|e| user(e, advice))
}
fn wrap_user_err<S: Into<Cow<'static, str>> + 'static>(
self,
message: S,
advice: &'static [&'static str],
) -> Result<T, Error> {
self.map_err(|e| wrap_user(e, message, advice))
}
fn or_system_err(self, advice: &'static [&'static str]) -> Result<T, Error> {
self.map_err(|e| system(e, advice))
}
fn wrap_system_err<S: Into<Cow<'static, str>> + 'static>(
self,
message: S,
advice: &'static [&'static str],
) -> Result<T, Error> {
self.map_err(|e| wrap_system(e, message, advice))
}
fn map_err_as_user(self, advice: &'static [&'static str]) -> Result<T, Error> {
self.map_err(|e| user(e, advice))
}
fn wrap_err_as_user<S: Into<Cow<'static, str>> + 'static>(
self,
message: S,
advice: &'static [&'static str],
) -> Result<T, Error> {
self.map_err(|e| user(wrap_user(e, message, advice), advice))
}
fn map_err_as_system(self, advice: &'static [&'static str]) -> Result<T, Error> {
self.map_err(|e| system(e, advice))
}
fn wrap_err_as_system<S: Into<Cow<'static, str>> + 'static>(
self,
message: S,
advice: &'static [&'static str],
) -> Result<T, Error> {
self.map_err(|e| system(wrap_system(e, message, advice), advice))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_or_user_error() {
let result: Result<i32, std::io::Error> = Err(std::io::Error::other("underlying error"));
let user_error = result
.or_user_err(&["Please check your input and try again."])
.err()
.unwrap();
assert!(user_error.is(Kind::User));
}
#[test]
fn test_wrap_user_error() {
let result: Result<i32, std::io::Error> = Err(std::io::Error::other("underlying error"));
let user_error = result
.wrap_user_err(
"Failed to process the input.",
&["Please check your input and try again."],
)
.err()
.unwrap();
assert!(user_error.is(Kind::User));
assert_eq!(user_error.message(), "Failed to process the input.");
}
#[test]
fn test_or_system_error() {
let result: Result<i32, std::io::Error> = Err(std::io::Error::other("underlying error"));
let system_error = result
.or_system_err(&["Please check your input and try again."])
.err()
.unwrap();
assert!(system_error.is(Kind::System));
}
#[test]
fn test_wrap_system_error() {
let result: Result<i32, std::io::Error> = Err(std::io::Error::other("underlying error"));
let system_error = result
.wrap_system_err(
"Failed to process the input.",
&["Please check your input and try again."],
)
.err()
.unwrap();
assert!(system_error.is(Kind::System));
assert_eq!(system_error.message(), "Failed to process the input.");
}
}