Err message for sort-by invalid types now shows the mismatched types (#2366)

* make sort-by fail gracefully if mismatched types are compared

* Added a test to check if sorted-by with invalid types exists gracefully

* Linter changes

* removed redundant pattern matching

* Changed the error message

* Added a comma after every argument

* Changed the test to accomodate the new err messages

* Err message for sort-by invalid types now shows the mismatched types

* Lints problems

* Changed unwrap to expect
This commit is contained in:
Luccas Mateus
2020-08-18 02:37:45 -03:00
committed by GitHub
parent 57101d5022
commit 1d5518a214
3 changed files with 24 additions and 4 deletions

View File

@ -135,13 +135,20 @@ pub fn sort(
} => {
let should_sort_case_insensitively = insensitive && vec.iter().all(|x| x.is_string());
if !vec
if let Some(values) = vec
.windows(2)
.all(|elem| coerce_compare(&elem[0], &elem[1]).is_ok())
.map(|elem| coerce_compare(&elem[0], &elem[1]))
.find(|elem| elem.is_err())
{
let (type_1, type_2) = values
.err()
.expect("An error ocourred in the checking of types");
return Err(ShellError::labeled_error(
"Not all values can be compared",
"not all values compare",
format!(
"Unable to sort values, as \"{}\" cannot compare against \"{}\"",
type_1, type_2
),
tag,
));
}