Skip to content
Merged
8 changes: 7 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,16 @@ jobs:
command: check
toolchain: stable
args: --manifest-path ./frame-metadata/Cargo.toml --no-default-features --features v13
- name: Checking v14
uses: actions-rs/cargo@master
with:
command: check
toolchain: stable
args: --manifest-path ./frame-metadata/Cargo.toml --no-default-features --features v14
- name: Checking all versions
uses: actions-rs/cargo@master
with:
command: check
toolchain: stable
args: --manifest-path ./frame-metadata/Cargo.toml --no-default-features --features v12,v13
args: --manifest-path ./frame-metadata/Cargo.toml --no-default-features --features v12,v13,v14

7 changes: 4 additions & 3 deletions frame-metadata/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "frame-metadata"
version = "12.0.0"
version = "14.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
license = "Apache-2.0"
Expand All @@ -19,9 +19,10 @@ scale-info = { version = "0.6.0", default-features = false, optional = true, fea
serde = { version = "1.0.101", optional = true, features = ["derive"] }

[features]
default = ["std", "v12"]
default = ["std", "v14"]
v12 = []
v13 = ["scale-info"]
v13 = []
v14 = ["scale-info"]
std = [
"codec/std",
"scale-info/std",
Expand Down
164 changes: 164 additions & 0 deletions frame-metadata/src/decode_different.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// This file is part of Substrate.

// Copyright (C) 2018-2020 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// 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 codec::{Encode, Output};

cfg_if::cfg_if! {
if #[cfg(feature = "std")] {
use codec::{Decode, Error, Input};
pub type StringBuf = String;
} else {
extern crate alloc;
use alloc::vec::Vec;

/// On `no_std` we do not support `Decode` and thus `StringBuf` is just `&'static str`.
/// So, if someone tries to decode this stuff on `no_std`, they will get a compilation error.
pub type StringBuf = &'static str;
}
}

/// A type that decodes to a different type than it encodes.
/// The user needs to make sure that both types use the same encoding.
///
/// For example a `&'static [ &'static str ]` can be decoded to a `Vec<String>`.
#[derive(Clone)]
pub enum DecodeDifferent<B, O>
where
B: 'static,
O: 'static,
{
Encode(B),
Decoded(O),
}

impl<B, O> Encode for DecodeDifferent<B, O>
where
B: Encode + 'static,
O: Encode + 'static,
{
fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
match self {
DecodeDifferent::Encode(b) => b.encode_to(dest),
DecodeDifferent::Decoded(o) => o.encode_to(dest),
}
}
}

impl<B, O> codec::EncodeLike for DecodeDifferent<B, O>
where
B: Encode + 'static,
O: Encode + 'static,
{
}

#[cfg(feature = "std")]
impl<B, O> Decode for DecodeDifferent<B, O>
where
B: 'static,
O: Decode + 'static,
{
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
<O>::decode(input).map(|val| DecodeDifferent::Decoded(val))
}
}

impl<B, O> PartialEq for DecodeDifferent<B, O>
where
B: Encode + Eq + PartialEq + 'static,
O: Encode + Eq + PartialEq + 'static,
{
fn eq(&self, other: &Self) -> bool {
self.encode() == other.encode()
}
}

impl<B, O> Eq for DecodeDifferent<B, O>
where
B: Encode + Eq + PartialEq + 'static,
O: Encode + Eq + PartialEq + 'static,
{
}

impl<B, O> core::fmt::Debug for DecodeDifferent<B, O>
where
B: core::fmt::Debug + Eq + 'static,
O: core::fmt::Debug + Eq + 'static,
{
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
DecodeDifferent::Encode(b) => b.fmt(f),
DecodeDifferent::Decoded(o) => o.fmt(f),
}
}
}

#[cfg(feature = "std")]
impl<B, O> serde::Serialize for DecodeDifferent<B, O>
where
B: serde::Serialize + 'static,
O: serde::Serialize + 'static,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
DecodeDifferent::Encode(b) => b.serialize(serializer),
DecodeDifferent::Decoded(o) => o.serialize(serializer),
}
}
}

pub type DecodeDifferentArray<B, O = B> = DecodeDifferent<&'static [B], Vec<O>>;

pub type DecodeDifferentStr = DecodeDifferent<&'static str, StringBuf>;

/// Newtype wrapper for support encoding functions (actual the result of the function).
#[derive(Clone, Eq)]
pub struct FnEncode<E>(pub fn() -> E)
where
E: Encode + 'static;

impl<E: Encode> Encode for FnEncode<E> {
fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
self.0().encode_to(dest);
}
}

impl<E: Encode> codec::EncodeLike for FnEncode<E> {}

impl<E: Encode + PartialEq> PartialEq for FnEncode<E> {
fn eq(&self, other: &Self) -> bool {
self.0().eq(&other.0())
}
}

impl<E: Encode + core::fmt::Debug> core::fmt::Debug for FnEncode<E> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
self.0().fmt(f)
}
}

#[cfg(feature = "std")]
impl<E: Encode + serde::Serialize> serde::Serialize for FnEncode<E> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0().serialize(serializer)
}
}
12 changes: 12 additions & 0 deletions frame-metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@ cfg_if::cfg_if! {

use codec::{Encode, Output};

#[cfg(any(feature = "v12", feature = "v13"))]
pub mod decode_different;

#[cfg(feature = "v12")]
pub mod v12;

#[cfg(feature = "v13")]
pub mod v13;

#[cfg(feature = "v14")]
pub mod v14;

/// Metadata prefixed by a u32 for reserved usage
#[derive(Eq, Encode, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
Expand Down Expand Up @@ -93,6 +99,12 @@ pub enum RuntimeMetadata {
/// Version 13 for runtime metadata, as raw encoded bytes.
#[cfg(not(feature = "v13"))]
V13(OpaqueMetadata),
/// Version 14 for runtime metadata.
#[cfg(feature = "v14")]
V14(v14::RuntimeMetadataV14),
/// Version 14 for runtime metadata, as raw encoded bytes.
#[cfg(not(feature = "v14"))]
V14(OpaqueMetadata),
}

/// Stores the encoded `RuntimeMetadata` as raw bytes.
Expand Down
Loading