pro_metadata/
lib.rs

1// Copyright 2018-2021 Parity Technologies (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![cfg_attr(not(feature = "std"), no_std)]
16
17#[cfg(not(feature = "std"))]
18extern crate alloc;
19
20#[cfg(test)]
21mod tests;
22
23pub mod layout;
24mod specs;
25mod utils;
26
27pub use self::specs::{
28    ConstructorSpec,
29    ConstructorSpecBuilder,
30    ContractSpec,
31    ContractSpecBuilder,
32    DisplayName,
33    EventParamSpec,
34    EventParamSpecBuilder,
35    EventSpec,
36    EventSpecBuilder,
37    MessageParamSpec,
38    MessageParamSpecBuilder,
39    MessageSpec,
40    MessageSpecBuilder,
41    ReturnTypeSpec,
42    Selector,
43    TypeSpec,
44};
45
46use impl_serde::serialize as serde_hex;
47
48#[cfg(feature = "derive")]
49use tetsy_scale_info::{
50    form::{
51        FormString,
52        PortableForm,
53    },
54    IntoPortable as _,
55    PortableRegistry,
56    Registry,
57};
58use serde::{
59    de::DeserializeOwned,
60    Deserialize,
61    Serialize,
62};
63
64/// An entire pro! project for metadata file generation purposes.
65#[derive(Debug, Serialize, Deserialize)]
66#[serde(bound(deserialize = "S: DeserializeOwned"))]
67pub struct ProProject<S: FormString = &'static str> {
68    #[serde(flatten)]
69    registry: PortableRegistry<S>,
70    #[serde(rename = "storage")]
71    /// The layout of the storage data structure
72    layout: layout::Layout<PortableForm<S>>,
73    spec: ContractSpec<PortableForm<S>>,
74}
75
76impl ProProject {
77    pub fn new<L, S>(layout: L, spec: S) -> Self
78    where
79        L: Into<layout::Layout>,
80        S: Into<ContractSpec>,
81    {
82        let mut registry = Registry::new();
83
84        Self {
85            layout: layout.into().into_portable(&mut registry),
86            spec: spec.into().into_portable(&mut registry),
87            registry: registry.into(),
88        }
89    }
90}
91
92impl<S> ProProject<S>
93where
94    S: FormString,
95{
96    /// Returns a read-only registry of types in the contract.
97    pub fn registry(&self) -> &PortableRegistry<S> {
98        &self.registry
99    }
100
101    /// Returns the storage layout of the contract.
102    pub fn layout(&self) -> &layout::Layout<PortableForm<S>> {
103        &self.layout
104    }
105
106    /// Returns the specification of the contract.
107    pub fn spec(&self) -> &ContractSpec<PortableForm<S>> {
108        &self.spec
109    }
110}