2025-02-22 23:39:32 +00:00
|
|
|
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<Vec<u8>, 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<Vec<u8>, 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)
|
|
|
|
}
|
2025-02-23 00:11:29 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum WriteError {
|
|
|
|
#[error("unhandled write error")]
|
|
|
|
Unhandled(ErrorKind),
|
|
|
|
}
|