cnidarium/
utils.rs

1use anyhow::bail;
2/// Splits a range into a tuple of start and end bounds, ignoring the inclusive/exclusive
3/// nature of the range bounds. And returns a tuple consisting of the range implementation,
4/// and the start and end bounds.
5/// # Errors
6/// This method returns an error when the range is inclusive on the end bound,
7/// and when the lower bound is greater than the upper bound.
8#[allow(clippy::type_complexity)]
9pub(crate) fn convert_bounds(
10    range: impl std::ops::RangeBounds<Vec<u8>>,
11) -> anyhow::Result<(
12    impl std::ops::RangeBounds<Vec<u8>>,
13    (Option<Vec<u8>>, Option<Vec<u8>>),
14)> {
15    let start = match range.start_bound() {
16        std::ops::Bound::Included(v) => Some(v.clone()),
17        std::ops::Bound::Excluded(v) => Some(v.clone()),
18        std::ops::Bound::Unbounded => None,
19    };
20
21    let end = match range.end_bound() {
22        std::ops::Bound::Included(_) => bail!("included end bound not supported"),
23        std::ops::Bound::Excluded(v) => Some(v.clone()),
24        std::ops::Bound::Unbounded => None,
25    };
26
27    if let (Some(k_start), Some(k_end)) = (&start, &end) {
28        if k_start > k_end {
29            bail!("lower bound is greater than upper bound")
30        }
31    }
32
33    Ok((range, (start, end)))
34}