mz_storage_operators/oneshot_source/
util.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Utility functions for Oneshot sources.
11
12/// Utility trait for converting various Rust Range types into a header value.
13/// according to the MDN Web Docs.
14///
15/// See: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range>
16pub trait IntoRangeHeaderValue {
17    fn into_range_header_value(&self) -> String;
18}
19
20impl IntoRangeHeaderValue for std::ops::Range<usize> {
21    fn into_range_header_value(&self) -> String {
22        let inclusive_end = std::cmp::max(self.start, self.end.saturating_sub(1));
23        (self.start..=inclusive_end).into_range_header_value()
24    }
25}
26
27impl IntoRangeHeaderValue for std::ops::RangeInclusive<usize> {
28    fn into_range_header_value(&self) -> String {
29        // See <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range>.
30        format!("bytes={}-{}", self.start(), self.end())
31    }
32}