improve path error handling

This commit is contained in:
mos 2024-07-28 08:57:44 +02:00
parent 3dfdf1af7b
commit 5028cf3eec
1 changed files with 4 additions and 12 deletions

View File

@ -7,11 +7,10 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
ReadError,
BadPath,
BadOutPath,
BadFile,
BadRead(String),
BadWrite(std::io::Error),
BadPath(std::io::Error),
BpError(bp::Error),
ParseError(bip::ParseError),
}
@ -20,11 +19,10 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ReadError => write!(f, "errors found"),
Self::BadPath => write!(f, "bad file path"),
Self::BadOutPath => write!(f, "bad output file path"),
Self::BadFile => write!(f, "invalid text"),
Self::BadRead(e) => write!(f, "{e}"),
Self::BadWrite(e) => write!(f, "{e}"),
Self::BadPath(e) => write!(f, "{e}"),
Self::BpError(e) => write!(f, "{e}"),
Self::ParseError(e) => write!(f, "{e}"),
}
@ -58,16 +56,10 @@ fn read_bip<R: BufRead>(buf: R) -> Result<Vec<bip::Node>> {
}
pub fn run(path: &str, outfile: &str) -> Result<()> {
let f = match std::fs::File::open(path) {
Ok(f) => f,
Err(_) => return Err(Error::BadPath),
};
let f = std::fs::File::open(path).map_err(Error::BadPath)?;
let node_tree = read_bip(BufReader::new(f))?;
let mut out = match std::fs::File::create(outfile) {
Ok(f) => f,
Err(_) => return Err(Error::BadOutPath),
};
let mut out = std::fs::File::create(outfile).map_err(Error::BadPath)?;
bp::Writer::new()
.parse_tree(&node_tree)