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


##########
object_store/src/util.rs:
##########
@@ -167,6 +173,114 @@ 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 object_store::GetRange;
+/// let range1: GetRange = (50..150).into();
+/// let range2: GetRange = (50..=150).into();
+/// let range3: GetRange = (50..).into();
+/// let range4: GetRange = (..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(crate) 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 [`Range`] 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 {
+        match self {
+            Self::Bounded(r) => f.write_fmt(format_args!("bytes={}-{}", 
r.start, r.end - 1)),
+            Self::Offset(o) => f.write_fmt(format_args!("bytes={o}-")),
+            Self::Suffix(n) => f.write_fmt(format_args!("bytes=-{n}")),

Review Comment:
   ```suggestion
               Self::Bounded(r) => write!(f, "bytes={}-{}", r.start, r.end - 1),
               Self::Offset(o) => write!(f, "bytes={o}-"),
               Self::Suffix(n) => write!(f, "bytes=-{n}"),
   ```



-- 
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