scale_info_legacy/
type_shape.rs

1// Copyright (C) 2024 Parity Technologies (UK) Ltd. (admin@parity.io)
2// This file is a part of the scale-info-legacy crate.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//         http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! This module provides a [`TypeShape`] enum, which describes the shape of a type, or in other
17//! words, how it should be SCALE encoded/decoded.
18
19use crate::lookup_name::LookupName;
20use alloc::string::String;
21use alloc::vec::Vec;
22
23pub use scale_type_resolver::{BitsOrderFormat, BitsStoreFormat, Primitive};
24
25/// This describes the shape of a type, with the aim of providing enough information
26/// that we know how to SCALE encode or decode some named type.
27#[derive(Debug, Clone)]
28pub enum TypeShape {
29    /// A "named composite" type in scale-info. This contains a list
30    /// of fields.
31    NamedStructOf(Vec<Field>),
32    /// An "unnamed composite" type in scale-info.
33    UnnamedStructOf(Vec<LookupName>),
34    /// An enum containing a list of variants.
35    EnumOf(Vec<Variant>),
36    /// A sequence of some type.
37    SequenceOf(LookupName),
38    /// A bit sequence.
39    BitSequence {
40        /// The order type is expected to resolve to a type with the path
41        /// `bitvec::order::Lsb0` or `bitvec::order::Msb0`.
42        order: LookupName,
43        /// The store type is expected to resolve to a primitive U8/U16/U32/U64.
44        store: LookupName,
45    },
46    /// A compact encoded type.
47    Compact(LookupName),
48    /// A primitive type.
49    Primitive(Primitive),
50    /// An alias to some other type in the registry. The
51    /// alias can be something like `Vec<T>` or `[u8; 16]` or `Bar`.
52    AliasOf(LookupName),
53}
54
55/// A struct field.
56#[derive(Debug, Clone)]
57#[cfg_attr(test, derive(PartialEq))]
58pub struct Field {
59    /// The struct field name.
60    pub name: String,
61    /// The shape of the field value.
62    pub value: LookupName,
63}
64
65/// An enum variant.
66#[derive(Debug, Clone)]
67#[cfg_attr(test, derive(PartialEq))]
68pub struct Variant {
69    /// The variant index.
70    pub index: u8,
71    /// The variant name.
72    pub name: String,
73    /// Shape of the variant's arguments.
74    pub fields: VariantDesc,
75}
76
77/// The shape of the variant.
78#[derive(Debug, Clone)]
79#[cfg_attr(test, derive(PartialEq))]
80pub enum VariantDesc {
81    /// named variant fields are basically a struct.
82    NamedStructOf(Vec<Field>),
83    /// Unnamed variant fields are basically a tuple of type descriptions.
84    UnnamedStructOf(Vec<LookupName>),
85}