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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
// Copyright (C) 2023 Parity Technologies (UK) Ltd. (admin@parity.io)
// This file is a part of the scale-decode crate.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//         http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! The [`Visitor`] trait and associated types.

mod decode;
pub mod types;

use scale_info::form::PortableForm;
use types::*;

pub use decode::decode_with_visitor;
pub(crate) use decode::decode_with_visitor_maybe_compact;

/// An implementation of the [`Visitor`] trait can be passed to the [`decode_with_visitor()`]
/// function, and is handed back values as they are encountered. It's up to the implementation
/// to decide what to do with these values.
pub trait Visitor: Sized {
    /// The type of the value to hand back from the [`decode_with_visitor()`] function.
    type Value<'scale, 'info>;
    /// The error type (which we must be able to convert a combination of [`Self`] and [`DecodeError`]s
    /// into, to handle any internal errors that crop up trying to decode things).
    type Error: From<DecodeError>;

    /// This method is called immediately upon running [`decode_with_visitor()`]. By default we ignore
    /// this call and return our visitor back (ie [`DecodeAsTypeResult::Skipped(visitor)`]). If you choose to
    /// do some decoding at this stage, return [`DecodeAsTypeResult::Decoded(result)`]. In either case, any bytes
    /// that you consume from the input (by altering what it points to) will be consumed for any subsequent visiting.
    ///
    /// # Warning
    ///
    /// Unlike the other `visit_*` methods, it is completely up to the implementor to decode and advance the
    /// bytes in a sensible way, and thus also possible for the implementor to screw this up. As a result,
    /// it's suggested that you don't implement this unless you know what you're doing.
    fn unchecked_decode_as_type<'scale, 'info>(
        self,
        _input: &mut &'scale [u8],
        _type_id: TypeId,
        _types: &'info scale_info::PortableRegistry,
    ) -> DecodeAsTypeResult<Self, Result<Self::Value<'scale, 'info>, Self::Error>> {
        DecodeAsTypeResult::Skipped(self)
    }

    /// This is called when a visitor function that you've not provided an implementation is called.
    /// You are provided an enum value corresponding to the function call, and can decide what to return
    /// in this case. The default is to return an error to announce the unexpected value.
    fn visit_unexpected<'scale, 'info>(
        self,
        unexpected: Unexpected,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        Err(DecodeError::Unexpected(unexpected).into())
    }

    /// Called when a bool is seen in the input bytes.
    fn visit_bool<'scale, 'info>(
        self,
        _value: bool,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::Bool)
    }
    /// Called when a char is seen in the input bytes.
    fn visit_char<'scale, 'info>(
        self,
        _value: char,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::Char)
    }
    /// Called when a u8 is seen in the input bytes.
    fn visit_u8<'scale, 'info>(
        self,
        _value: u8,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::U8)
    }
    /// Called when a u16 is seen in the input bytes.
    fn visit_u16<'scale, 'info>(
        self,
        _value: u16,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::U16)
    }
    /// Called when a u32 is seen in the input bytes.
    fn visit_u32<'scale, 'info>(
        self,
        _value: u32,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::U32)
    }
    /// Called when a u64 is seen in the input bytes.
    fn visit_u64<'scale, 'info>(
        self,
        _value: u64,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::U64)
    }
    /// Called when a u128 is seen in the input bytes.
    fn visit_u128<'scale, 'info>(
        self,
        _value: u128,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::U128)
    }
    /// Called when a u256 is seen in the input bytes.
    fn visit_u256<'info>(
        self,
        _value: &'_ [u8; 32],
        _type_id: TypeId,
    ) -> Result<Self::Value<'_, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::U256)
    }
    /// Called when an i8 is seen in the input bytes.
    fn visit_i8<'scale, 'info>(
        self,
        _value: i8,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::I8)
    }
    /// Called when an i16 is seen in the input bytes.
    fn visit_i16<'scale, 'info>(
        self,
        _value: i16,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::I16)
    }
    /// Called when an i32 is seen in the input bytes.
    fn visit_i32<'scale, 'info>(
        self,
        _value: i32,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::I32)
    }
    /// Called when an i64 is seen in the input bytes.
    fn visit_i64<'scale, 'info>(
        self,
        _value: i64,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::I64)
    }
    /// Called when an i128 is seen in the input bytes.
    fn visit_i128<'scale, 'info>(
        self,
        _value: i128,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::I128)
    }
    /// Called when an i256 is seen in the input bytes.
    fn visit_i256<'info>(
        self,
        _value: &'_ [u8; 32],
        _type_id: TypeId,
    ) -> Result<Self::Value<'_, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::I256)
    }
    /// Called when a sequence of values is seen in the input bytes.
    fn visit_sequence<'scale, 'info>(
        self,
        _value: &mut Sequence<'scale, 'info>,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::Sequence)
    }
    /// Called when a composite value is seen in the input bytes.
    fn visit_composite<'scale, 'info>(
        self,
        _value: &mut Composite<'scale, 'info>,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::Composite)
    }
    /// Called when a tuple of values is seen in the input bytes.
    fn visit_tuple<'scale, 'info>(
        self,
        _value: &mut Tuple<'scale, 'info>,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::Tuple)
    }
    /// Called when a string value is seen in the input bytes.
    fn visit_str<'scale, 'info>(
        self,
        _value: &mut Str<'scale>,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::Str)
    }
    /// Called when a variant is seen in the input bytes.
    fn visit_variant<'scale, 'info>(
        self,
        _value: &mut Variant<'scale, 'info>,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::Variant)
    }
    /// Called when an array is seen in the input bytes.
    fn visit_array<'scale, 'info>(
        self,
        _value: &mut Array<'scale, 'info>,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::Array)
    }
    /// Called when a bit sequence is seen in the input bytes.
    fn visit_bitsequence<'scale, 'info>(
        self,
        _value: &mut BitSequence<'scale>,
        _type_id: TypeId,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        self.visit_unexpected(Unexpected::Bitsequence)
    }
}

/// An error decoding SCALE bytes.
#[derive(Debug, Clone, PartialEq, Eq, derive_more::From, derive_more::Display)]
pub enum DecodeError {
    /// We ran into an error trying to decode a bit sequence.
    #[from]
    #[display(fmt = "Cannot decode bit sequence: {_0}")]
    BitSequenceError(BitSequenceError),
    /// The type we're trying to decode is supposed to be compact encoded, but that is not possible.
    #[display(fmt = "Could not decode compact encoded type into {_0:?}")]
    CannotDecodeCompactIntoType(scale_info::Type<PortableForm>),
    /// Failure to decode bytes into a string.
    #[from]
    #[display(fmt = "Could not decode string: {_0}")]
    InvalidStr(alloc::str::Utf8Error),
    /// We could not convert the [`u32`] that we found into a valid [`char`].
    #[display(fmt = "{_0} is expected to be a valid char, but is not")]
    InvalidChar(u32),
    /// We expected more bytes to finish decoding, but could not find them.
    #[display(fmt = "Ran out of data during decoding")]
    NotEnoughInput,
    /// We found a variant that does not match with any in the type we're trying to decode from.
    #[display(fmt = "Could not find variant with index {_0} in {_1:?}")]
    VariantNotFound(u8, scale_info::TypeDefVariant<PortableForm>),
    /// Some error emitted from a [`codec::Decode`] impl.
    #[from]
    CodecError(codec::Error),
    /// We could not find the type given in the type registry provided.
    #[display(fmt = "Cannot find type with ID {_0}")]
    TypeIdNotFound(u32),
    /// This is returned by default if a visitor function is not implemented.
    #[display(fmt = "Unexpected type {_0}")]
    Unexpected(Unexpected),
}

#[cfg(feature = "std")]
impl std::error::Error for DecodeError {}

/// This is returned by default when a visitor function isn't implemented.
#[derive(Debug, Clone, Copy, PartialEq, Eq, derive_more::Display)]
#[allow(missing_docs)]
pub enum Unexpected {
    #[display(fmt = "bool")]
    Bool,
    #[display(fmt = "char")]
    Char,
    #[display(fmt = "u8")]
    U8,
    #[display(fmt = "u16")]
    U16,
    #[display(fmt = "u32")]
    U32,
    #[display(fmt = "u64")]
    U64,
    #[display(fmt = "u128")]
    U128,
    #[display(fmt = "u256")]
    U256,
    #[display(fmt = "i8")]
    I8,
    #[display(fmt = "i16")]
    I16,
    #[display(fmt = "i32")]
    I32,
    #[display(fmt = "i64")]
    I64,
    #[display(fmt = "i128")]
    I128,
    #[display(fmt = "i256")]
    I256,
    #[display(fmt = "sequence")]
    Sequence,
    #[display(fmt = "composite")]
    Composite,
    #[display(fmt = "tuple")]
    Tuple,
    #[display(fmt = "str")]
    Str,
    #[display(fmt = "variant")]
    Variant,
    #[display(fmt = "array")]
    Array,
    #[display(fmt = "bitsequence")]
    Bitsequence,
}

/// The response from [`Visitor::unchecked_decode_as_type()`].
pub enum DecodeAsTypeResult<V, R> {
    /// Skip any manual decoding and return the visitor instead.
    Skipped(V),
    /// Some manually decoded result.
    Decoded(R),
}

/// This is implemented for visitor related types which have a `decode_item` method,
/// and allows you to generically talk about decoding unnamed items.
pub trait DecodeItemIterator<'scale, 'info> {
    /// Use a visitor to decode a single item.
    fn decode_item<V: Visitor>(
        &mut self,
        visitor: V,
    ) -> Option<Result<V::Value<'scale, 'info>, V::Error>>;
}

/// An error that can occur trying to decode a bit sequence.
pub type BitSequenceError = scale_bits::scale::format::FromMetadataError;

/// The ID of the type being decoded.
#[derive(Clone, Copy, Debug, Default)]
pub struct TypeId(pub u32);

/// A [`Visitor`] implementation that just ignores all of the bytes.
pub struct IgnoreVisitor;
impl Visitor for IgnoreVisitor {
    type Value<'scale, 'info> = ();
    type Error = DecodeError;

    // Whatever the value we visit is, just ignore it.
    fn visit_unexpected<'scale, 'info>(
        self,
        _unexpected: Unexpected,
    ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
        Ok(())
    }
}

#[cfg(test)]
mod test {
    use crate::visitor::TypeId;

    use super::*;
    use alloc::borrow::ToOwned;
    use alloc::string::{String, ToString};
    use alloc::vec;
    use alloc::vec::Vec;
    use codec::{self, CompactAs, Encode};
    use scale_info::PortableRegistry;

    /// A silly Value type for testing with a basic Visitor impl
    /// that tries to mirror what is called as best as possible.
    #[derive(Debug, PartialEq)]
    enum Value {
        Bool(bool),
        Char(char),
        U8(u8),
        U16(u16),
        U32(u32),
        U64(u64),
        U128(u128),
        U256([u8; 32]),
        I8(i8),
        I16(i16),
        I32(i32),
        I64(i64),
        I128(i128),
        I256([u8; 32]),
        Sequence(Vec<Value>),
        Composite(Vec<(String, Value)>),
        Tuple(Vec<Value>),
        Str(String),
        Array(Vec<Value>),
        Variant(String, Vec<(String, Value)>),
        BitSequence(scale_bits::Bits),
    }

    struct ValueVisitor;
    impl Visitor for ValueVisitor {
        type Value<'scale, 'info> = Value;
        type Error = DecodeError;

        fn visit_bool<'scale, 'info>(
            self,
            value: bool,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            Ok(Value::Bool(value))
        }
        fn visit_char<'scale, 'info>(
            self,
            value: char,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            Ok(Value::Char(value))
        }
        fn visit_u8<'scale, 'info>(
            self,
            value: u8,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            Ok(Value::U8(value))
        }
        fn visit_u16<'scale, 'info>(
            self,
            value: u16,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            Ok(Value::U16(value))
        }
        fn visit_u32<'scale, 'info>(
            self,
            value: u32,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            Ok(Value::U32(value))
        }
        fn visit_u64<'scale, 'info>(
            self,
            value: u64,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            Ok(Value::U64(value))
        }
        fn visit_u128<'scale, 'info>(
            self,
            value: u128,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            Ok(Value::U128(value))
        }
        fn visit_u256<'info>(
            self,
            value: &'_ [u8; 32],
            _type_id: TypeId,
        ) -> Result<Self::Value<'_, 'info>, Self::Error> {
            Ok(Value::U256(*value))
        }
        fn visit_i8<'scale, 'info>(
            self,
            value: i8,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            Ok(Value::I8(value))
        }
        fn visit_i16<'scale, 'info>(
            self,
            value: i16,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            Ok(Value::I16(value))
        }
        fn visit_i32<'scale, 'info>(
            self,
            value: i32,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            Ok(Value::I32(value))
        }
        fn visit_i64<'scale, 'info>(
            self,
            value: i64,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            Ok(Value::I64(value))
        }
        fn visit_i128<'scale, 'info>(
            self,
            value: i128,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            Ok(Value::I128(value))
        }
        fn visit_i256<'info>(
            self,
            value: &'_ [u8; 32],
            _type_id: TypeId,
        ) -> Result<Self::Value<'_, 'info>, Self::Error> {
            Ok(Value::I256(*value))
        }
        fn visit_sequence<'scale, 'info>(
            self,
            value: &mut Sequence<'scale, 'info>,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            let mut vals = vec![];
            while let Some(val) = value.decode_item(ValueVisitor) {
                let val = val?;
                vals.push(val);
            }
            Ok(Value::Sequence(vals))
        }
        fn visit_composite<'scale, 'info>(
            self,
            value: &mut Composite<'scale, 'info>,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            let mut vals = vec![];
            for item in value.by_ref() {
                let item = item?;
                let val = item.decode_with_visitor(ValueVisitor)?;
                let name = item.name().unwrap_or("").to_owned();
                vals.push((name, val));
            }
            Ok(Value::Composite(vals))
        }
        fn visit_tuple<'scale, 'info>(
            self,
            value: &mut Tuple<'scale, 'info>,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            let mut vals = vec![];
            while let Some(val) = value.decode_item(ValueVisitor) {
                let val = val?;
                vals.push(val);
            }
            Ok(Value::Tuple(vals))
        }
        fn visit_str<'scale, 'info>(
            self,
            value: &mut Str<'scale>,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            Ok(Value::Str(value.as_str()?.to_owned()))
        }
        fn visit_variant<'scale, 'info>(
            self,
            value: &mut Variant<'scale, 'info>,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            let mut vals = vec![];
            let fields = value.fields();
            for item in fields.by_ref() {
                let item = item?;
                let val = item.decode_with_visitor(ValueVisitor)?;
                let name = item.name().unwrap_or("").to_owned();
                vals.push((name, val));
            }
            Ok(Value::Variant(value.name().to_owned(), vals))
        }
        fn visit_array<'scale, 'info>(
            self,
            value: &mut Array<'scale, 'info>,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            let mut vals = vec![];
            while let Some(val) = value.decode_item(ValueVisitor) {
                let val = val?;
                vals.push(val);
            }
            Ok(Value::Array(vals))
        }
        fn visit_bitsequence<'scale, 'info>(
            self,
            value: &mut BitSequence<'scale>,
            _type_id: TypeId,
        ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
            let bools: Result<scale_bits::Bits, _> = value.decode()?.collect();
            Ok(Value::BitSequence(bools?))
        }
    }

    /// Given a type definition, return the PortableType and PortableRegistry
    /// that our decode functions expect.
    fn make_type<T: scale_info::TypeInfo + 'static>() -> (u32, PortableRegistry) {
        let m = scale_info::MetaType::new::<T>();
        let mut types = scale_info::Registry::new();
        let id = types.register_type(&m);
        let portable_registry: PortableRegistry = types.into();

        (id.id, portable_registry)
    }

    /// This just tests that if we try to decode some values we've encoded using a visitor
    /// which just ignores everything by default, that we'll consume all of the bytes.
    fn encode_decode_check_explicit_info<Ty: scale_info::TypeInfo + 'static, T: Encode>(
        val: T,
        expected: Value,
    ) {
        let encoded = val.encode();
        let (id, types) = make_type::<Ty>();
        let bytes = &mut &*encoded;
        let val = decode_with_visitor(bytes, id, &types, ValueVisitor)
            .expect("decoding should not error");

        assert_eq!(bytes.len(), 0, "Decoding should consume all bytes");
        assert_eq!(val, expected);
    }

    fn encode_decode_check<T: Encode + scale_info::TypeInfo + 'static>(val: T, expected: Value) {
        encode_decode_check_explicit_info::<T, T>(val, expected);
    }

    #[test]
    fn encode_decode_primitives() {
        encode_decode_check(123u8, Value::U8(123));
        encode_decode_check(123u16, Value::U16(123));
        encode_decode_check(123u32, Value::U32(123));
        encode_decode_check(123u64, Value::U64(123));
        encode_decode_check(123u128, Value::U128(123));
        encode_decode_check(codec::Compact(123u8), Value::U8(123));
        encode_decode_check(codec::Compact(123u16), Value::U16(123));
        encode_decode_check(codec::Compact(123u32), Value::U32(123));
        encode_decode_check(codec::Compact(123u64), Value::U64(123));
        encode_decode_check(codec::Compact(123u128), Value::U128(123));
        encode_decode_check(true, Value::Bool(true));
        encode_decode_check(false, Value::Bool(false));
        encode_decode_check_explicit_info::<char, _>('c' as u32, Value::Char('c'));
        encode_decode_check("Hello there", Value::Str("Hello there".to_owned()));
        encode_decode_check("Hello there".to_string(), Value::Str("Hello there".to_owned()));
    }

    #[test]
    fn decode_compact_named_wrapper_struct() {
        // A struct that can be compact encoded:
        #[derive(Encode, scale_info::TypeInfo, CompactAs)]
        struct MyWrapper {
            inner: u32,
        }

        encode_decode_check(
            codec::Compact(MyWrapper { inner: 123 }),
            Value::Composite(vec![("inner".into(), Value::U32(123))]),
        );
    }

    #[test]
    fn decode_compact_unnamed_wrapper_struct() {
        // A struct that can be compact encoded:
        #[derive(Encode, scale_info::TypeInfo, CompactAs)]
        struct MyWrapper(u32);

        encode_decode_check(
            codec::Compact(MyWrapper(123)),
            Value::Composite(vec![("".into(), Value::U32(123))]),
        );
    }

    #[test]
    fn decode_nested_compact_structs() {
        // A struct that has a nested field inner1, which can be compact encoded:
        #[derive(Encode, scale_info::TypeInfo)]
        struct Outer {
            #[codec(compact)]
            inner1: Inner1,
            other: u16,
        }

        #[derive(Encode, scale_info::TypeInfo, CompactAs)]
        struct Inner1 {
            inner2: Inner2,
        }

        #[derive(Encode, scale_info::TypeInfo, CompactAs)]
        struct Inner2(u64);

        let struct_to_check = Outer { inner1: Inner1 { inner2: Inner2(123) }, other: 42 };
        let expacted_value = Value::Composite(vec![
            (
                "inner1".into(),
                Value::Composite(vec![(
                    "inner2".into(),
                    Value::Composite(vec![("".into(), Value::U64(123))]),
                )]),
            ),
            ("other".into(), Value::U16(42)),
        ]);
        encode_decode_check(struct_to_check, expacted_value);
    }

    #[test]
    fn decode_compact_single_item_tuple_field() {
        // A struct that can be compact encoded:
        #[derive(Encode, scale_info::TypeInfo, CompactAs)]
        struct Struct {
            a: (u32,),
        }

        encode_decode_check(
            Struct { a: (123,) },
            Value::Composite(vec![("a".into(), Value::Tuple(vec![Value::U32(123)]))]),
        );
    }

    #[test]
    fn decode_sequence_array_tuple_types() {
        encode_decode_check(
            vec![1i32, 2, 3],
            Value::Sequence(vec![Value::I32(1), Value::I32(2), Value::I32(3)]),
        );
        encode_decode_check(
            [1i32, 2, 3], // compile-time length known
            Value::Array(vec![Value::I32(1), Value::I32(2), Value::I32(3)]),
        );
        encode_decode_check(
            (1i32, true, 123456u128),
            Value::Tuple(vec![Value::I32(1), Value::Bool(true), Value::U128(123456)]),
        );
    }

    #[test]
    fn decode_variant_types() {
        #[derive(Encode, scale_info::TypeInfo)]
        enum MyEnum {
            Foo(bool),
            Bar { hi: String, other: u128 },
        }

        encode_decode_check(
            MyEnum::Foo(true),
            Value::Variant("Foo".to_owned(), vec![(String::new(), Value::Bool(true))]),
        );
        encode_decode_check(
            MyEnum::Bar { hi: "hello".to_string(), other: 123 },
            Value::Variant(
                "Bar".to_owned(),
                vec![
                    ("hi".to_string(), Value::Str("hello".to_string())),
                    ("other".to_string(), Value::U128(123)),
                ],
            ),
        );
    }

    #[test]
    fn decode_composite_types() {
        #[derive(Encode, scale_info::TypeInfo)]
        struct Unnamed(bool, String, Vec<u8>);

        #[derive(Encode, scale_info::TypeInfo)]
        struct Named {
            is_valid: bool,
            name: String,
            bytes: Vec<u8>,
        }

        encode_decode_check(
            Unnamed(true, "James".into(), vec![1, 2, 3]),
            Value::Composite(vec![
                (String::new(), Value::Bool(true)),
                (String::new(), Value::Str("James".to_string())),
                (String::new(), Value::Sequence(vec![Value::U8(1), Value::U8(2), Value::U8(3)])),
            ]),
        );
        encode_decode_check(
            Named { is_valid: true, name: "James".into(), bytes: vec![1, 2, 3] },
            Value::Composite(vec![
                ("is_valid".to_string(), Value::Bool(true)),
                ("name".to_string(), Value::Str("James".to_string())),
                (
                    "bytes".to_string(),
                    Value::Sequence(vec![Value::U8(1), Value::U8(2), Value::U8(3)]),
                ),
            ]),
        );
    }

    #[test]
    fn decode_arrays() {
        encode_decode_check(
            [1u8, 2, 3],
            Value::Array(vec![Value::U8(1), Value::U8(2), Value::U8(3)]),
        )
    }

    #[test]
    fn decode_bit_sequence() {
        use bitvec::{
            bitvec,
            order::{Lsb0, Msb0},
        };
        use scale_bits::bits;

        // Check that Bits, as well as all compatible bitvecs, are properly decoded.
        encode_decode_check(bits![0, 1, 1, 0, 1, 0], Value::BitSequence(bits![0, 1, 1, 0, 1, 0]));
        encode_decode_check(
            bitvec![u8, Lsb0; 0, 1, 1, 0, 1, 0],
            Value::BitSequence(bits![0, 1, 1, 0, 1, 0]),
        );
        encode_decode_check(
            bitvec![u16, Lsb0; 0, 1, 1, 0, 1, 0],
            Value::BitSequence(bits![0, 1, 1, 0, 1, 0]),
        );
        encode_decode_check(
            bitvec![u32, Lsb0; 0, 1, 1, 0, 1, 0],
            Value::BitSequence(bits![0, 1, 1, 0, 1, 0]),
        );
        encode_decode_check(
            bitvec![u64, Lsb0; 0, 1, 1, 0, 1, 0],
            Value::BitSequence(bits![0, 1, 1, 0, 1, 0]),
        );
        encode_decode_check(
            bitvec![u8, Msb0; 0, 1, 1, 0, 1, 0],
            Value::BitSequence(bits![0, 1, 1, 0, 1, 0]),
        );
        encode_decode_check(
            bitvec![u16, Msb0; 0, 1, 1, 0, 1, 0],
            Value::BitSequence(bits![0, 1, 1, 0, 1, 0]),
        );
        encode_decode_check(
            bitvec![u32, Msb0; 0, 1, 1, 0, 1, 0],
            Value::BitSequence(bits![0, 1, 1, 0, 1, 0]),
        );
        encode_decode_check(
            bitvec![u64, Msb0; 0, 1, 1, 0, 1, 0],
            Value::BitSequence(bits![0, 1, 1, 0, 1, 0]),
        );
    }

    #[test]
    fn zero_copy_string_decoding() {
        let input = ("hello", "world");

        // The SCALE encoded bytes we want to zero-copy-decode from:
        let input_encoded = input.encode();

        // This can just zero-copy decode a string:
        struct ZeroCopyStrVisitor;
        impl Visitor for ZeroCopyStrVisitor {
            type Value<'scale, 'info> = &'scale str;
            type Error = DecodeError;

            fn visit_str<'scale, 'info>(
                self,
                value: &mut Str<'scale>,
                _type_id: TypeId,
            ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
                value.as_str()
            }
        }

        // This can zero-copy decode the pair of strings we have as input:
        struct ZeroCopyPairVisitor;
        impl Visitor for ZeroCopyPairVisitor {
            type Value<'scale, 'info> = (&'scale str, &'scale str);
            type Error = DecodeError;

            fn visit_tuple<'scale, 'info>(
                self,
                value: &mut Tuple<'scale, 'info>,
                _type_id: TypeId,
            ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
                let fst = value.decode_item(ZeroCopyStrVisitor).unwrap()?;
                let snd = value.decode_item(ZeroCopyStrVisitor).unwrap()?;
                Ok((fst, snd))
            }
        }

        let (ty_id, types) = make_type::<(&str, &str)>();
        let decoded =
            decode_with_visitor(&mut &*input_encoded, ty_id, &types, ZeroCopyPairVisitor).unwrap();
        assert_eq!(decoded, ("hello", "world"));
    }

    #[test]
    fn zero_copy_using_info_and_scale_lifetimes() {
        use alloc::collections::BTreeMap;

        #[derive(codec::Encode, scale_info::TypeInfo)]
        struct Foo {
            hello: String,
            world: String,
        }

        // The SCALE encoded bytes we want to zero-copy-decode from:
        let input_encoded = Foo { hello: "hi".to_string(), world: "planet".to_string() }.encode();

        // This can just zero-copy decode a string:
        struct ZeroCopyStrVisitor;
        impl Visitor for ZeroCopyStrVisitor {
            type Value<'scale, 'info> = &'scale str;
            type Error = DecodeError;

            fn visit_str<'scale, 'info>(
                self,
                value: &mut Str<'scale>,
                _type_id: TypeId,
            ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
                value.as_str()
            }
        }

        // This zero-copy decodes a composite into map of strings:
        struct ZeroCopyMapVisitor;
        impl Visitor for ZeroCopyMapVisitor {
            type Value<'scale, 'info> = alloc::collections::BTreeMap<&'info str, &'scale str>;
            type Error = DecodeError;

            fn visit_composite<'scale, 'info>(
                self,
                value: &mut Composite<'scale, 'info>,
                _type_id: TypeId,
            ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
                let mut vals = alloc::collections::BTreeMap::<&'info str, &'scale str>::new();
                for item in value {
                    let item = item?;
                    let Some(key) = item.name() else { continue };
                    let val = item.decode_with_visitor(ZeroCopyStrVisitor)?;
                    vals.insert(key, val);
                }
                Ok(vals)
            }
        }

        // Decode and check:
        let (ty_id, types) = make_type::<Foo>();
        let decoded =
            decode_with_visitor(&mut &*input_encoded, ty_id, &types, ZeroCopyMapVisitor).unwrap();
        assert_eq!(decoded, BTreeMap::from_iter([("hello", "hi"), ("world", "planet")]));
    }

    #[test]
    fn bailout_works() {
        let input = ("hello", "world");
        let (ty_id, types) = make_type::<(&str, &str)>();
        let input_encoded = input.encode();

        // Just return the scale encoded bytes and type ID to prove
        // that we can successfully "bail out".
        struct BailOutVisitor;
        impl Visitor for BailOutVisitor {
            type Value<'scale, 'info> = (&'scale [u8], u32);
            type Error = DecodeError;

            fn unchecked_decode_as_type<'scale, 'info>(
                self,
                input: &mut &'scale [u8],
                type_id: TypeId,
                _types: &'info scale_info::PortableRegistry,
            ) -> DecodeAsTypeResult<Self, Result<Self::Value<'scale, 'info>, Self::Error>>
            {
                DecodeAsTypeResult::Decoded(Ok((*input, type_id.0)))
            }
        }

        let decoded =
            decode_with_visitor(&mut &*input_encoded, ty_id, &types, BailOutVisitor).unwrap();
        assert_eq!(decoded, (&*input_encoded, ty_id));

        // We can also use this functionality to "fall-back" to a Decode impl
        // (though obviously with the caveat that this may be incorrect).
        struct CodecDecodeVisitor<T>(core::marker::PhantomData<T>);
        impl<T: codec::Decode> Visitor for CodecDecodeVisitor<T> {
            type Value<'scale, 'info> = T;
            type Error = DecodeError;

            fn unchecked_decode_as_type<'scale, 'info>(
                self,
                input: &mut &'scale [u8],
                _type_id: TypeId,
                _types: &'info scale_info::PortableRegistry,
            ) -> DecodeAsTypeResult<Self, Result<Self::Value<'scale, 'info>, Self::Error>>
            {
                DecodeAsTypeResult::Decoded(T::decode(input).map_err(|e| e.into()))
            }
        }

        let decoded: (String, String) = decode_with_visitor(
            &mut &*input_encoded,
            ty_id,
            &types,
            CodecDecodeVisitor(core::marker::PhantomData),
        )
        .unwrap();
        assert_eq!(decoded, ("hello".to_string(), "world".to_string()));
    }
}