use fdb::error::{FdbError, FdbResult};
use fdb::range::Range;
use fdb::Key;
use std::convert::TryFrom;
use crate::range::{bytes_endpoint, HighEndpoint, LowEndpoint};
pub type KeyLowEndpoint = LowEndpoint<Key>;
pub type KeyHighEndpoint = HighEndpoint<Key>;
#[derive(Debug, Clone, PartialEq)]
pub struct KeyRange {
low_endpoint: KeyLowEndpoint,
high_endpoint: KeyHighEndpoint,
}
impl KeyRange {
pub fn new(low_endpoint: KeyLowEndpoint, high_endpoint: KeyHighEndpoint) -> KeyRange {
KeyRange {
low_endpoint,
high_endpoint,
}
}
pub fn from_keys(low_key: Key, high_key: Key) -> KeyRange {
let low_endpoint = LowEndpoint::RangeInclusive(low_key);
let high_endpoint = HighEndpoint::RangeExclusive(high_key);
KeyRange::new(low_endpoint, high_endpoint)
}
pub(crate) fn into_parts(self) -> (KeyLowEndpoint, KeyHighEndpoint) {
let KeyRange {
low_endpoint,
high_endpoint,
} = self;
(low_endpoint, high_endpoint)
}
}
impl TryFrom<KeyRange> for Range {
type Error = FdbError;
fn try_from(value: KeyRange) -> FdbResult<Range> {
bytes_endpoint::build_range_continuation(
&None, value, None, false, )
}
}
#[cfg(test)]
mod tests {
use bytes::Bytes;
use fdb::Key;
use crate::range::{HighEndpoint, LowEndpoint};
use super::KeyRange;
#[test]
fn from_keys() {
let low_key = Key::from(Bytes::from_static(b"abcd"));
let high_key = Key::from(Bytes::from_static(b"efgh"));
let (low_key_endpoint, high_key_endpoint) =
KeyRange::from_keys(low_key, high_key).into_parts();
assert!(
matches!(low_key_endpoint, LowEndpoint::RangeInclusive(key) if key == Key::from(Bytes::from_static(b"abcd")))
);
assert!(
matches!(high_key_endpoint, HighEndpoint::RangeExclusive(key) if key == Key::from(Bytes::from_static(b"efgh")))
);
}
}