lib.rs 2.46 KB
Newer Older
1
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
Hero Bird's avatar
Hero Bird committed
2
//
3
4
5
// 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
Hero Bird's avatar
Hero Bird committed
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
Hero Bird's avatar
Hero Bird committed
8
//
9
10
11
12
13
// 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.
Hero Bird's avatar
Hero Bird committed
14
15
16
17
18
19

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;

Andrew Jones's avatar
Andrew Jones committed
20
21
22
#[cfg(test)]
mod tests;

23
pub mod layout;
Hero Bird's avatar
Hero Bird committed
24
mod specs;
25
mod utils;
Hero Bird's avatar
Hero Bird committed
26

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
pub use self::specs::{
    ConstructorSpec,
    ConstructorSpecBuilder,
    ContractSpec,
    ContractSpecBuilder,
    DisplayName,
    EventParamSpec,
    EventParamSpecBuilder,
    EventSpec,
    EventSpecBuilder,
    MessageParamSpec,
    MessageParamSpecBuilder,
    MessageSpec,
    MessageSpecBuilder,
    ReturnTypeSpec,
Andrew Jones's avatar
Andrew Jones committed
42
    Selector,
43
    TypeSpec,
Hero Bird's avatar
Hero Bird committed
44
};
45

Andrew Jones's avatar
Andrew Jones committed
46
47
use impl_serde::serialize as serde_hex;

48
#[cfg(feature = "derive")]
Andrew Jones's avatar
Andrew Jones committed
49
use scale_info::{
Hero Bird's avatar
Hero Bird committed
50
51
52
    form::CompactForm,
    IntoCompact as _,
    Registry,
Andrew Jones's avatar
Andrew Jones committed
53
54
55
56
57
    RegistryReadOnly,
};
use serde::{
    Deserialize,
    Serialize,
Hero Bird's avatar
Hero Bird committed
58
59
};

60
/// An entire ink! project for metadata file generation purposes.
Andrew Jones's avatar
Andrew Jones committed
61
#[derive(Debug, Serialize, Deserialize)]
Hero Bird's avatar
Hero Bird committed
62
pub struct InkProject {
63
    #[serde(flatten)]
Andrew Jones's avatar
Andrew Jones committed
64
    registry: RegistryReadOnly,
Hero Bird's avatar
Hero Bird committed
65
    #[serde(rename = "storage")]
66
    /// The layout of the storage data structure
67
    layout: layout::Layout<CompactForm>,
Hero Bird's avatar
Hero Bird committed
68
69
70
71
72
73
    spec: ContractSpec<CompactForm>,
}

impl InkProject {
    pub fn new<L, S>(layout: L, spec: S) -> Self
    where
74
        L: Into<layout::Layout>,
Hero Bird's avatar
Hero Bird committed
75
76
77
        S: Into<ContractSpec>,
    {
        let mut registry = Registry::new();
78

Hero Bird's avatar
Hero Bird committed
79
80
81
        Self {
            layout: layout.into().into_compact(&mut registry),
            spec: spec.into().into_compact(&mut registry),
Andrew Jones's avatar
Andrew Jones committed
82
            registry: registry.into(),
Hero Bird's avatar
Hero Bird committed
83
84
85
        }
    }
}
Andrew Jones's avatar
Andrew Jones committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

impl InkProject {
    /// Returns a read-only registry of types in the contract.
    pub fn registry(&self) -> &RegistryReadOnly {
        &self.registry
    }

    /// Returns the storage layout of the contract.
    pub fn layout(&self) -> &layout::Layout<CompactForm> {
        &self.layout
    }

    /// Returns the specification of the contract.
    pub fn spec(&self) -> &ContractSpec<CompactForm> {
        &self.spec
    }
}