Skip to content

Commit

Permalink
Fix panic of 'pastel distinct N' for N < 2, closes #69
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Aug 25, 2019
1 parent 556ed98 commit 704f335
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/cli/commands/distinct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ impl GenericCommand for DistinctCommand {
.parse::<usize>()
.map_err(|_| PastelError::CouldNotParseNumber(count.into()))?;

if count < 2 {
return Err(PastelError::DistinctColorCountMustBeLargerThanOne);
}

let distance_metric = match matches.value_of("metric").expect("required argument") {
"CIE76" => DistanceMetric::CIE76,
"CIEDE2000" => DistanceMetric::CIEDE2000,
Expand Down
4 changes: 4 additions & 0 deletions src/cli/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub enum PastelError {
CouldNotParseNumber(String),
StdoutClosed,
GradientNumberMustBeLargerThanOne,
DistinctColorCountMustBeLargerThanOne,
ColorPickerExecutionError(String),
NoColorPickerFound,
IoError(std::io::Error),
Expand All @@ -34,6 +35,9 @@ impl PastelError {
PastelError::GradientNumberMustBeLargerThanOne => {
"The number of colors must be larger one".into()
}
PastelError::DistinctColorCountMustBeLargerThanOne => {
"The number of colors must be larger one".into()
}
PastelError::ColorPickerExecutionError(name) => {
format!("Error while running color picker '{}", name)
}
Expand Down

0 comments on commit 704f335

Please sign in to comment.