Skip to main content

icydb_core/value/
wire.rs

1//! Module: value::wire
2//! Responsibility: module-local ownership and contracts for value::wire.
3//! Does not own: cross-module orchestration outside this module.
4//! Boundary: exposes this module API while keeping implementation details internal.
5
6use crate::{
7    types::*,
8    value::{MapValueError, Value, ValueEnum},
9};
10use serde::{Deserialize, Deserializer};
11
12///
13/// ValueWire
14/// Serde decode shape used to re-check Value::Map invariants during deserialization.
15///
16
17#[derive(Deserialize)]
18enum ValueWire {
19    Account(Account),
20    Blob(Vec<u8>),
21    Bool(bool),
22    Date(Date),
23    Decimal(Decimal),
24    Duration(Duration),
25    Enum(ValueEnum),
26    Float32(Float32),
27    Float64(Float64),
28    Int(i64),
29    Int128(Int128),
30    IntBig(Int),
31    List(Vec<Self>),
32    Map(Vec<(Self, Self)>),
33    Null,
34    Principal(Principal),
35    Subaccount(Subaccount),
36    Text(String),
37    Timestamp(Timestamp),
38    Uint(u64),
39    Uint128(Nat128),
40    UintBig(Nat),
41    Ulid(Ulid),
42    Unit,
43}
44
45impl ValueWire {
46    // Decode recursively while enforcing runtime map invariants.
47    fn into_value(self) -> Result<Value, MapValueError> {
48        match self {
49            Self::Account(v) => Ok(Value::Account(v)),
50            Self::Blob(v) => Ok(Value::Blob(v)),
51            Self::Bool(v) => Ok(Value::Bool(v)),
52            Self::Date(v) => Ok(Value::Date(v)),
53            Self::Decimal(v) => Ok(Value::Decimal(v)),
54            Self::Duration(v) => Ok(Value::Duration(v)),
55            Self::Enum(v) => Ok(Value::Enum(v)),
56            Self::Float32(v) => Ok(Value::Float32(v)),
57            Self::Float64(v) => Ok(Value::Float64(v)),
58            Self::Int(v) => Ok(Value::Int(v)),
59            Self::Int128(v) => Ok(Value::Int128(v)),
60            Self::IntBig(v) => Ok(Value::IntBig(v)),
61            Self::List(items) => {
62                let items = items
63                    .into_iter()
64                    .map(Self::into_value)
65                    .collect::<Result<Vec<_>, _>>()?;
66                Ok(Value::List(items))
67            }
68            Self::Map(entries) => {
69                let entries = entries
70                    .into_iter()
71                    .map(|(key, value)| Ok((key.into_value()?, value.into_value()?)))
72                    .collect::<Result<Vec<_>, MapValueError>>()?;
73                Value::from_map(entries)
74            }
75            Self::Null => Ok(Value::Null),
76            Self::Principal(v) => Ok(Value::Principal(v)),
77            Self::Subaccount(v) => Ok(Value::Subaccount(v)),
78            Self::Text(v) => Ok(Value::Text(v)),
79            Self::Timestamp(v) => Ok(Value::Timestamp(v)),
80            Self::Uint(v) => Ok(Value::Uint(v)),
81            Self::Uint128(v) => Ok(Value::Uint128(v)),
82            Self::UintBig(v) => Ok(Value::UintBig(v)),
83            Self::Ulid(v) => Ok(Value::Ulid(v)),
84            Self::Unit => Ok(Value::Unit),
85        }
86    }
87}
88
89impl<'de> Deserialize<'de> for Value {
90    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
91    where
92        D: Deserializer<'de>,
93    {
94        let wire = ValueWire::deserialize(deserializer)?;
95        wire.into_value().map_err(serde::de::Error::custom)
96    }
97}