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
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
//
// 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.
//! Builders for defining metadata for variant types (enums), and composite types (structs).
//! They are designed to allow only construction of valid definitions.
//!
//! In most cases we recommend using the `scale-info-derive` crate to auto generate the builder
//! constructions.
//!
//! # Examples
//!
//! ## Generic struct
//! ```
//! # use scale_info::{build::Fields, type_params, MetaType, Path, Type, TypeInfo};
//! struct Foo<T> {
//! bar: T,
//! data: u64,
//! }
//!
//! impl<T> TypeInfo for Foo<T>
//! where
//! T: TypeInfo + 'static,
//! {
//! type Identity = Self;
//!
//! fn type_info() -> Type {
//! Type::builder()
//! .path(Path::new("Foo", module_path!()))
//! .type_params(type_params!(T))
//! .composite(Fields::named()
//! .field(|f| f.ty::<T>().name("bar").type_name("T"))
//! .field(|f| f.ty::<u64>().name("data").type_name("u64"))
//! )
//! }
//! }
//! ```
//! ## Tuple struct
//! ```
//! # use scale_info::{build::Fields, MetaType, Path, Type, TypeInfo};
//! struct Foo(u32, bool);
//!
//! impl TypeInfo for Foo {
//! type Identity = Self;
//!
//! fn type_info() -> Type {
//! Type::builder()
//! .path(Path::new("Foo", module_path!()))
//! .composite(Fields::unnamed()
//! .field(|f| f.ty::<u32>().type_name("u32"))
//! .field(|f| f.ty::<bool>().type_name("bool"))
//! )
//! }
//! }
//! ```
//! ## Enum with fields
//! ```
//! # use scale_info::{build::{Fields, Variants}, type_params, MetaType, Path, Type, TypeInfo, Variant};
//! enum Foo<T>{
//! A(T),
//! B { f: u32 },
//! C,
//! }
//!
//! impl<T> TypeInfo for Foo<T>
//! where
//! T: TypeInfo + 'static,
//! {
//! type Identity = Self;
//!
//! fn type_info() -> Type {
//! Type::builder()
//! .path(Path::new("Foo", module_path!()))
//! .type_params(type_params!(T))
//! .variant(
//! Variants::new()
//! .variant("A", |v| v
//! .index(0)
//! .fields(Fields::unnamed().field(|f| f.ty::<T>().type_name("T")))
//! )
//! .variant("B", |v| v
//! .index(1)
//! .fields(Fields::named().field(|f| f.ty::<u32>().name("f").type_name("u32")))
//! )
//! .variant_unit("A", 2)
//! )
//! }
//! }
//! ```
//! ## Enum without fields, aka C-style enums.
//! ```
//! # use scale_info::{build::{Fields, Variants}, MetaType, Path, Type, TypeInfo, Variant};
//! enum Foo {
//! A,
//! B,
//! C = 33,
//! }
//!
//! impl TypeInfo for Foo {
//! type Identity = Self;
//!
//! fn type_info() -> Type {
//! Type::builder()
//! .path(Path::new("Foo", module_path!()))
//! .variant(
//! Variants::new()
//! .variant("A", |v| v.index(1))
//! .variant("B", |v| v.index(2))
//! .variant("C", |v| v.index(33))
//! )
//! }
//! }
//! ```
use crate::prelude::{marker::PhantomData, vec::Vec};
use crate::{
form::{Form, MetaForm, PortableForm},
Field, MetaType, Path, Type, TypeDef, TypeDefComposite, TypeDefVariant, TypeInfo,
TypeParameter, Variant,
};
/// State types for type builders which require a Path.
pub mod state {
/// State where the builder has not assigned a Path to the type
pub enum PathNotAssigned {}
/// State where the builder has assigned a Path to the type
pub enum PathAssigned {}
}
/// Builds a [`Type`](`crate::Type`)
#[must_use]
pub struct TypeBuilder<F: Form = MetaForm, S = state::PathNotAssigned> {
path: Option<Path<F>>,
type_params: Vec<TypeParameter<F>>,
docs: Vec<F::String>,
marker: PhantomData<fn() -> (F, S)>,
}
impl<F: Form, S> Default for TypeBuilder<F, S> {
fn default() -> Self {
TypeBuilder {
path: Default::default(),
type_params: Default::default(),
docs: Default::default(),
marker: Default::default(),
}
}
}
impl<F: Form> TypeBuilder<F, state::PathNotAssigned> {
/// Set the Path for the type
pub fn path(self, path: Path<F>) -> TypeBuilder<F, state::PathAssigned> {
TypeBuilder {
path: Some(path),
type_params: self.type_params,
docs: self.docs,
marker: Default::default(),
}
}
}
impl<F: Form> TypeBuilder<F, state::PathAssigned> {
fn build<D>(self, type_def: D) -> Type<F>
where
D: Into<TypeDef<F>>,
{
let path = self.path.expect("Path not assigned");
Type::new(path, self.type_params, type_def, self.docs)
}
/// Construct a "variant" type i.e an `enum`
pub fn variant(self, builder: Variants<F>) -> Type<F> {
self.build(builder.finalize())
}
/// Construct a "composite" type i.e. a `struct`
pub fn composite<T>(self, fields: FieldsBuilder<F, T>) -> Type<F> {
self.build(TypeDefComposite::new(fields.finalize()))
}
}
impl<F: Form, S> TypeBuilder<F, S> {
/// Set the type parameters if it's a generic type
pub fn type_params<I>(mut self, type_params: I) -> Self
where
I: IntoIterator<Item = TypeParameter<F>>,
{
self.type_params = type_params.into_iter().collect();
self
}
}
impl<S> TypeBuilder<PortableForm, S> {
#[cfg(feature = "docs")]
/// Set the type documentation (for types in portable form).
pub fn docs_portable<I>(mut self, docs: I) -> Self
where
I: IntoIterator<Item = <PortableForm as Form>::String>,
{
self.docs = docs.into_iter().collect();
self
}
}
impl<S> TypeBuilder<MetaForm, S> {
#[cfg(feature = "docs")]
/// Set the type documentation
pub fn docs(mut self, docs: &[&'static str]) -> Self {
self.docs = docs.to_vec();
self
}
#[cfg(not(feature = "docs"))]
#[inline]
/// Doc capture is not enabled via the "docs" feature so this is a no-op.
pub fn docs(self, _docs: &'static [&'static str]) -> Self {
self
}
/// Set the type documentation, always captured even if the "docs" feature is not enabled.
pub fn docs_always(mut self, docs: &[&'static str]) -> Self {
self.docs = docs.to_vec();
self
}
}
/// A fields builder has no fields (e.g. a unit struct)
pub enum NoFields {}
/// A fields builder only allows named fields (e.g. a struct)
pub enum NamedFields {}
/// A fields builder only allows unnamed fields (e.g. a tuple)
pub enum UnnamedFields {}
/// Provides FieldsBuilder constructors
pub struct Fields<F: Form>(PhantomData<fn() -> F>);
impl<F: Form> Fields<F> {
/// The type construct has no fields
pub fn unit() -> FieldsBuilder<F, NoFields> {
FieldsBuilder::<F, NoFields>::default()
}
/// Fields for a type construct with named fields
pub fn named() -> FieldsBuilder<F, NamedFields> {
FieldsBuilder::default()
}
/// Fields for a type construct with unnamed fields
pub fn unnamed() -> FieldsBuilder<F, UnnamedFields> {
FieldsBuilder::default()
}
}
/// Build a set of either all named (e.g. for a struct) or all unnamed (e.g. for a tuple struct)
#[must_use]
pub struct FieldsBuilder<F: Form, T> {
fields: Vec<Field<F>>,
marker: PhantomData<fn() -> T>,
}
impl<F: Form, T> Default for FieldsBuilder<F, T> {
fn default() -> Self {
Self {
fields: Vec::new(),
marker: Default::default(),
}
}
}
impl<F: Form, T> FieldsBuilder<F, T> {
/// Complete building and return the set of fields
pub fn finalize(self) -> Vec<Field<F>> {
self.fields
}
}
impl<T> FieldsBuilder<MetaForm, T> {
fn push_field(mut self, field: Field) -> Self {
// filter out fields of PhantomData
if !field.ty.is_phantom() {
self.fields.push(field);
}
self
}
}
impl FieldsBuilder<MetaForm, NamedFields> {
/// Add a named field constructed using the builder.
pub fn field<B>(self, builder: B) -> Self
where
B: Fn(
FieldBuilder,
)
-> FieldBuilder<MetaForm, field_state::NameAssigned, field_state::TypeAssigned>,
{
let builder = builder(FieldBuilder::new());
self.push_field(builder.finalize())
}
}
impl FieldsBuilder<MetaForm, UnnamedFields> {
/// Add an unnamed field constructed using the builder.
pub fn field<B>(self, builder: B) -> Self
where
B: Fn(
FieldBuilder,
)
-> FieldBuilder<MetaForm, field_state::NameNotAssigned, field_state::TypeAssigned>,
{
let builder = builder(FieldBuilder::new());
self.push_field(builder.finalize())
}
}
impl<T> FieldsBuilder<PortableForm, T> {
fn push_field(mut self, field: Field<PortableForm>) -> Self {
self.fields.push(field);
self
}
}
impl FieldsBuilder<PortableForm, NamedFields> {
/// Add a named field constructed using the builder.
pub fn field_portable<B>(self, builder: B) -> Self
where
B: Fn(
FieldBuilder<PortableForm, field_state::NameNotAssigned, field_state::TypeNotAssigned>,
)
-> FieldBuilder<PortableForm, field_state::NameAssigned, field_state::TypeAssigned>,
{
let builder = builder(FieldBuilder::new());
self.push_field(builder.finalize())
}
}
impl FieldsBuilder<PortableForm, UnnamedFields> {
/// Add an unnamed field constructed using the builder.
pub fn field_portable<B>(self, builder: B) -> Self
where
B: Fn(
FieldBuilder<PortableForm, field_state::NameNotAssigned, field_state::TypeNotAssigned>,
) -> FieldBuilder<
PortableForm,
field_state::NameNotAssigned,
field_state::TypeAssigned,
>,
{
let builder = builder(FieldBuilder::new());
self.push_field(builder.finalize())
}
}
/// Type states for building a field.
pub mod field_state {
/// A name has not been assigned to the field.
pub enum NameNotAssigned {}
/// A name has been assigned to the field.
pub enum NameAssigned {}
/// A type has not been assigned to the field.
pub enum TypeNotAssigned {}
/// A type has been assigned to the field.
pub enum TypeAssigned {}
}
/// Construct a valid [`Field`].
#[must_use]
pub struct FieldBuilder<
F: Form = MetaForm,
N = field_state::NameNotAssigned,
T = field_state::TypeNotAssigned,
> {
name: Option<F::String>,
ty: Option<F::Type>,
type_name: Option<F::String>,
docs: Vec<F::String>,
marker: PhantomData<fn() -> (N, T)>,
}
impl<F: Form, N, T> Default for FieldBuilder<F, N, T> {
fn default() -> Self {
FieldBuilder {
name: Default::default(),
ty: Default::default(),
type_name: Default::default(),
docs: Default::default(),
marker: Default::default(),
}
}
}
impl<F: Form> FieldBuilder<F> {
/// Create a new FieldBuilder.
pub fn new() -> Self {
Default::default()
}
}
impl<F: Form, T> FieldBuilder<F, field_state::NameNotAssigned, T> {
/// Initialize the field name.
pub fn name(self, name: F::String) -> FieldBuilder<F, field_state::NameAssigned, T> {
FieldBuilder {
name: Some(name),
ty: self.ty,
type_name: self.type_name,
docs: self.docs,
marker: PhantomData,
}
}
}
impl<N> FieldBuilder<MetaForm, N, field_state::TypeNotAssigned> {
/// Initialize the type of the field.
pub fn ty<TY>(self) -> FieldBuilder<MetaForm, N, field_state::TypeAssigned>
where
TY: TypeInfo + 'static + ?Sized,
{
FieldBuilder {
name: self.name,
ty: Some(MetaType::new::<TY>()),
type_name: self.type_name,
docs: self.docs,
marker: PhantomData,
}
}
/// Initializes the type of the field as a compact type.
pub fn compact<TY>(self) -> FieldBuilder<MetaForm, N, field_state::TypeAssigned>
where
TY: scale::HasCompact + TypeInfo + 'static,
{
FieldBuilder {
name: self.name,
ty: Some(MetaType::new::<scale::Compact<TY>>()),
type_name: self.type_name,
docs: self.docs,
marker: PhantomData,
}
}
}
impl<N> FieldBuilder<PortableForm, N, field_state::TypeNotAssigned> {
/// Initialize the type of the field.
pub fn ty<T>(self, ty: T) -> FieldBuilder<PortableForm, N, field_state::TypeAssigned>
where
T: Into<<PortableForm as Form>::Type>,
{
FieldBuilder {
name: self.name,
ty: Some(ty.into()),
type_name: self.type_name,
docs: self.docs,
marker: PhantomData,
}
}
}
impl<F: Form, N, T> FieldBuilder<F, N, T> {
/// Initialize the type name of a field (optional).
pub fn type_name(self, type_name: F::String) -> FieldBuilder<F, N, T> {
FieldBuilder {
name: self.name,
ty: self.ty,
type_name: Some(type_name),
docs: self.docs,
marker: PhantomData,
}
}
}
impl<N, T> FieldBuilder<PortableForm, N, T> {
#[cfg(feature = "docs")]
/// Initialize the documentation of a field (for types in portable form, optional).
pub fn docs_portable<I>(mut self, docs: I) -> Self
where
I: IntoIterator<Item = <PortableForm as Form>::String>,
{
self.docs = docs.into_iter().collect();
self
}
}
impl<N, T> FieldBuilder<MetaForm, N, T> {
#[cfg(feature = "docs")]
/// Initialize the documentation of a field (optional).
pub fn docs(self, docs: &'static [&'static str]) -> Self {
FieldBuilder {
name: self.name,
ty: self.ty,
type_name: self.type_name,
docs: docs.to_vec(),
marker: PhantomData,
}
}
#[cfg(not(feature = "docs"))]
#[inline]
/// Doc capture is not enabled via the "docs" feature so this is a no-op.
pub fn docs(self, _docs: &'static [&'static str]) -> Self {
self
}
/// Initialize the documentation of a field, always captured even if the "docs" feature is not
/// enabled.
pub fn docs_always(self, docs: &'static [&'static str]) -> Self {
FieldBuilder {
name: self.name,
ty: self.ty,
type_name: self.type_name,
docs: docs.to_vec(),
marker: PhantomData,
}
}
}
impl<F: Form, N> FieldBuilder<F, N, field_state::TypeAssigned> {
/// Complete building and return a new [`Field`].
pub fn finalize(self) -> Field<F> {
Field::new(
self.name,
self.ty.expect("Type should be set by builder"),
self.type_name,
self.docs,
)
}
}
/// Builds a definition of a variant type i.e an `enum`
#[derive(Default)]
#[must_use]
pub struct Variants<F: Form = MetaForm> {
variants: Vec<Variant<F>>,
}
impl<F: Form> Variants<F> {
/// Create a new [`VariantsBuilder`].
pub fn new() -> Self {
Self {
variants: Vec::new(),
}
}
/// Add a variant
pub fn variant<B>(mut self, name: F::String, builder: B) -> Self
where
B: Fn(VariantBuilder<F>) -> VariantBuilder<F, variant_state::IndexAssigned>,
{
let builder = builder(VariantBuilder::new(name));
self.variants.push(builder.finalize());
self
}
/// Add a unit variant (without fields).
pub fn variant_unit(mut self, name: F::String, index: u8) -> Self {
let builder = VariantBuilder::new(name).index(index);
self.variants.push(builder.finalize());
self
}
/// Construct a new [`TypeDefVariant`] from the initialized builder variants.
pub fn finalize(self) -> TypeDefVariant<F> {
TypeDefVariant::new(self.variants)
}
}
/// State types for the `VariantBuilder` which requires an index.
pub mod variant_state {
/// State where the builder has not assigned an index to a variant.
pub enum IndexNotAssigned {}
/// State where the builder has assigned an index to a variant.
pub enum IndexAssigned {}
}
/// Build a [`Variant`].
#[must_use]
pub struct VariantBuilder<F: Form, S = variant_state::IndexNotAssigned> {
name: F::String,
index: Option<u8>,
fields: Vec<Field<F>>,
discriminant: Option<u64>,
docs: Vec<F::String>,
marker: PhantomData<S>,
}
impl<F: Form> VariantBuilder<F, variant_state::IndexNotAssigned> {
/// Create a new [`VariantBuilder`].
pub fn new(name: F::String) -> Self {
Self {
name,
fields: Vec::new(),
discriminant: None,
index: None,
docs: Vec::new(),
marker: Default::default(),
}
}
/// Set the variant's codec index.
pub fn index(self, index: u8) -> VariantBuilder<F, variant_state::IndexAssigned> {
VariantBuilder {
name: self.name,
index: Some(index),
fields: self.fields,
discriminant: self.discriminant,
docs: self.docs,
marker: Default::default(),
}
}
}
impl<F: Form, S> VariantBuilder<F, S> {
/// Set the variant's discriminant.
pub fn discriminant(mut self, discriminant: u64) -> Self {
self.discriminant = Some(discriminant);
self
}
/// Initialize the variant's fields.
pub fn fields<T>(mut self, fields_builder: FieldsBuilder<F, T>) -> Self {
self.fields = fields_builder.finalize();
self
}
}
impl<S> VariantBuilder<PortableForm, S> {
#[cfg(feature = "docs")]
/// Initialize the variant's documentation (for types in portable form).
pub fn docs_portable<I>(mut self, docs: I) -> Self
where
I: IntoIterator<Item = <PortableForm as Form>::String>,
{
self.docs = docs.into_iter().collect();
self
}
}
impl<S> VariantBuilder<MetaForm, S> {
#[cfg(feature = "docs")]
/// Initialize the variant's documentation.
pub fn docs(mut self, docs: &[&'static str]) -> Self {
self.docs = docs.to_vec();
self
}
#[cfg(not(feature = "docs"))]
#[inline]
/// Doc capture is not enabled via the "docs" feature so this is a no-op.
pub fn docs(self, _docs: &[&'static str]) -> Self {
self
}
/// Initialize the variant's documentation, always captured even if the "docs" feature is not
/// enabled.
pub fn docs_always(mut self, docs: &[&'static str]) -> Self {
self.docs = docs.to_vec();
self
}
}
impl<F: Form> VariantBuilder<F, variant_state::IndexAssigned> {
/// Complete building and create final [`Variant`] instance.
pub fn finalize(self) -> Variant<F> {
Variant::new(
self.name,
self.fields,
self.index.expect("Index should be assigned by the builder"),
self.docs,
)
}
}