Skip to main content

crux_kv/protocol/
value.rs

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