cnidarium/
utils.rs

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

    let end = match range.end_bound() {
        std::ops::Bound::Included(_) => bail!("included end bound not supported"),
        std::ops::Bound::Excluded(v) => Some(v.clone()),
        std::ops::Bound::Unbounded => None,
    };

    if let (Some(k_start), Some(k_end)) = (&start, &end) {
        if k_start > k_end {
            bail!("lower bound is greater than upper bound")
        }
    }

    Ok((range, (start, end)))
}