Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
14bbbe9
Capture docs for types, fields, and enum variants
ascjones Apr 26, 2021
4e696dc
Add docs to derive tests
ascjones Apr 26, 2021
55fc739
Fmt
ascjones Apr 26, 2021
ded2f3f
Fmt
ascjones Apr 27, 2021
3b78d47
Make clippy happy
ascjones Apr 27, 2021
c74422d
Fix struct docs tests
ascjones Apr 27, 2021
329d915
Fix struct docs tests
ascjones Apr 27, 2021
637ab5a
Add missing Vec import
ascjones Apr 27, 2021
efdd3e4
Fix test
ascjones Apr 27, 2021
77fc88a
Clear docs
ascjones Apr 27, 2021
912fbcb
Merge branch 'master' into aj-docs
ascjones May 25, 2021
4151589
Promote build to it's own mod, split fields and variant builders
ascjones May 25, 2021
a0743b0
Refactor variant construction with builder
ascjones May 25, 2021
fd6f893
Fix up derive and unit tests with Variant builder
ascjones May 25, 2021
52f5e83
Fmt
ascjones May 25, 2021
2d2ab5c
Condense build module back to single file
ascjones May 25, 2021
c4bf49d
Fix up doc tests
ascjones May 25, 2021
05d875b
Fix up README
ascjones May 25, 2021
790a6b8
Define initial field builder
ascjones May 26, 2021
0f3b795
Fix up field building
ascjones May 26, 2021
655279a
Fix tests
ascjones May 26, 2021
63b986a
Fully qualify calls to stringify
ascjones May 26, 2021
150ef4e
Default impl for FieldBuilder
ascjones May 26, 2021
43551f4
Fix up default impl
ascjones May 26, 2021
0decd01
Fix spelling errors
ascjones May 26, 2021
e031939
Remove clear_docs
ascjones May 26, 2021
fdd96c3
Fully qualify module_path
ascjones May 26, 2021
585a671
Update derive/src/lib.rs
ascjones May 26, 2021
a7179b5
Merge remote-tracking branch 'origin/aj-docs' into aj-docs
ascjones May 26, 2021
4d73ae4
Use callback for variant builder
ascjones May 26, 2021
59ff44c
Update README
ascjones May 26, 2021
e8ea40b
Remove leading space in doc comments
ascjones May 26, 2021
1c01b94
Satisfy clippy
ascjones May 26, 2021
0edec05
Add test for doc capture
ascjones May 27, 2021
fcc8985
Rename index back to discriminant, changes for this will be in https:…
ascjones May 27, 2021
6fb8042
Rename index back to discriminant, changes for this will be in https:…
ascjones May 27, 2021
882bdb4
Update src/build.rs
ascjones May 27, 2021
ebc3e4b
Update src/build.rs
ascjones May 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Promote build to it's own mod, split fields and variant builders
  • Loading branch information
ascjones committed May 25, 2021
commit 4151589ad76f13a0ea3c60547522797b01eea613
128 changes: 128 additions & 0 deletions src/build/fields.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright 2019-2021 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.

use crate::prelude::{
marker::PhantomData,
vec::Vec,
};

use crate::{
Field,
TypeInfo,
form::MetaForm,
};

/// 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 enum Fields {}

impl Fields {
/// The type construct has no fields
pub fn unit() -> FieldsBuilder<NoFields> {
FieldsBuilder::<NoFields>::default()
}

/// Fields for a type construct with named fields
pub fn named() -> FieldsBuilder<NamedFields> {
FieldsBuilder::default()
}

/// Fields for a type construct with unnamed fields
pub fn unnamed() -> FieldsBuilder<UnnamedFields> {
FieldsBuilder::default()
}
}

/// Build a set of either all named (e.g. for a struct) or all unnamed (e.g. for a tuple struct)
pub struct FieldsBuilder<T> {
fields: Vec<Field>,
marker: PhantomData<fn() -> T>,
}

impl<T> Default for FieldsBuilder<T> {
fn default() -> Self {
Self {
fields: Vec::new(),
marker: Default::default(),
}
}
}

impl<T> FieldsBuilder<T> {
/// Complete building and return the set of fields
pub fn finalize(self) -> Vec<Field<MetaForm>> {
self.fields
}
}

impl FieldsBuilder<NamedFields> {
/// Add a named field with the type of the type parameter `T`
pub fn field_of<T>(
mut self,
name: &'static str,
type_name: &'static str,
docs: &[&'static str],
) -> Self
where
T: TypeInfo + ?Sized + 'static,
{
self.fields
.push(Field::named_of::<T>(name, type_name, docs));
self
}

/// Add a named, [`Compact`] field of type `T`.
pub fn compact_of<T>(
mut self,
name: &'static str,
type_name: &'static str,
docs: &[&'static str],
) -> Self
where
T: scale::HasCompact,
<T as scale::HasCompact>::Type: TypeInfo + 'static,
{
self.fields
.push(Field::compact_of::<T>(Some(name), type_name, docs));
self
}
}

impl FieldsBuilder<UnnamedFields> {
/// Add an unnamed field with the type of the type parameter `T`
pub fn field_of<T>(mut self, type_name: &'static str, docs: &[&'static str]) -> Self
where
T: TypeInfo + ?Sized + 'static,
{
self.fields.push(Field::unnamed_of::<T>(type_name, docs));
self
}

/// Add an unnamed, [`Compact`] field of type `T`.
pub fn compact_of<T>(mut self, type_name: &'static str, docs: &[&'static str]) -> Self
where
T: scale::HasCompact,
<T as scale::HasCompact>::Type: TypeInfo + 'static,
{
self.fields
.push(Field::compact_of::<T>(None, type_name, docs));
self
}
}
192 changes: 6 additions & 186 deletions src/build.rs → src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,23 @@
//! }
//! ```

mod fields;
mod variant;

pub use self::fields::*;
pub use self::variant::*;

use crate::prelude::{
marker::PhantomData,
vec::Vec,
};

use crate::{
form::MetaForm,
Field,
MetaType,
Path,
Type,
TypeDef,
TypeDefComposite,
TypeDefVariant,
TypeInfo,
Variant,
};

/// State types for type builders which require a Path
Expand Down Expand Up @@ -209,184 +210,3 @@ impl<S> TypeBuilder<S> {
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 enum Fields {}

impl Fields {
/// The type construct has no fields
pub fn unit() -> FieldsBuilder<NoFields> {
FieldsBuilder::<NoFields>::default()
}

/// Fields for a type construct with named fields
pub fn named() -> FieldsBuilder<NamedFields> {
FieldsBuilder::default()
}

/// Fields for a type construct with unnamed fields
pub fn unnamed() -> FieldsBuilder<UnnamedFields> {
FieldsBuilder::default()
}
}

/// Build a set of either all named (e.g. for a struct) or all unnamed (e.g. for a tuple struct)
pub struct FieldsBuilder<T> {
fields: Vec<Field>,
marker: PhantomData<fn() -> T>,
}

impl<T> Default for FieldsBuilder<T> {
fn default() -> Self {
Self {
fields: Vec::new(),
marker: Default::default(),
}
}
}

impl<T> FieldsBuilder<T> {
/// Complete building and return the set of fields
pub fn finalize(self) -> Vec<Field<MetaForm>> {
self.fields
}
}

impl FieldsBuilder<NamedFields> {
/// Add a named field with the type of the type parameter `T`
pub fn field_of<T>(
mut self,
name: &'static str,
type_name: &'static str,
docs: &[&'static str],
) -> Self
where
T: TypeInfo + ?Sized + 'static,
{
self.fields
.push(Field::named_of::<T>(name, type_name, docs));
self
}

/// Add a named, [`Compact`] field of type `T`.
pub fn compact_of<T>(
mut self,
name: &'static str,
type_name: &'static str,
docs: &[&'static str],
) -> Self
where
T: scale::HasCompact,
<T as scale::HasCompact>::Type: TypeInfo + 'static,
{
self.fields
.push(Field::compact_of::<T>(Some(name), type_name, docs));
self
}
}

impl FieldsBuilder<UnnamedFields> {
/// Add an unnamed field with the type of the type parameter `T`
pub fn field_of<T>(mut self, type_name: &'static str, docs: &[&'static str]) -> Self
where
T: TypeInfo + ?Sized + 'static,
{
self.fields.push(Field::unnamed_of::<T>(type_name, docs));
self
}

/// Add an unnamed, [`Compact`] field of type `T`.
pub fn compact_of<T>(mut self, type_name: &'static str, docs: &[&'static str]) -> Self
where
T: scale::HasCompact,
<T as scale::HasCompact>::Type: TypeInfo + 'static,
{
self.fields
.push(Field::compact_of::<T>(None, type_name, docs));
self
}
}

/// Build a type with no variants.
pub enum NoVariants {}
/// Build a type where at least one variant has fields.
pub enum VariantFields {}
/// Build a type where *all* variants have no fields and the discriminant can
/// be directly chosen or accessed
pub enum Fieldless {}

/// Empty enum for VariantsBuilder constructors for the type builder DSL.
pub enum Variants {}

impl Variants {
/// Build a set of variants, at least one of which will have fields.
pub fn with_fields() -> VariantsBuilder<VariantFields> {
VariantsBuilder::new()
}

/// Build a set of variants, none of which will have fields, and the discriminant can
/// be directly chosen or accessed
pub fn fieldless() -> VariantsBuilder<Fieldless> {
VariantsBuilder::new()
}
}

/// Builds a definition of a variant type i.e an `enum`
#[derive(Default)]
pub struct VariantsBuilder<T> {
variants: Vec<Variant>,
marker: PhantomData<fn() -> T>,
}

impl VariantsBuilder<VariantFields> {
/// Add a variant with fields constructed by the supplied [`FieldsBuilder`](`crate::build::FieldsBuilder`)
pub fn variant<F>(
mut self,
name: &'static str,
fields: FieldsBuilder<F>,
docs: &[&'static str],
) -> Self {
self.variants
.push(Variant::with_fields(name, fields, docs.to_vec()));
self
}

/// Add a variant with no fields i.e. a unit variant
pub fn variant_unit(self, name: &'static str, docs: &[&'static str]) -> Self {
self.variant::<NoFields>(name, Fields::unit(), docs)
}
}

impl VariantsBuilder<Fieldless> {
/// Add a fieldless variant, explicitly setting the discriminant
pub fn variant(
mut self,
name: &'static str,
discriminant: u64,
docs: &[&'static str],
) -> Self {
self.variants
.push(Variant::with_discriminant(name, discriminant, docs));
self
}
}

impl<T> VariantsBuilder<T> {
fn new() -> Self {
VariantsBuilder {
variants: Vec::new(),
marker: Default::default(),
}
}

fn finalize(self) -> TypeDefVariant {
TypeDefVariant::new(self.variants)
}
}
Loading