use {
problemo::{common::*, *},
std::path::*,
};
fn read_file(path: &str) -> Result<String, Problem> {
let path = PathBuf::from(path);
std::fs::read_to_string(&path).into_file_problem("read_to_string", path)
}
fn main() {
if let Err(problem) = read_file("non-existing.txt") {
println!("{}", problem);
if let Some(path) = problem.attachment_of_type::<PathBuf>() {
println!(" path: {}", path.display());
}
}
}
pub trait FileResult<OkT> {
fn into_file_problem(self, message: &'static str, path: PathBuf) -> Result<OkT, Problem>;
}
impl<ResultT, OkT> FileResult<OkT> for ResultT
where
ResultT: IntoProblemResult<OkT>,
{
#[track_caller]
fn into_file_problem(self, message: &'static str, path: PathBuf) -> Result<OkT, Problem> {
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.via(IoError::new(message)).with(path)),
}
}
}