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