use std::path::{Path, PathBuf};
pub struct Loaded {
pub source: PathBuf,
pub read: en16931_formats::Read,
}
pub fn load(path: &Path) -> Result<Loaded, String> {
let bytes = bytes(path)?;
let read = en16931_formats::read(&bytes).map_err(|e| format!("{}: {e}", path.display()))?;
Ok(Loaded {
source: path.to_owned(),
read,
})
}
pub fn bytes(path: &Path) -> Result<Vec<u8>, String> {
if path == Path::new("-") {
use std::io::Read as _;
let mut buf = Vec::new();
std::io::stdin()
.read_to_end(&mut buf)
.map_err(|e| format!("-: {e}"))?;
return Ok(buf);
}
std::fs::read(path).map_err(|e| format!("{}: {e}", path.display()))
}