scale_info/ty/fields.rs
1// Copyright 2019-2022 Parity Technologies (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{
16 build::FieldBuilder,
17 form::{Form, MetaForm, PortableForm},
18 prelude::vec::Vec,
19 IntoPortable, Registry,
20};
21use scale::Encode;
22#[cfg(feature = "serde")]
23use serde::{de::DeserializeOwned, Deserialize, Serialize};
24
25/// A field of a struct-like data type.
26///
27/// Name is optional so it can represent both named and unnamed fields.
28///
29/// This can be a named field of a struct type or an enum struct variant, or an
30/// unnamed field of a tuple struct.
31///
32/// # Type name
33///
34/// The `type_name` field contains a string which is the name of the type of the
35/// field as it appears in the source code. The exact contents and format of the
36/// type name are not specified, but in practice will be the name of any valid
37/// type for a field e.g.
38///
39/// - Concrete types e.g `"u32"`, `"bool"`, `"Foo"` etc.
40/// - Type parameters e.g `"T"`, `"U"`
41/// - Generic types e.g `"Vec<u32>"`, `"Vec<T>"`
42/// - Associated types e.g. `"T::MyType"`, `"<T as MyTrait>::MyType"`
43/// - Type aliases e.g. `"MyTypeAlias"`, `"MyTypeAlias<T>"`
44/// - Other built in Rust types e.g. arrays, references etc.
45///
46/// Note that the type name doesn't correspond to the underlying type of the
47/// field, unless using a concrete type directly. Any given type may be referred
48/// to by multiple field type names, when using generic type parameters and type
49/// aliases.
50///
51/// This is intended for informational and diagnostic purposes only. Although it
52/// is possible to infer certain properties e.g. whether a type name is a type
53/// alias, there are no guarantees provided, and the type name representation
54/// may change.
55#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
56#[cfg_attr(
57 feature = "serde",
58 serde(bound(
59 serialize = "T::Type: Serialize, T::String: Serialize",
60 deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned",
61 ))
62)]
63#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
64#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
65#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
66#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Encode)]
67pub struct Field<T: Form = MetaForm> {
68 /// The name of the field. None for unnamed fields.
69 #[cfg_attr(
70 feature = "serde",
71 serde(skip_serializing_if = "Option::is_none", default)
72 )]
73 pub name: Option<T::String>,
74 /// The type of the field.
75 #[cfg_attr(feature = "serde", serde(rename = "type"))]
76 pub ty: T::Type,
77 /// The name of the type of the field as it appears in the source code.
78 #[cfg_attr(
79 feature = "serde",
80 serde(skip_serializing_if = "Option::is_none", default)
81 )]
82 pub type_name: Option<T::String>,
83 /// Documentation
84 #[cfg_attr(
85 feature = "serde",
86 serde(skip_serializing_if = "Vec::is_empty", default)
87 )]
88 pub docs: Vec<T::String>,
89}
90
91impl IntoPortable for Field {
92 type Output = Field<PortableForm>;
93
94 fn into_portable(self, registry: &mut Registry) -> Self::Output {
95 Field {
96 name: self.name.map(Into::into),
97 ty: registry.register_type(&self.ty),
98 type_name: self.type_name.map(Into::into),
99 docs: self.docs.into_iter().map(Into::into).collect(),
100 }
101 }
102}
103
104impl<T> Field<T>
105where
106 T: Form,
107{
108 /// Returns a new [`FieldBuilder`] for constructing a field.
109 pub fn builder() -> FieldBuilder<T> {
110 FieldBuilder::new()
111 }
112
113 /// Creates a new field.
114 ///
115 /// Use this constructor if you want to instantiate from a given meta type.
116 pub fn new(
117 name: Option<T::String>,
118 ty: T::Type,
119 type_name: Option<T::String>,
120 docs: Vec<T::String>,
121 ) -> Self {
122 Self {
123 name,
124 ty,
125 type_name,
126 docs,
127 }
128 }
129}
130
131impl<T> Field<T>
132where
133 T: Form,
134{
135 /// Returns the name of the field. None for unnamed fields.
136 #[deprecated(
137 since = "2.5.0",
138 note = "Prefer to access the fields directly; this getter will be removed in the next major version"
139 )]
140 pub fn name(&self) -> Option<&T::String> {
141 self.name.as_ref()
142 }
143
144 /// Returns the type of the field.
145 #[deprecated(
146 since = "2.5.0",
147 note = "Prefer to access the fields directly; this getter will be removed in the next major version"
148 )]
149 pub fn ty(&self) -> &T::Type {
150 &self.ty
151 }
152
153 /// Returns a string which is the name of the type of the field as it
154 /// appears in the source code. The exact contents and format of the type
155 /// name are not specified, but in practice will be the name of any valid
156 /// type for a field. This is intended for informational and diagnostic
157 /// purposes only.
158 #[deprecated(
159 since = "2.5.0",
160 note = "Prefer to access the fields directly; this getter will be removed in the next major version"
161 )]
162 pub fn type_name(&self) -> Option<&T::String> {
163 self.type_name.as_ref()
164 }
165
166 /// Returns the documentation of the field.
167 #[deprecated(
168 since = "2.5.0",
169 note = "Prefer to access the fields directly; this getter will be removed in the next major version"
170 )]
171 pub fn docs(&self) -> &[T::String] {
172 &self.docs
173 }
174}