pub trait ErrorWrap<T, E>where
E: Send + Sync + 'static,{
fn wrap<C, F>(self, f: F) -> Result<T, Error>
where
C: Display + Send + Sync + 'static,
F: FnOnce() -> C;
fn wrap_help<C, F>(self, f: F, help: &'static str) -> Result<T, Error>
where
C: Display + Send + Sync + 'static,
F: FnOnce() -> C;
fn wrap_help_owned<C, F>(self, f: F, help: String) -> Result<T, Error>
where
C: Display + Send + Sync + 'static,
F: FnOnce() -> C;
}
Expand description
Provides wrap
, wrap_help
and wrap_help_owned
methods for Result
.
This trait will be sealed and should not be implemented for types outside of
narrate
.
Example
use narrate::{ErrorWrap, Result};
use std::fs;
use std::path::PathBuf;
pub struct ImportantThing {
path: PathBuf,
}
impl ImportantThing {
pub fn detach(&mut self) -> Result<()> {...}
}
pub fn do_it(mut it: ImportantThing) -> Result<Vec<u8>> {
it.detach().wrap(|| "Failed to detach the important thing")?;
let path = &it.path;
let content = fs::read(path)
.wrap(|| format!("Failed to read instrs from {}", path.display()))?;
Ok(content)
}
Required Methods
Wrap an error value with additional context that is evaluated lazily only once an error does occur.
Lazily evaluated error wrapper, with an additional static help message