use bytes::Bytes;
#[derive(Clone, Debug, PartialEq)]
pub struct Key(Bytes);
impl From<Bytes> for Key {
fn from(b: Bytes) -> Key {
Key(b)
}
}
impl From<Key> for Bytes {
fn from(k: Key) -> Bytes {
k.0
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Value(Bytes);
impl From<Bytes> for Value {
fn from(b: Bytes) -> Value {
Value(b)
}
}
impl From<Value> for Bytes {
fn from(v: Value) -> Bytes {
v.0
}
}
#[derive(Clone, Debug)]
pub struct KeyValue {
key: Key,
value: Value,
}
impl KeyValue {
pub fn get_key_ref(&self) -> &Key {
&self.key
}
pub fn get_value_ref(&self) -> &Value {
&self.value
}
pub fn into_key(self) -> Key {
self.key
}
pub fn into_value(self) -> Value {
self.value
}
pub fn into_parts(self) -> (Key, Value) {
(self.key, self.value)
}
pub(crate) fn new(key: Key, value: Value) -> KeyValue {
KeyValue { key, value }
}
}
#[derive(Clone, Debug)]
pub struct KeySelector {
key: Key,
or_equal: bool,
offset: i32,
}
impl KeySelector {
pub fn new(key: impl Into<Key>, or_equal: bool, offset: i32) -> KeySelector {
KeySelector {
key: key.into(),
or_equal,
offset,
}
}
#[allow(clippy::should_implement_trait)]
pub fn add(self, offset: i32) -> KeySelector {
KeySelector::new(self.key, self.or_equal, self.offset + offset)
}
pub fn first_greater_or_equal(key: impl Into<Key>) -> KeySelector {
KeySelector::new(key, false, 1)
}
pub fn first_greater_than(key: impl Into<Key>) -> KeySelector {
KeySelector::new(key, true, 1)
}
pub fn get_key(&self) -> &Key {
&self.key
}
pub fn get_offset(&self) -> i32 {
self.offset
}
pub fn last_less_or_equal(key: impl Into<Key>) -> KeySelector {
KeySelector::new(key, true, 0)
}
pub fn last_less_than(key: impl Into<Key>) -> KeySelector {
KeySelector::new(key, false, 0)
}
pub(crate) fn deconstruct(self) -> (Key, bool, i32) {
let KeySelector {
key,
or_equal,
offset,
} = self;
(key, or_equal, offset)
}
}