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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0

use byteorder::LittleEndian;
use std::convert::TryInto;
use std::io::Read;

use crate::de::{Enum, LooselyTyped, MapAccess, SeqAccess};
use crate::error::{Error, Result};
use crate::value::layout::Summand;
use crate::value::layout::{BinProtRule, Polyvar, TaggedPolyvar};
use crate::Deserializer as DS;
use crate::ReadBinProtExt;
use serde::de::Visitor;
use serde::{Deserialize, Serialize};

use byteorder::ReadBytesExt;

impl<'de, R: Read> DS<R, LooselyTyped> {
    /// The loose deserializer version of deserialize
    /// Only implemented on the LooselyTyped variant of the Deserializer
    pub fn deserialize_loose<V>(&mut self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        match self.mode.layout_iter.next() {
            Some(rule) => {
                match rule {
                    BinProtRule::Unit => {
                        self.rdr.bin_read_unit()?;
                        visitor.visit_unit()
                    }
                    BinProtRule::Record(fields) => {
                        // Grab the field names from the rule to pass to the map access
                        visitor.visit_map(MapAccess::new(
                            self,
                            fields.into_iter().map(|f| f.field_name).rev().collect(),
                        ))
                    }
                    BinProtRule::Tuple(items) => {
                        visitor.visit_seq(SeqAccess::new(self, items.len()))
                    }
                    BinProtRule::Bool => visitor.visit_bool(self.rdr.bin_read_bool()?),
                    BinProtRule::Sum(summands) => {
                        // read the enum variant index.
                        // We need this to select which variant layout to use
                        // when deserializing the variants data
                        let index = self.rdr.bin_read_variant_index()?;
                        let variant_rules = summands[index as usize].ctor_args.clone();
                        self.mode
                            .layout_iter
                            .push(vec![BinProtRule::Tuple(variant_rules)]);
                        visitor.visit_enum(ValueEnum::new(
                            self,
                            VariantType::Sum(summands[index as usize].clone()),
                        ))
                    }
                    BinProtRule::Polyvar(summands) => {
                        let tag = self.rdr.bin_read_polyvar_tag()?;
                        let (index, variant) = summands
                            .into_iter()
                            .enumerate()
                            .find_map(|(i, v)| {
                                match v {
                                    Polyvar::Tagged(t) => {
                                        // return the first tagged variant where the tag matches
                                        if t.hash == tag {
                                            Some((i, t))
                                        } else {
                                            None
                                        }
                                    }
                                    Polyvar::Inherited(_) => unimplemented!(), // don't know how to handle these yet
                                }
                            })
                            .ok_or(Error::UnknownPolyvarTag(tag))?;
                        self.mode
                            .layout_iter
                            .push(vec![BinProtRule::Tuple(variant.clone().polyvar_args)]);
                        visitor.visit_enum(ValueEnum::new(
                            self,
                            VariantType::Polyvar(index as u8, variant),
                        ))
                    }
                    BinProtRule::Option(some_rule) => {
                        let index = self.rdr.bin_read_variant_index()?; // 0 or 1
                        match index {
                            0 => visitor.visit_none(),
                            1 => {
                                self.mode.layout_iter.push(vec![*some_rule]);
                                visitor.visit_some(self)
                            }
                            _ => Err(Error::InvalidOptionByte { got: index }),
                        }
                    }
                    BinProtRule::String => visitor.visit_bytes(&self.rdr.bin_read_bytes()?),
                    BinProtRule::Float => visitor.visit_f64(self.rdr.read_f64::<LittleEndian>()?),
                    BinProtRule::Char => {
                        let c = self.rdr.read_u8()?;
                        visitor.visit_char(c as char)
                    }
                    BinProtRule::List(element_rule) => {
                        // read the length
                        let len = self.rdr.bin_read_nat0()?;
                        // request the iterator repeats the list elements the current number of times
                        self.mode.layout_iter.push_n(*element_rule, len);
                        // read the elements
                        visitor.visit_seq(SeqAccess::new_list(self, len))
                    }
                    BinProtRule::Int
                    | BinProtRule::Int32
                    | BinProtRule::Int64
                    | BinProtRule::NativeInt => visitor.visit_i64(self.rdr.bin_read_integer()?),
                    BinProtRule::Vec(_, _)
                    | BinProtRule::Nat0
                    | BinProtRule::Hashtable(_)
                    | BinProtRule::TypeVar(_)
                    | BinProtRule::Bigstring
                    | BinProtRule::SelfReference(_)
                    | BinProtRule::TypeClosure(_, _)
                    | BinProtRule::TypeAbstraction(_, _)
                    | BinProtRule::Reference(_) => Err(Error::UnimplementedRule), // Don't know how to implement these yet
                    BinProtRule::Custom(_) => {
                        // the traverse function should never produce this
                        Err(Error::LayoutIteratorError)
                    }
                    BinProtRule::CustomForPath(path, rules) => {
                        // here is where custom deser methods can be looked up by path
                        match path.as_str() {
                            // These vector types will be handled like any other sequence
                            "Pickles_type.Vector.Vector2" // the missing 's' on 'types' here is intention due to a bug in layout producing code
                            | "Pickles_types.Vector.Vector2" // in case it gets fixed :P
                            | "Pickles_types.Vector.Vector4"
                            | "Pickles_types.Vector.Vector8"
                            | "Pickles_types.Vector.Vector17"
                            | "Pickles_types.Vector.Vector18" => {
                                let element_rule = rules.first().unwrap();
                                let len = match path.as_str() {
                                    "Pickles_type.Vector.Vector2"
                                    | "Pickles_types.Vector.Vector2" => 2,
                                    "Pickles_types.Vector.Vector4" => 4,
                                    "Pickles_types.Vector.Vector8" => 8,
                                    "Pickles_types.Vector.Vector17" => 17,
                                    "Pickles_types.Vector.Vector18" => 18,
                                    _ => unreachable!()
                                };
                                self.mode.layout_iter.push(vec![BinProtRule::Unit]); // zero byte terminator, will be read last
                                self.mode.layout_iter.push_n(element_rule.clone(), len);
                                visitor.visit_seq(SeqAccess::new(self, len+1))
                            }
                            "Ledger_hash0" // these are all BigInt (32 bytes)
                            | "State_hash"
                            | "Pending_coinbase.Stack_hash"
                            | "State_body_hash"
                            | "Pending_coinbase.Hash_builder"
                            | "Snark_params.Make_inner_curve_scalar"
                            | "Snark_params.Tick"
                            | "Epoch_seed"
                            | "Zexe_backend.Zexe_backend_common.Stable.Field"
                            | "Pending_coinbase.Coinbase_stack" => {
                                // force it to read a 32 element long tuple of u8/chars
                                self.mode.layout_iter.push_n(BinProtRule::Char, 32);
                                visitor.visit_seq(SeqAccess::new(self, 32))
                            }
                            _ => Err(Error::UnknownCustomType{ typ: path })
                        }
                    }
                }
            }
            None => Err(Error::UnexpectedEndOfLayout),
        }
    }
}

pub enum VariantType {
    Sum(Summand),
    Polyvar(u8, TaggedPolyvar),
}

// for accessing enums when using the loosely typed method
// to deserialize into a Value
pub struct ValueEnum<'a, R: Read, Mode> {
    de: &'a mut DS<R, Mode>,
    variant: VariantType,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum EnumData {
    Sum {
        index: u8,
        name: String,
        len: usize,
    },
    Polyvar {
        index: u8,
        tag: u32,
        name: String,
        len: usize,
    },
}

impl<'a, R: Read, Mode> ValueEnum<'a, R, Mode> {
    fn new(de: &'a mut DS<R, Mode>, variant: VariantType) -> Self {
        Self { de, variant }
    }
}

impl<'de, 'a, R: Read> serde::de::EnumAccess<'de> for ValueEnum<'a, R, LooselyTyped> {
    type Error = Error;
    type Variant = Enum<'a, R, LooselyTyped>;

    fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant)>
    where
        V: serde::de::DeserializeSeed<'de>,
    {
        // bit of a hack here. visit_enum in the visitor is expecting to be able to
        // deserialize the enum details (e.g. variant index and name) from the stream.
        // Since in this case it comes from the layout file we need to serialize this data
        // and then return the deserializer to be handled by visit_enum

        let (index, enum_data) = match self.variant {
            VariantType::Sum(summand) => (
                summand.index as u8,
                EnumData::Sum {
                    index: summand.index.try_into().unwrap(),
                    name: summand.ctor_name,
                    len: summand.ctor_args.len(),
                },
            ),
            VariantType::Polyvar(index, polyvar) => (
                index,
                EnumData::Polyvar {
                    index,
                    tag: polyvar.hash,
                    name: polyvar.polyvar_name,
                    len: polyvar.polyvar_args.len(),
                },
            ),
        };

        let mut buf = Vec::<u8>::new();
        crate::to_writer(&mut buf, &enum_data).unwrap();
        let mut de = DS::from_reader(buf.as_slice());
        let v = seed.deserialize(&mut de)?;

        Ok((v, Enum::new(self.de, index)))
    }
}