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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
use std::fmt::Debug;
use serde::{Deserialize, Deserializer, Serialize};
/// BinaryKV is how data is represented programmatically in the database.
///
/// It accepts any type that implements `Serialize` and `Deserialize` from the `serde` crate
/// and uses it for the value of the key-value pair.
/// ```rust
/// use quick_kv::prelude::*;
///
/// BinaryKv::new("key".to_string(), "value".to_string());
/// ```
/// This is the same as:
/// ```rust
/// use quick_kv::prelude::*;
///
/// let data = BinaryKv {
/// key: "key".to_string(),
/// value: "value".to_string(),
/// };
/// ```
#[derive(Serialize, PartialEq, Debug, Clone, Eq, Hash, PartialOrd)]
pub struct BinaryKv<T>
where
T: Serialize + Clone + Debug,
{
/// The key of the key-value pair
pub key: String,
/// The value of the key-value pair
pub value: T,
}
impl<T> BinaryKv<T>
where
T: Serialize + Clone + Debug,
{
pub fn new(key: String, value: T) -> Self
{
BinaryKv { key, value }
}
}
impl<'de, T> Deserialize<'de> for BinaryKv<T>
where
T: Deserialize<'de> + Serialize + Clone + Debug,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct ValueHelper<T>
{
key: String,
value: T,
}
let helper = ValueHelper::<T>::deserialize(deserializer)?;
Ok(BinaryKv {
key: helper.key,
value: helper.value,
})
}
}
/// Bytes of data that are cached in memory for speed improvements on the mini client.
/// ```rust
/// use quick_kv::prelude::*;
/// use quick_kv::types::binarykv::BinaryKvCache;
///
/// let cache = BinaryKvCache::new("key".to_string(), "value".to_string().into_bytes());
#[derive(Serialize, PartialEq, Debug, Clone, Eq, Hash, PartialOrd)]
pub struct BinaryKvCache
{
pub key: String,
pub value: Vec<u8>,
}
impl BinaryKvCache
{
pub fn new(key: String, value: Vec<u8>) -> Self
{
Self { key, value }
}
}