1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use serde::ser::SerializeStruct;
use serde::Serialize;

use crate::wrapper::*;
use crate::{ValueOwned, ValueRef};

impl<'a> AsRef<ValueRef<'a>> for ValueRef<'a> {
    #[inline]
    fn as_ref(&self) -> &ValueRef<'a> {
        self
    }
}

impl<'a> Serialize for ValueRef<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        if let ValueRef::Void = self {
            let mut st = serializer.serialize_struct("S", 1)?;
            st.serialize_field("type", "Void")?;
            return st.end();
        }
        let mut st = serializer.serialize_struct("S", 2)?;
        macro_rules! match_helper {
            (match self {
                $(ValueRef::$Variant:ident($pat:pat) => $exp:expr),+,
                _ => unreachable!(),
            }) => {
                match self {
                    $(ValueRef::$Variant($pat) => {
                        st.serialize_field("type", stringify!($Variant))?;
                        st.serialize_field("value", $exp)?;
                        st.end()
                    })+
                    _ => unreachable!(),
                }
            }
        }
        match_helper! {
            match self {
                ValueRef::Int(v) => wrap(v),
                ValueRef::Int8(v) => wrap(v),
                ValueRef::Int16(v) => wrap(v),
                ValueRef::Int32(v) => wrap(v),
                ValueRef::Int64(v) => wrap(v),
                ValueRef::Int128(v) => wrap(v),
                ValueRef::Int256(v) => wrap(v),
                ValueRef::UInt(v) => wrap(v),
                ValueRef::UInt8(v) => wrap(v),
                ValueRef::UInt16(v) => wrap(v),
                ValueRef::UInt32(v) => wrap(v),
                ValueRef::UInt64(v) => wrap(v),
                ValueRef::UInt128(v) => wrap(v),
                ValueRef::UInt256(v) => wrap(v),
                ValueRef::Fix64(f) => f,
                ValueRef::UFix64(f) => f,
                ValueRef::Word8(v) => wrap(&v.0),
                ValueRef::Word16(v) => wrap(&v.0),
                ValueRef::Word32(v) => wrap(&v.0),
                ValueRef::Word64(v) => wrap(&v.0),
                ValueRef::Bool(b) => b,
                ValueRef::Optional(o) => o,
                ValueRef::String(s) => s,
                ValueRef::Address(a) => a,
                ValueRef::Array(v) => v,
                ValueRef::Dictionary(v) => v,
                ValueRef::Struct(v) => v,
                ValueRef::Resource(v) => v,
                ValueRef::Event(v) => v,
                ValueRef::Contract(v) => v,
                ValueRef::Enum(v) => v,
                ValueRef::Path(v) => v,
                ValueRef::Type(v) => &TypeSer { static_type: v },
                ValueRef::Capability(v) => v,
                _ => unreachable!(),
            }
        }
    }
}

impl<'a> Serialize for ValueOwned {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        if let ValueOwned::Void = self {
            let mut st = serializer.serialize_struct("S", 1)?;
            st.serialize_field("type", "Void")?;
            return st.end();
        }
        let mut st = serializer.serialize_struct("S", 2)?;
        macro_rules! match_helper {
            (match self {
                $(ValueOwned::$Variant:ident($pat:pat) => $exp:expr),+,
                _ => unreachable!(),
            }) => {
                match self {
                    $(ValueOwned::$Variant($pat) => {
                        st.serialize_field("type", stringify!($Variant))?;
                        st.serialize_field("value", $exp)?;
                        st.end()
                    })+
                    _ => unreachable!(),
                }
            }
        }
        match_helper! {
            match self {
                ValueOwned::Int(v) => wrap(v),
                ValueOwned::Int8(v) => wrap(v),
                ValueOwned::Int16(v) => wrap(v),
                ValueOwned::Int32(v) => wrap(v),
                ValueOwned::Int64(v) => wrap(v),
                ValueOwned::Int128(v) => wrap(v),
                ValueOwned::Int256(v) => wrap(v),
                ValueOwned::UInt(v) => wrap(v),
                ValueOwned::UInt8(v) => wrap(v),
                ValueOwned::UInt16(v) => wrap(v),
                ValueOwned::UInt32(v) => wrap(v),
                ValueOwned::UInt64(v) => wrap(v),
                ValueOwned::UInt128(v) => wrap(v),
                ValueOwned::UInt256(v) => wrap(v),
                ValueOwned::Fix64(f) => f,
                ValueOwned::UFix64(f) => f,
                ValueOwned::Word8(v) => wrap(&v.0),
                ValueOwned::Word16(v) => wrap(&v.0),
                ValueOwned::Word32(v) => wrap(&v.0),
                ValueOwned::Word64(v) => wrap(&v.0),
                ValueOwned::Bool(b) => b,
                ValueOwned::Optional(o) => o,
                ValueOwned::String(s) => s,
                ValueOwned::Address(a) => a,
                ValueOwned::Array(v) => v,
                ValueOwned::Dictionary(v) => v,
                ValueOwned::Struct(v) => v,
                ValueOwned::Resource(v) => v,
                ValueOwned::Event(v) => v,
                ValueOwned::Contract(v) => v,
                ValueOwned::Enum(v) => v,
                ValueOwned::Path(v) => v,
                ValueOwned::Type(v) => &TypeSer { static_type: v },
                ValueOwned::Capability(v) => v,
                _ => unreachable!(),
            }
        }
    }
}