Skip to main content

icydb_core/value/
wire.rs

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