use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Formatter};
use crate::proto;
#[derive(Serialize, Deserialize, Clone)]
pub struct Value {
pub version: i64,
pub value: Vec<u8>,
}
impl Debug for Value {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Value")
.field("version", &self.version)
.field(
"value",
&String::from_utf8(self.value.clone()).unwrap_or_else(|_| hex::encode(&self.value)),
)
.finish()
}
}
impl Into<(String, Value)> for proto::KeyValue {
fn into(self) -> (String, Value) {
(self.key, Value { version: self.version, value: self.value })
}
}
impl Into<proto::KeyValue> for (String, Option<Value>) {
fn into(self) -> proto::KeyValue {
let (key, v) = self;
let version = v.as_ref().map(|v| v.version).unwrap_or(-1);
let value = v.as_ref().map(|v| v.value.clone()).unwrap_or_default();
proto::KeyValue { key, version, value }
}
}
impl Into<proto::KeyValue> for (String, Value) {
fn into(self) -> proto::KeyValue {
let (key, v) = self;
proto::KeyValue { key, version: v.version, value: v.value }
}
}