Skip to main content

icydb_core/serialize/
mod.rs

1mod cbor;
2
3use crate::error::InternalError;
4use serde::{Serialize, de::DeserializeOwned};
5use thiserror::Error as ThisError;
6
7/// Generic CBOR serialization infrastructure.
8///
9/// This module is format-level only:
10/// - No database-layer constants or policy limits are defined here.
11/// - Callers that need bounded decode must pass explicit limits.
12/// - Engine-specific decode policy belongs in subsystem wrappers (for example, `db::codec`).
13
14///
15/// SerializeError
16///
17
18#[derive(Debug, ThisError)]
19pub enum SerializeError {
20    #[error("serialize error: {0}")]
21    Serialize(String),
22
23    #[error("deserialize error: {0}")]
24    Deserialize(String),
25}
26
27impl From<SerializeError> for InternalError {
28    fn from(err: SerializeError) -> Self {
29        Self::serialize_internal(err.to_string())
30    }
31}
32
33/// Serialize a value using the default `canic` serializer.
34///
35/// This helper keeps the error type aligned with the rest of `icydb`.
36pub fn serialize<T>(ty: &T) -> Result<Vec<u8>, SerializeError>
37where
38    T: Serialize,
39{
40    cbor::serialize(ty)
41}
42
43/// Deserialize a value produced by [`serialize`].
44pub fn deserialize<T>(bytes: &[u8]) -> Result<T, SerializeError>
45where
46    T: DeserializeOwned,
47{
48    cbor::deserialize(bytes)
49}
50
51/// Deserialize a value produced by [`serialize`], with an explicit size limit.
52///
53/// Size limits are caller policy, not serialization-format policy.
54pub fn deserialize_bounded<T>(bytes: &[u8], max_bytes: usize) -> Result<T, SerializeError>
55where
56    T: DeserializeOwned,
57{
58    cbor::deserialize_bounded(bytes, max_bytes)
59}