crux_kv/
value.rs

1use serde::{Deserialize, Serialize};
2
3/// The value stored under a key.
4///
5/// `Value::None` is used to represent the absence of a value.
6///
7/// Note: we can't use `Option` here because generics are not currently
8/// supported across the FFI boundary, when using the builtin typegen.
9#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
10pub enum Value {
11    None,
12    Bytes(#[serde(with = "serde_bytes")] Vec<u8>),
13}
14
15impl From<Vec<u8>> for Value {
16    fn from(bytes: Vec<u8>) -> Self {
17        Value::Bytes(bytes)
18    }
19}
20
21impl From<Value> for Option<Vec<u8>> {
22    fn from(value: Value) -> Option<Vec<u8>> {
23        match value {
24            Value::None => None,
25            Value::Bytes(bytes) => Some(bytes),
26        }
27    }
28}
29
30impl From<Option<Vec<u8>>> for Value {
31    fn from(val: Option<Vec<u8>>) -> Self {
32        match val {
33            None => Value::None,
34            Some(bytes) => Value::Bytes(bytes),
35        }
36    }
37}