icydb_core/
serialize.rs

1use crate::Error;
2use canic::serialize::{deserialize as canic_deserialize, serialize as canic_serialize};
3use serde::{Serialize, de::DeserializeOwned};
4use thiserror::Error as ThisError;
5
6/// Serialization errors surfaced through the `icydb` convenience helpers.
7#[derive(Debug, ThisError)]
8pub enum SerializeError {
9    #[error(transparent)]
10    SerializeError(#[from] canic::Error),
11}
12
13/// Serialize a value using the default `canic` serializer.
14///
15/// This helper keeps the error type aligned with the rest of `icydb`.
16pub fn serialize<T>(ty: &T) -> Result<Vec<u8>, Error>
17where
18    T: Serialize,
19{
20    canic_serialize(ty)
21        .map_err(SerializeError::from)
22        .map_err(Error::from)
23}
24
25/// Deserialize a value produced by [`serialize`].
26pub fn deserialize<T>(bytes: &[u8]) -> Result<T, Error>
27where
28    T: DeserializeOwned,
29{
30    canic_deserialize(bytes)
31        .map_err(SerializeError::from)
32        .map_err(Error::from)
33}