Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable ink_metadata to be used for metadata generation outside of ink! #1357

Merged
merged 26 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
98fc658
switch API to support string, expose more generic constructors and bu…
xermicus Sep 8, 2022
29f1740
this is not needed
xermicus Sep 8, 2022
defcbc1
Merge branch 'master' into cl/runtime-metadata
xermicus Sep 19, 2022
85ce4f7
Merge branch 'master' into cl/runtime-metadata
xermicus Sep 22, 2022
c980a89
patch scale-info dep
xermicus Sep 22, 2022
e44b706
clippy happy
xermicus Sep 22, 2022
98b43e1
Merge branch 'master' into cl/runtime-metadata
xermicus Sep 29, 2022
5427347
patch scale-info dep to current dev branch
xermicus Sep 29, 2022
fec263b
WIP adapting for changes in scale-info
xermicus Oct 5, 2022
494d9a3
clippy
xermicus Oct 5, 2022
61b82f3
point to scale-info master
xermicus Oct 11, 2022
98d093b
Merge branch 'master' into cl/runtime-metadata
xermicus Oct 11, 2022
9553e49
more generic root layout constructor
xermicus Oct 12, 2022
5a5f44c
Merge branch 'master' into cl/runtime-metadata
xermicus Oct 27, 2022
d936040
test future version of scale-info
xermicus Oct 27, 2022
df2baee
nigthly fmt
xermicus Oct 27, 2022
a4fe8f5
make spec fields private again
xermicus Oct 27, 2022
b480e4d
Merge branch 'master' into cl/runtime-metadata
xermicus Oct 27, 2022
aae6fa4
new_custom for ReturnTypeSpec
xermicus Oct 27, 2022
d10f31d
Merge branch 'master' into cl/runtime-metadata
xermicus Oct 31, 2022
f7556d6
yay scale-info release
xermicus Oct 31, 2022
dd2f2d7
tidy up some constructors
xermicus Nov 1, 2022
d385e16
getting rid of DocString trait
xermicus Nov 1, 2022
8ca76e9
generic type specs constructors
xermicus Nov 2, 2022
bd2f379
TypeSpec::new renamed to TypeSpec::of_type
xermicus Nov 2, 2022
c658c46
Merge branch 'master' into cl/runtime-metadata
xermicus Nov 2, 2022
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
78 changes: 42 additions & 36 deletions crates/metadata/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ impl<'a> From<&'a Key> for LayoutKey {
}

impl LayoutKey {
/// Construct a custom layout key.
pub fn new<T>(key: T) -> Self
where
T: Into<u32>,
{
Self { key: key.into() }
}

/// Returns the key of the layout key.
pub fn key(&self) -> &Key {
&self.key
Expand All @@ -125,19 +133,6 @@ pub struct RootLayout<F: Form = MetaForm> {
layout: Box<Layout<F>>,
}

impl RootLayout {
/// Creates a new root layout.
pub fn new<L>(root_key: LayoutKey, layout: L) -> Self
where
L: Into<Layout>,
{
Self {
root_key,
layout: Box::new(layout.into()),
}
}
}

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

Expand All @@ -153,6 +148,17 @@ impl<F> RootLayout<F>
where
F: Form,
{
/// Creates a new root layout.
pub fn new<L>(root_key: LayoutKey, layout: L) -> Self
where
L: Into<Layout<F>>,
{
Self {
root_key,
layout: Box::new(layout.into()),
}
}

/// Returns the root key of the sub-tree.
pub fn root_key(&self) -> &LayoutKey {
&self.root_key
Expand All @@ -179,7 +185,7 @@ pub struct LeafLayout<F: Form = MetaForm> {

impl LeafLayout {
/// Creates a new cell layout.
pub fn new<T>(key: LayoutKey) -> Self
pub fn from_key<T>(key: LayoutKey) -> Self
where
T: TypeInfo + 'static,
{
Expand Down Expand Up @@ -241,6 +247,10 @@ where
pub fn ty(&self) -> &F::Type {
&self.ty
}

pub fn new(key: LayoutKey, ty: <F as Form>::Type) -> Self {
Self { key, ty }
}
}

/// A hashing layout potentially hitting all cells of the storage.
Expand Down Expand Up @@ -448,24 +458,22 @@ pub struct StructLayout<F: Form = MetaForm> {
fields: Vec<FieldLayout<F>>,
}

impl StructLayout {
impl<F> StructLayout<F>
where
F: Form,
{
/// Creates a new struct layout.
pub fn new<N, F>(name: N, fields: F) -> Self
pub fn new<N, T>(name: N, fields: T) -> Self
where
N: Into<&'static str>,
F: IntoIterator<Item = FieldLayout>,
N: Into<F::String>,
T: IntoIterator<Item = FieldLayout<F>>,
{
Self {
name: name.into(),
fields: fields.into_iter().collect(),
}
}
}

impl<F> StructLayout<F>
where
F: Form,
{
/// Returns the name of the struct.
pub fn name(&self) -> &F::String {
&self.name
Expand All @@ -481,7 +489,7 @@ impl IntoPortable for StructLayout {

fn into_portable(self, registry: &mut Registry) -> Self::Output {
StructLayout {
name: self.name.into(),
name: self.name.to_string(),
fields: self
.fields
.into_iter()
Expand All @@ -507,24 +515,22 @@ pub struct FieldLayout<F: Form = MetaForm> {
layout: Layout<F>,
}

impl FieldLayout {
/// Creates a new field layout.
impl<F> FieldLayout<F>
where
F: Form,
{
/// Creates a new custom field layout.
pub fn new<N, L>(name: N, layout: L) -> Self
where
N: Into<&'static str>,
L: Into<Layout>,
N: Into<F::String>,
L: Into<Layout<F>>,
{
Self {
name: name.into(),
layout: layout.into(),
}
}
}

impl<F> FieldLayout<F>
where
F: Form,
{
/// Returns the name of the field.
pub fn name(&self) -> &F::String {
&self.name
Expand All @@ -544,7 +550,7 @@ impl IntoPortable for FieldLayout {

fn into_portable(self, registry: &mut Registry) -> Self::Output {
FieldLayout {
name: self.name.into_portable(registry),
name: self.name.to_string(),
layout: self.layout.into_portable(registry),
}
}
Expand Down Expand Up @@ -587,7 +593,7 @@ impl EnumLayout {
/// Creates a new enum layout.
pub fn new<N, K, V>(name: N, dispatch_key: K, variants: V) -> Self
where
N: Into<&'static str>,
N: Into<<MetaForm as Form>::String>,
K: Into<LayoutKey>,
V: IntoIterator<Item = (Discriminant, StructLayout)>,
{
Expand Down Expand Up @@ -624,7 +630,7 @@ impl IntoPortable for EnumLayout {

fn into_portable(self, registry: &mut Registry) -> Self::Output {
EnumLayout {
name: self.name.into(),
name: self.name.to_string(),
dispatch_key: self.dispatch_key,
variants: self
.variants
Expand Down
18 changes: 9 additions & 9 deletions crates/metadata/src/layout/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ fn named_fields_struct_layout(key: &Key) -> Layout {
StructLayout::new(
"Struct",
vec![
FieldLayout::new("a", LeafLayout::new::<i32>(LayoutKey::from(key))),
FieldLayout::new("b", LeafLayout::new::<i64>(LayoutKey::from(key))),
FieldLayout::new("a", LeafLayout::from_key::<i32>(LayoutKey::from(key))),
FieldLayout::new("b", LeafLayout::from_key::<i64>(LayoutKey::from(key))),
],
)
.into()
Expand Down Expand Up @@ -73,8 +73,8 @@ fn tuple_struct_layout(key: &Key) -> Layout {
StructLayout::new(
"(A, B)",
vec![
FieldLayout::new("0", LeafLayout::new::<i32>(LayoutKey::from(key))),
FieldLayout::new("1", LeafLayout::new::<i64>(LayoutKey::from(key))),
FieldLayout::new("0", LeafLayout::from_key::<i32>(LayoutKey::from(key))),
FieldLayout::new("1", LeafLayout::from_key::<i64>(LayoutKey::from(key))),
],
)
.into()
Expand Down Expand Up @@ -175,11 +175,11 @@ fn mixed_enum_layout(key: &Key) -> Layout {
vec![
FieldLayout::new(
"0",
LeafLayout::new::<i32>(LayoutKey::from(variant_key)),
LeafLayout::from_key::<i32>(LayoutKey::from(variant_key)),
),
FieldLayout::new(
"1",
LeafLayout::new::<i64>(LayoutKey::from(variant_key)),
LeafLayout::from_key::<i64>(LayoutKey::from(variant_key)),
),
],
),
Expand All @@ -194,11 +194,11 @@ fn mixed_enum_layout(key: &Key) -> Layout {
vec![
FieldLayout::new(
"a",
LeafLayout::new::<i32>(LayoutKey::from(variant_key)),
LeafLayout::from_key::<i32>(LayoutKey::from(variant_key)),
),
FieldLayout::new(
"b",
LeafLayout::new::<i64>(LayoutKey::from(variant_key)),
LeafLayout::from_key::<i64>(LayoutKey::from(variant_key)),
),
],
),
Expand Down Expand Up @@ -287,7 +287,7 @@ fn unbounded_hashing_layout(key: &Key) -> Layout {
b"ink storage hashmap".to_vec(),
Vec::new(),
),
LeafLayout::new::<(i32, bool)>(LayoutKey::from(root_key)),
LeafLayout::from_key::<(i32, bool)>(LayoutKey::from(root_key)),
)
.into()
}
Expand Down
14 changes: 7 additions & 7 deletions crates/metadata/src/layout/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ mod tests {
0.into(),
RootLayout::new(
1.into(),
RootLayout::new(2.into(), LeafLayout::new::<u32>(2.into())),
RootLayout::new(2.into(), LeafLayout::from_key::<u32>(2.into())),
),
);

Expand Down Expand Up @@ -159,7 +159,7 @@ mod tests {
StructLayout::new(
"Struct0",
vec![
FieldLayout::new("d", LeafLayout::new::<u128>(root_0)),
FieldLayout::new("d", LeafLayout::from_key::<u128>(root_0)),
FieldLayout::new(
"f",
RootLayout::new(
Expand All @@ -180,7 +180,7 @@ mod tests {
"g",
RootLayout::new(
root_4,
LeafLayout::new::<
LeafLayout::from_key::<
BTreeSet<u64>,
>(
root_4
Expand All @@ -197,7 +197,7 @@ mod tests {
"Second",
vec![FieldLayout::new(
"0",
LeafLayout::new::<u8>(root_2),
LeafLayout::from_key::<u8>(root_2),
)],
),
),
Expand All @@ -209,7 +209,7 @@ mod tests {
"0",
RootLayout::new(
root_3,
LeafLayout::new::<String>(
LeafLayout::from_key::<String>(
root_3,
),
),
Expand All @@ -223,10 +223,10 @@ mod tests {
],
),
),
FieldLayout::new("b", LeafLayout::new::<u32>(root_0)),
FieldLayout::new("b", LeafLayout::from_key::<u32>(root_0)),
FieldLayout::new(
"c",
RootLayout::new(root_1, LeafLayout::new::<Vec<u8>>(root_1)),
RootLayout::new(root_1, LeafLayout::from_key::<Vec<u8>>(root_1)),
),
],
),
Expand Down
19 changes: 17 additions & 2 deletions crates/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub struct InkProject {
}

impl InkProject {
/// Create a new ink! project from a layout and a spec.
pub fn new<L, S>(layout: L, spec: S) -> Self
where
L: Into<layout::Layout>,
Expand All @@ -110,9 +111,23 @@ impl InkProject {
registry: registry.into(),
}
}
}

impl InkProject {
/// Create a new portable ink! project.
///
/// The caller is responsible to register all types into the supplied registry.
pub fn new_portable(
layout: layout::Layout<PortableForm>,
spec: ContractSpec<PortableForm>,
registry: PortableRegistry,
) -> Self {
Self {
version: Default::default(),
layout,
spec,
registry,
}
}

/// Returns the metadata version used by the contract.
pub fn version(&self) -> &MetadataVersion {
&self.version
Expand Down
Loading