lightning_storage_server/
model.rs

1use serde::{Deserialize, Serialize};
2use std::fmt::{Debug, Formatter};
3
4use crate::proto;
5
6#[derive(Serialize, Deserialize, Clone)]
7pub struct Value {
8    /// The version of the value.  These must be strictly increasing with no gaps.
9    /// This is used to detect concurrent updates.
10    /// Normally, we would used an unsigned integer, but databases don't map those well.
11    pub version: i64,
12    /// Client provided opaque value.
13    /// NOTE: the client should internally append an HMAC, but this is out of scope for the server
14    /// data model.
15    pub value: Vec<u8>,
16}
17
18impl Debug for Value {
19    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20        f.debug_struct("Value")
21            .field("version", &self.version)
22            // try to emit the value as a string, but if that fails, print the hex bytes
23            .field(
24                "value",
25                &String::from_utf8(self.value.clone()).unwrap_or_else(|_| hex::encode(&self.value)),
26            )
27            .finish()
28    }
29}
30
31impl Into<(String, Value)> for proto::KeyValue {
32    fn into(self) -> (String, Value) {
33        (self.key, Value { version: self.version, value: self.value })
34    }
35}
36
37// convert a conflict to proto
38impl Into<proto::KeyValue> for (String, Option<Value>) {
39    fn into(self) -> proto::KeyValue {
40        let (key, v) = self;
41        let version = v.as_ref().map(|v| v.version).unwrap_or(-1);
42        let value = v.as_ref().map(|v| v.value.clone()).unwrap_or_default();
43        proto::KeyValue { key, version, value }
44    }
45}
46
47// convert get result to proto
48impl Into<proto::KeyValue> for (String, Value) {
49    fn into(self) -> proto::KeyValue {
50        let (key, v) = self;
51        proto::KeyValue { key, version: v.version, value: v.value }
52    }
53}