Skip to content

Commit

Permalink
Add reflexive patch index range parsing
Browse files Browse the repository at this point in the history
Add reflexive patch index range parsing so that if someone defines a
range of 1-1, or 3-3, etc. It will be parsted into 1, None, or 3, None
respectively.

[changelog]
added: reflexive patch index range parsing

<!-- ps-id: 187cc887-1423-4822-b50f-701194324acd -->
  • Loading branch information
drewdeponte committed Oct 19, 2023
1 parent 294f036 commit 555a2b9
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/commands/patch_index_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@ impl std::str::FromStr for PatchIndexRange {
let patch_end_index = patch_end_index_str.parse::<usize>().map_err(|e| {
ParsePatchIndexOrRangeError::UnparsableIndex(patch_end_index_str.to_string(), e)
})?;
if patch_end_index > patch_start_index {
Ok(PatchIndexRange {
match patch_end_index.cmp(&patch_start_index) {
std::cmp::Ordering::Greater => Ok(PatchIndexRange {
start_index: patch_start_index,
end_index: Some(patch_end_index),
})
} else {
Err(ParsePatchIndexOrRangeError::StartPatchIndexLargerThanEnd(
s.to_string(),
))
}),
std::cmp::Ordering::Equal => Ok(PatchIndexRange {
start_index: patch_start_index,
end_index: None,
}),
std::cmp::Ordering::Less => Err(
ParsePatchIndexOrRangeError::StartPatchIndexLargerThanEnd(s.to_string()),
),
}
} else {
Err(ParsePatchIndexOrRangeError::InvalidIndexRange(
Expand Down Expand Up @@ -164,12 +167,16 @@ fn range2() {
);
}

// CR-someday alizter: We don't accept reflexive ranges, maybe we should?
// It seems simple enought to parse this as a single patch index.
#[test]
fn range3() {
let patch_index_or_range = "2-2".parse::<PatchIndexRange>();
assert!(patch_index_or_range.is_err());
let patch_index_or_range = "2-2".parse::<PatchIndexRange>().unwrap();
assert_eq!(
patch_index_or_range,
PatchIndexRange {
start_index: 2,
end_index: None,
},
);
}

#[test]
Expand Down

0 comments on commit 555a2b9

Please sign in to comment.