Skip to content

Commit bf05741

Browse files
refactor: simplify file_range by deduplicating match arms
Extract the common Bound/ContentRange construction out of the three satisfiable match arms. The match now only computes the start..=end range, and a single block after it builds the Content-Range header. Also simplifies the guards to plain arithmetic comparisons.
1 parent f0326ab commit bf05741

1 file changed

Lines changed: 14 additions & 42 deletions

File tree

src/lib.rs

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -47,56 +47,28 @@ pub fn file_range(
4747
let size = size.get();
4848

4949
let Some(http_range) = http_range else {
50-
let end = size - 1;
5150
return Ok(ContentRange {
5251
header: None,
53-
range: 0..=end,
52+
range: 0..=size - 1,
5453
});
5554
};
5655

57-
match http_range {
58-
HttpRange::StartingPoint(start) if size > start => {
59-
let end = size - 1;
60-
61-
let content_range =
62-
HttpContentRange::Bound(Bound::new(start..=end, Some(size)).unwrap());
63-
64-
Ok(ContentRange {
65-
header: Some(content_range),
66-
range: start..=end,
67-
})
56+
let range = match http_range {
57+
HttpRange::StartingPoint(start) if start < size => start..=size - 1,
58+
HttpRange::Range(range) if range.end() < size => range.start()..=range.end(),
59+
HttpRange::Suffix(suffix) if suffix > 0 && suffix <= size => size - suffix..=size - 1,
60+
_ => {
61+
let content_range = HttpContentRange::Unsatisfiable(Unsatisfiable::new(size));
62+
return Err(UnsatisfiableRange(content_range));
6863
}
69-
HttpRange::Range(ordered_range) if size > ordered_range.end() => {
70-
let start = ordered_range.start();
71-
let end = ordered_range.end();
64+
};
7265

73-
let content_range =
74-
HttpContentRange::Bound(Bound::new(start..=end, Some(size)).unwrap());
66+
let content_range = HttpContentRange::Bound(Bound::new(range.clone(), Some(size)).unwrap());
7567

76-
Ok(ContentRange {
77-
header: Some(content_range),
78-
range: start..=end,
79-
})
80-
}
81-
HttpRange::Suffix(suffix) if size.checked_sub(suffix).is_some_and(|start| start < size) => {
82-
let start = size - suffix;
83-
let end = size - 1;
84-
let content_range =
85-
HttpContentRange::Bound(Bound::new(start..=end, Some(size)).unwrap());
86-
87-
Ok(ContentRange {
88-
header: Some(content_range),
89-
range: start..=end,
90-
})
91-
}
92-
_ => {
93-
let content_range = HttpContentRange::Unsatisfiable(
94-
crate::headers::content_range::Unsatisfiable::new(size),
95-
);
96-
97-
Err(UnsatisfiableRange(content_range))
98-
}
99-
}
68+
Ok(ContentRange {
69+
header: Some(content_range),
70+
range,
71+
})
10072
}
10173

10274
/// A container for the payload slice and the optional `Content-Range` header.

0 commit comments

Comments
 (0)