improve error handling

This commit is contained in:
mos 2024-10-10 14:31:07 +02:00
parent f34c06758f
commit ea962dc8cf
1 changed files with 9 additions and 5 deletions

View File

@ -7,7 +7,9 @@ pub enum ReadError {
pub enum Error {
Read(ReadError),
BadParam(String),
BadPopSize,
BadMut,
BadCrossRatio,
BadMove(Pos),
}
@ -71,7 +73,9 @@ impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Read(e) => write!(f, "{e}"),
Error::BadParam(s) => write!(f, "{s}"),
Error::BadPopSize => write!(f, "population size cannot be 0"),
Error::BadMut => write!(f, "mutation length too large"),
Error::BadCrossRatio => write!(f, "crossover ratio cannot be 0"),
Error::BadMove(pos) => write!(f, "invalid move ({pos})"),
}
}
@ -511,15 +515,15 @@ pub fn run(config: Config, rle: Option<String>) -> Result<(), Error> {
.unwrap();
if config.pop_size == 0 {
return Err(Error::BadParam("population size cannot be 0".into()));
return Err(Error::BadPopSize);
}
if config.mut_max > config.dna_size {
return Err(Error::BadParam("mutation length too large".into()));
return Err(Error::BadMut);
}
if config.cross_ratio == 0 {
return Err(Error::BadParam("crossover ratio cannot be 0".into()));
return Err(Error::BadCrossRatio);
}
let map = if let Some(rle) = rle {