1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::collections::BTreeMap;
use schemars::schema::RootSchema;
use thiserror::Error;
pub const IDL_VERSION: &str = "1.0.0";
pub struct Api {
pub contract_name: String,
pub contract_version: String,
pub instantiate: RootSchema,
pub execute: Option<RootSchema>,
pub query: Option<RootSchema>,
pub migrate: Option<RootSchema>,
pub sudo: Option<RootSchema>,
pub responses: Option<BTreeMap<String, RootSchema>>,
}
impl Api {
pub fn render(self) -> JsonApi {
let mut json_api = JsonApi {
contract_name: self.contract_name,
contract_version: self.contract_version,
idl_version: IDL_VERSION.to_string(),
instantiate: self.instantiate,
execute: self.execute,
query: self.query,
migrate: self.migrate,
sudo: self.sudo,
responses: self.responses,
};
if let Some(metadata) = &mut json_api.instantiate.schema.metadata {
metadata.title = Some("InstantiateMsg".to_string());
}
if let Some(execute) = &mut json_api.execute {
if let Some(metadata) = &mut execute.schema.metadata {
metadata.title = Some("ExecuteMsg".to_string());
}
}
if let Some(query) = &mut json_api.query {
if let Some(metadata) = &mut query.schema.metadata {
metadata.title = Some("QueryMsg".to_string());
}
}
if let Some(migrate) = &mut json_api.migrate {
if let Some(metadata) = &mut migrate.schema.metadata {
metadata.title = Some("MigrateMsg".to_string());
}
}
if let Some(sudo) = &mut json_api.sudo {
if let Some(metadata) = &mut sudo.schema.metadata {
metadata.title = Some("SudoMsg".to_string());
}
}
json_api
}
}
#[derive(serde::Serialize)]
pub struct JsonApi {
contract_name: String,
contract_version: String,
idl_version: String,
instantiate: RootSchema,
execute: Option<RootSchema>,
query: Option<RootSchema>,
migrate: Option<RootSchema>,
sudo: Option<RootSchema>,
responses: Option<BTreeMap<String, RootSchema>>,
}
impl JsonApi {
pub fn to_string(&self) -> Result<String, EncodeError> {
serde_json::to_string_pretty(&self).map_err(Into::into)
}
pub fn to_writer(&self, writer: impl std::io::Write) -> Result<(), EncodeError> {
serde_json::to_writer_pretty(writer, self).map_err(Into::into)
}
}
#[derive(Error, Debug)]
pub enum EncodeError {
#[error("{0}")]
JsonError(#[from] serde_json::Error),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn version_is_semver() {
semver::Version::parse(IDL_VERSION).unwrap();
}
}