icydb_core/
serialize.rs

1mod cbor;
2
3use crate::error::{ErrorClass, ErrorOrigin, InternalError};
4use serde::{Serialize, de::DeserializeOwned};
5use thiserror::Error as ThisError;
6
7///
8/// SerializeError
9///
10
11#[derive(Debug, ThisError)]
12pub enum SerializeError {
13    #[error("serialize error: {0}")]
14    Serialize(String),
15    #[error("deserialize error: {0}")]
16    Deserialize(String),
17}
18
19impl SerializeError {
20    pub(crate) const fn class() -> ErrorClass {
21        ErrorClass::Internal
22    }
23}
24
25impl From<SerializeError> for InternalError {
26    fn from(err: SerializeError) -> Self {
27        Self::new(
28            SerializeError::class(),
29            ErrorOrigin::Serialize,
30            err.to_string(),
31        )
32    }
33}
34/// Serialize a value using the default `canic` serializer.
35///
36/// This helper keeps the error type aligned with the rest of `icydb`.
37pub fn serialize<T>(ty: &T) -> Result<Vec<u8>, SerializeError>
38where
39    T: Serialize,
40{
41    cbor::serialize(ty)
42}
43
44/// Deserialize a value produced by [`serialize`].
45pub fn deserialize<T>(bytes: &[u8]) -> Result<T, SerializeError>
46where
47    T: DeserializeOwned,
48{
49    cbor::deserialize(bytes)
50}