grafbase_sdk/types/
data.rs

1use serde::Serialize;
2
3use crate::{SdkError, cbor, wit};
4
5/// Data serialized in either JSON or CBOR
6#[derive(Debug)]
7pub enum Data {
8    /// JSON bytes
9    Json(Vec<u8>),
10    /// CBOR bytes
11    Cbor(Vec<u8>),
12}
13
14impl Data {
15    /// Serialize a type into the most efficient supported serialization
16    pub fn new<T: Serialize>(data: T) -> Result<Self, SdkError> {
17        let bytes = cbor::to_vec(&data)?;
18        Ok(Data::Cbor(bytes))
19    }
20}
21
22impl From<Data> for wit::Data {
23    fn from(value: Data) -> Self {
24        match value {
25            Data::Json(bytes) => wit::Data::Json(bytes),
26            Data::Cbor(bytes) => wit::Data::Cbor(bytes),
27        }
28    }
29}