use std::{fs::File, io::{BufReader, ErrorKind, Read}, path::Path}; use thiserror::Error; #[derive(Debug, Error)] pub enum ReadError { #[error("file not found")] FileNotFound, #[error("invalid file format")] InvalidFormat, #[error("unhandled read error")] Unhandled(ErrorKind), } pub fn read_file(path: &Path) -> Result, ReadError> { let file = File::open(path).map_err(|err| { match err.kind() { ErrorKind::NotFound => ReadError::FileNotFound, _ => ReadError::Unhandled( err.kind()), } })?; read_fh(&file) } pub fn read_fh(file: &File) -> Result, ReadError> { let mut content = Vec::new(); let mut reader = BufReader::new(file); reader.read_to_end(&mut content).map_err(|err| ReadError::Unhandled(err.kind()))?; Ok(content) }