tustvold commented on code in PR #5222:
URL: https://github.com/apache/arrow-rs/pull/5222#discussion_r1440394289


##########
object_store/src/util.rs:
##########
@@ -167,6 +178,167 @@ fn merge_ranges(ranges: &[std::ops::Range<usize>], 
coalesce: usize) -> Vec<std::
     ret
 }
 
+/// A single range in a `Range` request.
+///
+/// These can be created from [usize] ranges, like
+///
+/// ```rust
+/// # use byteranges::request::HttpRange;
+/// let range1: HttpRange = (50..150).into();
+/// let range2: HttpRange = (50..=150).into();
+/// let range3: HttpRange = (50..).into();
+/// let range4: HttpRange = (..150).into();
+/// ```
+#[derive(Debug, PartialEq, Eq, Clone)]
+pub enum GetRange {
+    /// A bounded byte range.
+    Bounded(Range<usize>),
+    /// A range defined only by the first byte requested (requests all 
remaining bytes).
+    Offset(usize),
+    /// A range defined as the number of bytes at the end of the resource.
+    Suffix(usize),
+}
+
+#[derive(Debug, Snafu)]
+pub enum InvalidGetRange {
+    #[snafu(display("Wanted suffix with {expected}B, resource was {actual}B 
long"))]
+    SuffixTooLarge { expected: usize, actual: usize },
+
+    #[snafu(display("Wanted range starting at {expected}, resource was 
{actual}B long"))]
+    StartTooLarge { expected: usize, actual: usize },
+
+    #[snafu(display("Wanted range ending at {expected}, resource was {actual}B 
long"))]
+    EndTooLarge { expected: usize, actual: usize },
+
+    #[snafu(display("Range started at {start} and ended at {end}"))]
+    Inconsistent { start: usize, end: usize },
+}
+
+impl GetRange {
+    /// Convert to a [std::ops::Range<usize>] if valid.
+    pub(crate) fn as_range(&self, len: usize) -> Result<Range<usize>, 
InvalidGetRange> {
+        match self {
+            Self::Bounded(r) => {
+                if r.start >= len {
+                    Err(InvalidGetRange::StartTooLarge {
+                        expected: r.start,
+                        actual: len,
+                    })
+                } else if r.end <= r.start {
+                    Err(InvalidGetRange::Inconsistent {
+                        start: r.start,
+                        end: r.end,
+                    })
+                } else if r.end >= len {
+                    Err(InvalidGetRange::EndTooLarge {
+                        expected: r.end,
+                        actual: len,
+                    })
+                } else {
+                    Ok(r.clone())
+                }
+            }
+            Self::Offset(o) => {
+                if o >= &len {
+                    Err(InvalidGetRange::StartTooLarge {
+                        expected: *o,
+                        actual: len,
+                    })
+                } else {
+                    Ok(*o..len)
+                }
+            }
+            Self::Suffix(n) => {
+                len.checked_sub(*n)
+                    .map(|start| start..len)
+                    .ok_or(InvalidGetRange::SuffixTooLarge {
+                        expected: *n,
+                        actual: len,
+                    })
+            }
+        }
+    }
+}
+
+impl Display for GetRange {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

Review Comment:
   Should this include the `bytes=` part?



##########
object_store/src/util.rs:
##########
@@ -279,66 +274,13 @@ impl<T: RangeBounds<usize>> From<T> for GetRange {
             Unbounded => 0,
         };
         match value.end_bound() {
-            Included(i) => GetRange::Bounded(first..(i + 1)),
-            Excluded(i) => GetRange::Bounded(first..*i),
-            Unbounded => GetRange::Offset(first),
+            Included(i) => Self::Bounded(first..(i + 1)),
+            Excluded(i) => Self::Bounded(first..*i),
+            Unbounded => Self::Offset(first),
         }
     }
 }
 
-#[derive(Debug, Snafu)]

Review Comment:
   This is moved into get.rs (and tweaked) to avoid feature flag issues



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to