use std::fs::File;
use std::io;
use std::path::{Path, PathBuf};
#[derive(std::fmt::Debug)]
pub struct IoErrorWithPath {
source: io::Error,
path: PathBuf,
}
impl std::error::Error for IoErrorWithPath {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.source)
}
}
impl std::fmt::Display for IoErrorWithPath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{}: {}", self.source, self.path.display())
}
}
pub fn open_file(path: &Path) -> Result<File, IoErrorWithPath> {
File::open(path).map_err(|e| IoErrorWithPath {
source: e,
path: path.to_path_buf(),
})
}
pub fn create_file(path: &Path) -> Result<File, IoErrorWithPath> {
File::create(path).map_err(|e| IoErrorWithPath {
source: e,
path: path.to_path_buf(),
})
}
pub fn read_to_string(path: &Path) -> Result<String, IoErrorWithPath> {
std::fs::read_to_string(path).map_err(|e| IoErrorWithPath {
source: e,
path: path.to_path_buf(),
})
}