improve error handling
This commit is contained in:
parent
f34c06758f
commit
ea962dc8cf
14
src/lib.rs
14
src/lib.rs
|
@ -7,7 +7,9 @@ pub enum ReadError {
|
||||||
|
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
Read(ReadError),
|
Read(ReadError),
|
||||||
BadParam(String),
|
BadPopSize,
|
||||||
|
BadMut,
|
||||||
|
BadCrossRatio,
|
||||||
BadMove(Pos),
|
BadMove(Pos),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +73,9 @@ impl std::fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Error::Read(e) => write!(f, "{e}"),
|
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})"),
|
Error::BadMove(pos) => write!(f, "invalid move ({pos})"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -511,15 +515,15 @@ pub fn run(config: Config, rle: Option<String>) -> Result<(), Error> {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
if config.pop_size == 0 {
|
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 {
|
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 {
|
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 {
|
let map = if let Some(rle) = rle {
|
||||||
|
|
Loading…
Reference in New Issue