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    E8s(E8s),
22    E18s(E18s),
23    Float32(Float32),
24    Float64(Float64),
25    Int(i64),
26    Int128(Int128),
27    IntBig(Int),
28    List(Vec<Self>),
29    Map(Vec<(Self, Self)>),
30    Null,
31    Principal(Principal),
32    Subaccount(Subaccount),
33    Text(String),
34    Timestamp(Timestamp),
35    Uint(u64),
36    Uint128(Nat128),
37    UintBig(Nat),
38    Ulid(Ulid),
39    Unit,
40}
41
42impl ValueWire {
43    // Decode recursively while enforcing runtime map invariants.
44    fn into_value(self) -> Result<Value, MapValueError> {
45        match self {
46            Self::Account(v) => Ok(Value::Account(v)),
47            Self::Blob(v) => Ok(Value::Blob(v)),
48            Self::Bool(v) => Ok(Value::Bool(v)),
49            Self::Date(v) => Ok(Value::Date(v)),
50            Self::Decimal(v) => Ok(Value::Decimal(v)),
51            Self::Duration(v) => Ok(Value::Duration(v)),
52            Self::Enum(v) => Ok(Value::Enum(v)),
53            Self::E8s(v) => Ok(Value::E8s(v)),
54            Self::E18s(v) => Ok(Value::E18s(v)),
55            Self::Float32(v) => Ok(Value::Float32(v)),
56            Self::Float64(v) => Ok(Value::Float64(v)),
57            Self::Int(v) => Ok(Value::Int(v)),
58            Self::Int128(v) => Ok(Value::Int128(v)),
59            Self::IntBig(v) => Ok(Value::IntBig(v)),
60            Self::List(items) => {
61                let items = items
62                    .into_iter()
63                    .map(Self::into_value)
64                    .collect::<Result<Vec<_>, _>>()?;
65                Ok(Value::List(items))
66            }
67            Self::Map(entries) => {
68                let entries = entries
69                    .into_iter()
70                    .map(|(key, value)| Ok((key.into_value()?, value.into_value()?)))
71                    .collect::<Result<Vec<_>, MapValueError>>()?;
72                Value::from_map(entries)
73            }
74            Self::Null => Ok(Value::Null),
75            Self::Principal(v) => Ok(Value::Principal(v)),
76            Self::Subaccount(v) => Ok(Value::Subaccount(v)),
77            Self::Text(v) => Ok(Value::Text(v)),
78            Self::Timestamp(v) => Ok(Value::Timestamp(v)),
79            Self::Uint(v) => Ok(Value::Uint(v)),
80            Self::Uint128(v) => Ok(Value::Uint128(v)),
81            Self::UintBig(v) => Ok(Value::UintBig(v)),
82            Self::Ulid(v) => Ok(Value::Ulid(v)),
83            Self::Unit => Ok(Value::Unit),
84        }
85    }
86}
87
88impl<'de> Deserialize<'de> for Value {
89    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
90    where
91        D: Deserializer<'de>,
92    {
93        let wire = ValueWire::deserialize(deserializer)?;
94        wire.into_value().map_err(serde::de::Error::custom)
95    }
96}