kudu 0.1.0

Library for interacting with Antelope blockchains
Documentation
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
use std::collections::HashMap;

use serde_json::{
    json,
    Map as JsonMap,
    Value as JsonValue,
};
use snafu::{ensure, ResultExt};
use strum::VariantNames;
use tracing::{debug, warn, instrument};

use crate::{
    AntelopeType, AntelopeValue, Bytes, Name, VarUint32, TypeName,
    ABIDefinition, ByteStream, ABISerializable,
    abi::error::*,
    abi::definition::{
        TypeName as TypeNameOwned, Struct, Variant
    },
};

type Result<T, E = ABIError> = core::result::Result<T, E>;

// TODO: make sure that we can (de)serialize an ABI (ABIDefinition?) itself (eg, see: https://github.com/wharfkit/antelope/blob/master/src/chain/abi.ts, which implements ABISerializableObject)

#[derive(Default, Clone, Debug)]
pub struct ABI {
    // ABI-related fields
    typedefs: HashMap<TypeNameOwned, TypeNameOwned>,
    structs: HashMap<TypeNameOwned, Struct>,
    actions: HashMap<Name, TypeNameOwned>,
    tables: HashMap<Name, TypeNameOwned>,
    variants: HashMap<TypeNameOwned, Variant>,
    action_results: HashMap<Name, TypeNameOwned>,
}


impl ABI {
    pub fn new() -> Self {
        Self {
            typedefs: HashMap::new(),
            structs: HashMap::new(),
            actions: HashMap::new(),
            tables: HashMap::new(),
            variants: HashMap::new(),
            action_results: HashMap::new(),
        }
    }


    // -----------------------------------------------------------------------------
    //     Constructors and validation of ABI
    // -----------------------------------------------------------------------------

    pub fn from_definition(abi: &ABIDefinition) -> Result<Self> {
        let mut result = Self::new();
        result.set_abi(abi)?;
        Ok(result)
    }

    pub fn from_str(abi: &str) -> Result<Self> {
        Self::from_definition(&ABIDefinition::from_str(abi)?)
    }

    pub fn from_hex_abi(abi: &str) -> Result<Self> {
        Self::from_bin_abi(&hex::decode(abi)?)
    }

    pub fn from_bin_abi(abi: &[u8]) -> Result<Self> {
        let mut data = ByteStream::from(abi.to_owned());
        let abi_def = ABIDefinition::decode(&mut data)?;
        Self::from_definition(&abi_def)
    }

    fn set_abi(&mut self, abi: &ABIDefinition) -> Result<()> {
        ensure!(abi.version.starts_with("eosio::abi/1."), VersionSnafu { version: &abi.version });

        self.typedefs.clear();
        self.structs.clear();
        self.actions.clear();
        self.tables.clear();
        self.variants.clear();
        self.action_results.clear();

        self.structs.extend(abi.structs.iter().map(|s| (s.name.to_string(), s.clone())));

        for td in &abi.types {
            // note: this check seems redundant with the circular reference detection
            //       in `validate()` but this also checks that we have no duplicates
            //       between the previously defined structs and the typedefs
            ensure!(!self.is_type(TypeName(&td.new_type_name)),
                    IntegritySnafu { message: format!("type already exists: `{}`",
                                                      td.new_type_name) });
            self.typedefs.insert(td.new_type_name.clone(), td.type_.clone());
        }

        self.actions.extend(abi.actions.iter()
                            .map(|a| (a.name, a.type_.clone())));
        self.tables.extend(abi.tables.iter()
                           .map(|t| (t.name, t.type_.clone())));
        self.variants.extend(abi.variants.iter()
                             .map(|v| (v.name.clone(), v.clone())));
        self.action_results.extend(abi.action_results.iter()
                                   .map(|a| (a.name, a.result_type.clone())));

        // The ABIDefinition vectors may contain duplicates which would make it an invalid ABI
        ensure!(self.typedefs.len() == abi.types.len(),
                IntegritySnafu { message: "duplicate type definition detected" });
        ensure!(self.structs.len() == abi.structs.len(),
                IntegritySnafu { message: "duplicate struct definition detected" });
        ensure!(self.actions.len() == abi.actions.len(),
                IntegritySnafu { message: "duplicate action definition detected" });
        ensure!(self.tables.len() == abi.tables.len(),
                IntegritySnafu { message: "duplicate table definition detected" });
        ensure!(self.variants.len() == abi.variants.len(),
                IntegritySnafu { message: "duplicate variants definition detected" });
        ensure!(self.action_results.len() == abi.action_results.len(),
                IntegritySnafu { message: "" });

        self.validate()
    }

    pub fn is_type(&self, t: TypeName) -> bool {
        // NOTE: this would be a better behavior IMO but it doesn't match the C++ code
        //       for Antelope Spring; keep the latter for better compatibility

        let mut t = t;
        let mut ft = t.fundamental_type();
        while ft != t {
            t = ft;
            ft = t.fundamental_type();
        }

        // NOTE: this is the C++ Antelope Spring behavior
        // let t = t.fundamental_type();
        AntelopeValue::VARIANTS.contains(&t)
            || (self.typedefs.contains_key(t.as_str()) &&
               self.is_type(TypeName(self.typedefs.get(t.as_str()).unwrap())))  // safe unwrap
            || self.structs.contains_key(t.as_str())
            || self.variants.contains_key(t.as_str())
    }

    pub fn resolve_type<'a>(&'a self, t: TypeName<'a>) -> TypeName<'a> {
        let mut rtype = t;
        loop {
            match self.typedefs.get(rtype.as_str()) {
                Some(t) => rtype = TypeName(t),
                None => return rtype,
            }
        }
    }

    pub fn validate(&self) -> Result<(), ABIError> {
        // check there are no circular references in the typedefs definition
        for t in &self.typedefs {
            let mut types_seen = vec![t.0, t.1];
            let mut itr = self.typedefs.get(&t.1[..]);
            while itr.is_some() {
                let it = itr.unwrap();
                ensure!(!types_seen.contains(&it),
                        IntegritySnafu { message: format!("circular reference in type `{}`", t.0) });
                types_seen.push(it);
                itr = self.typedefs.get(it);
            }
        }

        // check all types used in typedefs are valid types
        for t in &self.typedefs {
            ensure!(!t.0.is_empty(),
                    IntegritySnafu { message: "empty name in typedef" });
            ensure!(self.is_type(t.1.into()),
                    IntegritySnafu { message: format!("invalid type used in typedef `{}`", t.1) });
        }

        // check there are no circular references in the structs definition
        for s in self.structs.values() {
            ensure!(!s.name.is_empty(), IntegritySnafu { message: "empty name in struct definition" });
            if !s.base.is_empty() {
                let mut current = s;
                let mut types_seen = vec![&current.name];
                while !current.base.is_empty() {
                    ensure!(self.structs.contains_key(&current.base),
                            IntegritySnafu { message: format!("invalid type used in '{}::base': `{}`", &s.name, &current.base) });
                    let base = self.structs.get(&current.base).unwrap();  // safe unwrap
                    ensure!(!types_seen.contains(&&base.name),
                            IntegritySnafu { message: format!("circular reference in struct '{}'", &s.name) });
                    types_seen.push(&base.name);
                    current = base;
                }
            }

            // check all field types are valid types
            for field in &s.fields {
                ensure!(self.is_type(TypeName(&field.type_[..]).remove_bin_extension()),
                        IntegritySnafu { message: format!("invalid type used in field '{}::{}': `{}`",
                                                          &s.name, &field.name, &field.type_) });
            }
        }

        // check all types from a variant are valid types
        for v in self.variants.values() {
            ensure!(!v.name.is_empty(), IntegritySnafu { message: "empty name in variant definition" });
            for t in &v.types {
                ensure!(self.is_type(t.into()),
                        IntegritySnafu { message: format!("invalid type `{}` used in variant '{}'",
                                                          t, v.name) });
            }
        }

        // check all actions are valid types
        for (name, type_) in &self.actions {
            ensure!(self.is_type(type_.into()),
                    IntegritySnafu { message: format!("invalid type `{}` used in action '{}'",
                                                      type_, name) });
        }

        // check all tables are valid types
        for (name, type_) in &self.tables {
            ensure!(self.is_type(type_.into()),
                    IntegritySnafu { message: format!("invalid type `{}` used in table '{}'",
                                                      type_, name) });
        }

        // check all action results are valid types
        for (name, type_) in &self.action_results {
            ensure!(self.is_type(type_.into()),
                    IntegritySnafu { message: format!("invalid type `{}` used in action result '{}'",
                                                      type_, name) });
        }

        Ok(())
    }


    // -----------------------------------------------------------------------------
    //     Encoding of variant -> binary
    // -----------------------------------------------------------------------------

    pub fn variant_to_binary<'a, T>(&self, typename: T, obj: &JsonValue)
                                    -> Result<Bytes>
    where
        T: Into<TypeName<'a>>
    {
        let mut ds = ByteStream::new();
        self.encode_variant(&mut ds, typename.into(), obj)?;
        Ok(ds.into())
    }

    #[inline]
    pub fn encode<T: ABISerializable>(&self, stream: &mut ByteStream, obj: &T) {
        obj.to_bin(stream)
    }

    #[inline]
    pub fn encode_variant<'a, T>(&self, ds: &mut ByteStream, typename: T, object: &JsonValue)
                                 -> Result<(), ABIError>
    where
        T: Into<TypeName<'a>>
    {
        self.encode_variant_(&mut VariantToBinaryContext::new(), ds, typename.into(), object)
    }

    #[instrument(skip(self, ctx, ds))]
    fn encode_variant_(&self, ctx: &mut VariantToBinaryContext, ds: &mut ByteStream,
                       typename: TypeName, object: &JsonValue)
                       -> Result<(), ABIError> {
        // see C++ implementation here: https://github.com/AntelopeIO/spring/blob/main/libraries/chain/abi_serializer.cpp#L493
        let rtype = self.resolve_type(typename);
        let ftype = rtype.fundamental_type();

        debug!(rtype=rtype.0, ftype=ftype.0);
        // check that the types are OK, similar to what `abieos` does in `get_type`
        // see: https://github.com/AntelopeIO/abieos/blob/main/src/abi.cpp#L46
        if rtype.is_optional() {
            ensure!(!ftype.is_optional() && !ftype.has_bin_extension(),
                    IntegritySnafu { message: format!("invalid optional nesting for type {rtype}") }); }
        if rtype.is_array() {
            ensure!(!ftype.is_optional() && !ftype.has_bin_extension(),
                    IntegritySnafu { message: format!("invalid array nesting for type {rtype}") }); }
        if rtype.has_bin_extension() {
            ensure!(!ftype.has_bin_extension(),
                    IntegritySnafu { message: format!("invalid extension nesting for type {rtype}") }); }

        // use a closure to avoid cloning and copying if no error occurs
        let incompatible_types = || { IncompatibleVariantTypesSnafu {
            typename: rtype.to_string(),
            value: Box::new(object.clone())
        }.build() };

        if AntelopeValue::VARIANTS.contains(&ftype) {
            // if our fundamental type is a builtin type, we can serialize it directly
            // to the stream
            let inner_type: AntelopeType = ftype.try_into().unwrap();  // safe unwrap
            if rtype.is_array() {
                let a = object.as_array().ok_or_else(incompatible_types)?;
                VarUint32::from(a.len()).to_bin(ds);
                for v in a {
                    AntelopeValue::from_variant(inner_type, v)
                        .with_context(|_| VariantConversionSnafu { v: v.clone() })?
                        .to_bin(ds);
                }
            }
            else if rtype.is_optional() {
                match !object.is_null() {
                    true => {
                        true.to_bin(ds);
                        AntelopeValue::from_variant(inner_type, object)
                            .with_context(|_| VariantConversionSnafu { v: object.clone() })?
                            .to_bin(ds);
                    },
                    false => false.to_bin(ds),
                }
            }
            else {
                AntelopeValue::from_variant(inner_type, object)
                    .with_context(|_| VariantConversionSnafu { v: object.clone() })?
                    .to_bin(ds);
            }
        }
        else {
            // not a builtin type, we have to recurse down

            if rtype.is_array() {
                let a = object.as_array().ok_or_else(incompatible_types)?;
                VarUint32::from(a.len()).to_bin(ds);
                for v in a {
                    self.encode_variant_(ctx, ds, ftype, v)?;
                }
            }
            else if rtype.is_optional() {
                match !object.is_null() {
                    true => {
                        true.to_bin(ds);
                        self.encode_variant_(ctx, ds, ftype, object)?;
                    },
                    false => false.to_bin(ds),
                }
            }
            else if let Some(variant_def) = self.variants.get(rtype.as_str()) {
                debug!("serializing type {:?} with variant: {:?}", rtype, object);
                ensure!(object.is_array() && object.as_array().unwrap().len() == 2,
                        EncodeSnafu {
                            message: format!("expected input to be an array of 2 elements while processing variant: {}",
                                             &object)
                        });
                ensure!(object[0].is_string(),
                        EncodeSnafu {
                            message: format!("expected variant typename to be a string: {}",
                                             object[0])
                        });
                let variant_type = TypeName(object[0].as_str().unwrap());
                if let Some(vpos) = variant_def.types.iter().position(|v| *v == variant_type) {
                    VarUint32::from(vpos).to_bin(ds);
                    self.encode_variant_(ctx, ds, variant_type, &object[1])?;
                }
                else {
                    EncodeSnafu {
                        message: format!("specified type `{}` is not valid within the variant '{}'",
                                         variant_type, rtype)
                    }.fail()?;
                }
            }
            else if let Some(struct_def) = self.structs.get(rtype.as_str()) {
                warn!(t=rtype.0, obj=object.to_string());
                self.encode_struct(ctx, ds, struct_def, object)?;
            }
            else {
                EncodeSnafu { message: format!("unknown ABI type: `{}`", rtype) }.fail()?;
            }
        }

        Ok(())
    }

    fn encode_struct(&self, ctx: &mut VariantToBinaryContext, ds: &mut ByteStream,
                     struct_def: &Struct, object: &JsonValue)
                     -> Result<(), ABIError> {
        // we want to serialize a struct...
        if let Some(obj) = object.as_object() {
            // ...and we are given an object -> serialize fields using their name
            if !struct_def.base.is_empty() {
                let _ = ctx.disallow_extensions_unless(false);
                self.encode_variant(ds, TypeName(&struct_def.base), object)?;
            }

            let mut allow_additional_fields = true;
            for (i, field) in struct_def.fields.iter().enumerate() {
                let ftype = TypeName(&field.type_);
                let nfields = struct_def.fields.len();
                let present: bool = obj.contains_key(&field.name);
                if present || ftype.is_optional() {
                    ensure!(allow_additional_fields,
                            EncodeSnafu { message: format!(
                                "Unexpected field '{}' found in input object while processing struct '{}'",
                                &field.name, &struct_def.name) });
                    let value = if present { obj.get(&field.name).unwrap() }  // safe unwrap
                    else                   { &JsonValue::Null };
                    // TODO: ctx.push_to_path
                    ctx.disallow_extensions_unless(i == nfields-1); // disallow except for the last field
                    self.encode_variant(ds, ftype.remove_bin_extension(), value)?;
                }
                else if ftype.has_bin_extension() && ctx.allow_extensions {
                    allow_additional_fields = false;
                }
                else if !allow_additional_fields {
                    EncodeSnafu { message: format!(
                        "Encountered field '{}' without binary extension designation while processing struct '{}'",
                        &field.name, &struct_def.name) }.fail()?;
                }
                else {
                    EncodeSnafu { message: format!(
                        "missing field '{}' in input object while processing struct '{}'",
                        &field.name, &struct_def.name) }.fail()?;
                }
            }
        }
        else if let Some(arr) = object.as_array() {
            // ..and we are given an array -> serialize fields using their position
            ensure!(struct_def.base.is_empty(),
                    EncodeSnafu { message: format!(concat!(
                        "using input array to specify the fields of the derived struct '{}'; ",
                        "input arrays are currently only allowed for structs without a base"
                    ), struct_def.name) });

            for (i, field) in struct_def.fields.iter().enumerate() {
                // let field = &struct_def.fields[i];
                let ftype = TypeName(&field.type_);
                let nfields = struct_def.fields.len();
                if i < arr.len() {
                    // TODO: ctx.push_to_path
                    ctx.disallow_extensions_unless(i == nfields-1);
                    self.encode_variant(ds, ftype.remove_bin_extension(), &arr[i])?;
                }
                else if ftype.has_bin_extension() && ctx.allow_extensions {
                    break;
                }
                else {
                    EncodeSnafu { message: format!(concat!(
                        "early end to input array specifying the fields of struct '{}'; ",
                        "require input for field '{}'"
                    ), struct_def.name, field.name) }.fail()?;
                }
            }
        }
        else {
            EncodeSnafu { message: format!(
                "unexpected input while encoding struct '{}': {}",
                struct_def.name, object) }.fail()?;
        }

        Ok(())
    }


    // -----------------------------------------------------------------------------
    //     Decoding of binary data -> variant
    // -----------------------------------------------------------------------------

    pub fn binary_to_variant<'a, T>(&self, typename: T, bytes: Bytes)
                                    -> Result<JsonValue>
    where
        T: Into<TypeName<'a>>
    {
        let mut ds = ByteStream::from(bytes);
        self.decode_variant_(&mut ds, typename.into())
    }


    #[inline]
    pub fn decode_variant<'a, T>(&self, ds: &mut ByteStream, typename: T) -> Result<JsonValue, ABIError>
    where
        T: Into<TypeName<'a>>
    {
        self.decode_variant_(ds, typename.into())
    }

    #[allow(clippy::collapsible_else_if)]
    fn decode_variant_(&self, ds: &mut ByteStream, typename: TypeName) -> Result<JsonValue, ABIError> {
        let rtype = self.resolve_type(typename);
        let ftype = rtype.fundamental_type();

        Ok(if AntelopeValue::VARIANTS.contains(&ftype) {
            let type_ = ftype.try_into().unwrap();  // safe unwrap

            // if our fundamental type is a builtin type, we can deserialize it directly
            // from the stream
            if rtype.is_array() {
                let item_count = decode_usize(ds, "item_count (as varuint32)")?;
                debug!(r#"reading array of {item_count} elements of type "{ftype}""#);
                // limit the maximum size that can be reserved before data is read
                let initial_capacity = item_count.min(1024);
                let mut a = Vec::with_capacity(initial_capacity);
                // loop {}
                for _ in 0..item_count {
                    a.push(read_value(ds, type_, "array item")?);
                }
                JsonValue::Array(a)
            }
            else if rtype.is_optional() {
                let non_null = bool::from_bin(ds)
                    .context(DeserializeSnafu { what: "optional discriminant" })?;
                match non_null {
                    true => read_value(ds, type_, "optional value")?,
                    false => JsonValue::Null,
                }
            }
            else {
                read_value(ds, type_, "single `AntelopeValue`")?
            }
        }
        else {
            if rtype.is_array() {
                // not a builtin type, we have to recurse down
                let item_count = decode_usize(ds, "item_count (as varuint32)")?;
                debug!(r#"reading array of {item_count} elements22 of type "{ftype}""#);
                // limit the maximum size that can be reserved before data is read
                let initial_capacity = item_count.min(1024);
                let mut a = Vec::with_capacity(initial_capacity);
                // loop {}
                for _ in 0..item_count {
                    a.push(self.decode_variant(ds, ftype)?);
                }
                JsonValue::Array(a)
            }
            else if rtype.is_optional() {
                let non_null = bool::from_bin(ds)
                    .context(DeserializeSnafu { what: "optional discriminant" })?;
                match non_null {
                    true => self.decode_variant(ds, ftype)?,
                    false => JsonValue::Null,
                }
            }
            else if let Some(variant_def) = self.variants.get(rtype.as_str()) {
                let variant_tag: usize = decode_usize(ds, "variant tag (as varuint32)")?;
                ensure!(variant_tag < variant_def.types.len(),
                        DecodeSnafu { message: format!("deserialized invalid tag {} for variant {}",
                                                       variant_tag, rtype)
                        });
                let variant_type = TypeName(&variant_def.types[variant_tag]);
                json!([variant_type, self.decode_variant(ds, variant_type)?])
            }
            else if let Some(struct_def) = self.structs.get(rtype.as_str()) {
                self.decode_struct(ds, struct_def)?
            }
            else {
                DecodeSnafu { message: format!("do not know how to deserialize type: {}", rtype) }.fail()?
            }
        })
    }

    fn decode_struct(&self, ds: &mut ByteStream, struct_def: &Struct) -> Result<JsonValue, ABIError> {
        debug!(r#"reading struct with name "{}" and base "{}""#, struct_def.name, struct_def.base);

        let mut result: JsonMap<String, JsonValue> = JsonMap::new();

        if !struct_def.base.is_empty() {
            // result.insert("base".to_owned(), json!(struct_def.base));
            let base_def = self.structs.get(&struct_def.base).unwrap();
            let mut base = self.decode_struct(ds, base_def)?;
            debug!("base {base:?}");
            // array(&mut result, "fields").append(array(&mut base, "fields"));
            result.append(base.as_object_mut().unwrap());
        }

        let mut encountered_extension = false;
        let nfields = struct_def.fields.len();
        debug!("reading {nfields} fields");
        for field in &struct_def.fields {
            let fname = &field.name;
            let ftype = TypeName(&field.type_);
            encountered_extension |= ftype.has_bin_extension();
            if ds.leftover().is_empty() {
                if ftype.has_bin_extension() {
                    continue;
                }
                ensure!(!encountered_extension,
                        DecodeSnafu { message: format!(
                            "encountered field '{}' without binary extension designation while processing struct '{}'",
                            fname, &struct_def.name) });

                DecodeSnafu { message: format!(
                    "stream ended unexpectedly; unable to unpack field '{}' of struct '{}'",
                    fname, struct_def.name) }.fail()?
            }

            let rtype = self.resolve_type(ftype.remove_bin_extension());
            let value = self.decode_variant(ds, rtype)?;
            debug!(r#"decoded field '{fname}' with type `{ftype}`: {value}"#);
            result.insert(fname.to_string(), value);
        }

        debug!("fully decoded `{}` struct: {:#?}", struct_def.name, result);
        Ok(JsonValue::Object(result))
    }
}

fn read_value(stream: &mut ByteStream, type_: AntelopeType, what: &str) ->  Result<JsonValue, ABIError> {
    Ok(AntelopeValue::from_bin(type_, stream)
       .context(DeserializeSnafu { what })?.to_variant())
}

fn decode_usize(stream: &mut ByteStream, what: &str) -> Result<usize, ABIError> {
    let n = VarUint32::from_bin(stream).context(DeserializeSnafu { what })?;
    Ok(n.into())
}



// TODO: rename this ScopeGuard?
struct ScopeExit<T>
where
    T: FnMut()
{
    callback: T,
}

impl<T: FnMut()> ScopeExit<T> {
    pub fn new(f: T) -> ScopeExit<T> {
        ScopeExit { callback: f }
    }
}

impl<T: FnMut()> Drop for ScopeExit<T> {
    fn drop(&mut self) {
        (self.callback)()
    }
}

struct VariantToBinaryContext {
    allow_extensions: bool,
}

impl VariantToBinaryContext {
    pub fn new() -> VariantToBinaryContext {
        VariantToBinaryContext { allow_extensions: true }
    }

    pub fn disallow_extensions_unless(&mut self, cond: bool) -> ScopeExit<impl FnMut() + '_> {
        let old_allow_extensions = self.allow_extensions;

        if !cond { self.allow_extensions = false }

        let callback = move || { self.allow_extensions = old_allow_extensions; };
        ScopeExit::new(callback)
    }
}