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

Add version field to ink! metadata #1313

Merged
merged 11 commits into from
Aug 23, 2022
6 changes: 2 additions & 4 deletions crates/lang/codegen/src/generator/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ impl GenerateCode for Metadata<'_> {
#[cfg(not(feature = "ink-as-dependency"))]
const _: () = {
#[no_mangle]
pub fn __ink_generate_metadata() -> ::ink_metadata::MetadataVersioned {
<::ink_metadata::InkProject as ::core::convert::Into<::ink_metadata::MetadataVersioned>>::into(
::ink_metadata::InkProject::new(#layout, #contract)
)
pub fn __ink_generate_metadata() -> ::ink_metadata::InkProject {
::ink_metadata::InkProject::new(#layout, #contract)
}
};
}
Expand Down
43 changes: 19 additions & 24 deletions crates/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,38 +57,27 @@ use serde::{
Serialize,
};

/// Versioned ink! project metadata.
/// The metadata version of the generated ink! contract.
///
/// The serialized metadata format (which this represents) is different from the
/// version of this crate or the contract for Rust semantic versioning purposes.
///
/// # Note
///
/// Represents the version of the serialized metadata *format*, which is distinct from the version
/// of this crate for Rust semantic versioning compatibility.
#[derive(Debug, Serialize, Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum MetadataVersioned {
/// Version 0 placeholder. Represents the original non-versioned metadata format.
V0(MetadataVersionDeprecated),
/// Version 1 of the contract metadata.
V1(MetadataVersionDeprecated),
/// Version 2 of the contract metadata.
V2(MetadataVersionDeprecated),
/// Version 3 of the contract metadata.
V3(InkProject),
}

impl From<InkProject> for MetadataVersioned {
fn from(ink_project: InkProject) -> Self {
MetadataVersioned::V3(ink_project)
}
/// Versions other than the `Default` are considered deprecated. If you want to
/// deserialize legacy metadata versions you will need to use an old version of
/// this crate.
#[derive(Default, Debug, Serialize, Deserialize)]
pub enum MetadataVersion {
#[default]
HCastano marked this conversation as resolved.
Show resolved Hide resolved
#[serde(rename = "3")]
V3,
ascjones marked this conversation as resolved.
Show resolved Hide resolved
}

/// Enum to represent a deprecated metadata version that cannot be instantiated.
#[derive(Debug, Serialize, Deserialize)]
pub enum MetadataVersionDeprecated {}

/// An entire ink! project for metadata file generation purposes.
#[derive(Debug, Serialize, Deserialize)]
pub struct InkProject {
version: MetadataVersion,
#[serde(flatten)]
registry: PortableRegistry,
#[serde(rename = "storage")]
Expand All @@ -106,6 +95,7 @@ impl InkProject {
let mut registry = Registry::new();

Self {
version: Default::default(),
layout: layout.into().into_portable(&mut registry),
spec: spec.into().into_portable(&mut registry),
registry: registry.into(),
Expand All @@ -114,6 +104,11 @@ impl InkProject {
}

impl InkProject {
/// Returns the metadata version used by the contract.
pub fn version(&self) -> &MetadataVersion {
&self.version
}

/// Returns a read-only registry of types in the contract.
pub fn registry(&self) -> &PortableRegistry {
&self.registry
Expand Down