use std::borrow::Cow;
use super::*;
pub trait OptionExt<T> {
fn ok_or_user_err<S: Into<Cow<'static, str>>>(self, msg: S, advice: &'static [&'static str]) -> Result<T, Error>;
fn ok_or_system_err<S: Into<Cow<'static, str>>>(self, msg: S, advice: &'static [&'static str]) -> Result<T, Error>;
}
impl <T> OptionExt<T> for Option<T> {
fn ok_or_user_err<S: Into<Cow<'static, str>>>(self, msg: S, advice: &'static [&'static str]) -> Result<T, Error> {
match self {
Some(value) => Ok(value),
None => Err(user(msg.into(), advice)),
}
}
fn ok_or_system_err<S: Into<Cow<'static, str>>>(self, msg: S, advice: &'static [&'static str]) -> Result<T, Error> {
match self {
Some(value) => Ok(value),
None => Err(system(msg.into(), advice)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ok_or_user_err_some() {
let value = Some(42).ok_or_user_err("No value", &["Provide a value"]).unwrap();
assert_eq!(value, 42);
}
#[test]
fn test_ok_or_user_err_none() {
let err = None::<i32>.ok_or_user_err("No value", &["Provide a value"]).unwrap_err();
assert!(err.is(Kind::User));
assert_eq!(err.message(), "No value");
}
#[test]
fn test_ok_or_system_err_some() {
let value = Some(42).ok_or_system_err("No value", &["Check system"]).unwrap();
assert_eq!(value, 42);
}
#[test]
fn test_ok_or_system_err_none() {
let err = None::<i32>.ok_or_system_err("No value", &["Check system"]).unwrap_err();
assert!(err.is(Kind::System));
assert_eq!(err.message(), "No value");
}
}