Skip to content

Commit

Permalink
sketch constructors for meta and portable form
Browse files Browse the repository at this point in the history
  • Loading branch information
xermicus committed Nov 12, 2022
1 parent ed1aeec commit a37af75
Showing 1 changed file with 50 additions and 18 deletions.
68 changes: 50 additions & 18 deletions crates/metadata/src/specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use alloc::{
vec::Vec,
};
use core::marker::PhantomData;
use ink_primitives::LangError;
use scale_info::{
form::{
Form,
Expand All @@ -33,6 +34,7 @@ use scale_info::{
},
meta_type,
IntoPortable,
PortableRegistryBuilder,
Registry,
TypeInfo,
};
Expand Down Expand Up @@ -349,6 +351,7 @@ pub struct ConstructorSpecBuilder<F: Form, Selector, IsPayable, Returns> {
impl<F> ConstructorSpec<F>
where
F: Form,
ReturnTypeSpec<F>: Default,
{
/// Creates a new constructor spec builder.
pub fn from_label(
Expand All @@ -365,7 +368,7 @@ where
selector: Selector::default(),
payable: Default::default(),
args: Vec::new(),
return_type: ReturnTypeSpec::new(None),
return_type: Default::default(),
docs: Vec::new(),
},
marker: PhantomData,
Expand Down Expand Up @@ -519,6 +522,7 @@ mod state {
impl<F> MessageSpec<F>
where
F: Form,
ReturnTypeSpec<F>: Default,
{
/// Creates a new message spec builder.
pub fn from_label(
Expand All @@ -537,7 +541,7 @@ where
mutates: false,
payable: false,
args: Vec::new(),
return_type: ReturnTypeSpec::new(None),
return_type: Default::default(),
docs: Vec::new(),
},
marker: PhantomData,
Expand Down Expand Up @@ -1214,45 +1218,73 @@ where
#[must_use]
pub struct ReturnTypeSpec<F: Form = MetaForm> {
#[serde(rename = "type")]
opt_type: Option<TypeSpec<F>>,
return_type: TypeSpec<F>,
}

impl Default for ReturnTypeSpec<MetaForm> {
fn default() -> Self {
Self {
return_type: TypeSpec::of_type::<Result<(), LangError>>(),
}
}
}

impl Default for ReturnTypeSpec<PortableForm> {
fn default() -> Self {
Self {
return_type: Default::default(),
}
}
}

impl IntoPortable for ReturnTypeSpec {
type Output = ReturnTypeSpec<PortableForm>;

fn into_portable(self, registry: &mut Registry) -> Self::Output {
ReturnTypeSpec {
opt_type: self
.opt_type
.map(|opt_type| opt_type.into_portable(registry)),
return_type: self.return_type.into_portable(registry),
}
}
}

impl<F> ReturnTypeSpec<F>
where
F: Form,
{
/// Creates a new return type specification from the given type or `None`.
impl ReturnTypeSpec {
/// Creates a new return type specification XXX: TODO update docs
///
/// # Examples
///
/// ```no_run
/// # use ink_metadata::{TypeSpec, ReturnTypeSpec};
/// <ReturnTypeSpec<scale_info::form::MetaForm>>::new(None); // no return type;
/// # use ink_primitives::LangError;
/// <ReturnTypeSpec<scale_info::form::MetaForm>>::new::<(), LangError>(None); // no return type;
/// ```
pub fn new<T>(ty: T) -> Self
pub fn new<O, E>() -> Self
where
T: Into<Option<TypeSpec<F>>>,
O: TypeInfo + 'static,
E: TypeInfo + 'static,
{
Self {
opt_type: ty.into(),
return_type: TypeSpec::of_type::<Result<O, E>>(),
}
}
}

/// Returns the optional return type
pub fn opt_type(&self) -> Option<&TypeSpec<F>> {
self.opt_type.as_ref()
impl ReturnTypeSpec<PortableForm> {
pub fn portable(
_ok: <PortableForm as Form>::Type,
_err: <PortableForm as Form>::Type,
_registry: &mut PortableRegistryBuilder,
) -> Self {
todo!()
}
}

impl<F> ReturnTypeSpec<F>
where
F: Form,
{
/// Returns the return type specification
pub fn return_type(&self) -> &TypeSpec<F> {
&self.return_type
}
}

Expand Down

0 comments on commit a37af75

Please sign in to comment.