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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//! The Cosmwasm IDL (Interface Description Language)

use std::collections::BTreeMap;

use schemars::schema::RootSchema;
use thiserror::Error;

/// The version of the CosmWasm IDL.
///
/// Follows Semantic Versioning 2.0.0: <https://semver.org/>
// To determine if a change is breaking, assume consumers allow unknown fields and bump accordingly.
pub const IDL_VERSION: &str = "1.0.0";

/// Rust representation of a contract's API.
pub struct Api {
    pub contract_name: String,
    pub contract_version: String,
    pub instantiate: Option<RootSchema>,
    pub execute: Option<RootSchema>,
    pub query: Option<RootSchema>,
    pub migrate: Option<RootSchema>,
    pub sudo: Option<RootSchema>,
    /// A mapping of query variants to response types
    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(instantiate) = &mut json_api.instantiate {
            if let Some(metadata) = &mut 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
    }
}

/// A JSON representation of a contract's API.
#[derive(serde::Serialize)]
pub struct JsonApi {
    contract_name: String,
    contract_version: String,
    idl_version: String,
    instantiate: Option<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_schema_files(&self) -> Result<Vec<(String, String)>, EncodeError> {
        let mut result = Vec::new();

        if let Some(instantiate) = &self.instantiate {
            result.push((
                "instantiate.json".to_string(),
                serde_json::to_string_pretty(&instantiate)?,
            ));
        }

        if let Some(execute) = &self.execute {
            result.push((
                "execute.json".to_string(),
                serde_json::to_string_pretty(&execute)?,
            ));
        }
        if let Some(query) = &self.query {
            result.push((
                "query.json".to_string(),
                serde_json::to_string_pretty(&query)?,
            ));
        }
        if let Some(migrate) = &self.migrate {
            result.push((
                "migrate.json".to_string(),
                serde_json::to_string_pretty(&migrate)?,
            ));
        }
        if let Some(sudo) = &self.sudo {
            result.push((
                "sudo.json".to_string(),
                serde_json::to_string_pretty(&sudo)?,
            ));
        }
        if let Some(responses) = &self.responses {
            for (name, response) in responses {
                result.push((
                    format!("response_to_{name}.json"),
                    serde_json::to_string_pretty(&response)?,
                ));
            }
        }

        Ok(result)
    }

    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 crate::schema_for;

    use super::*;

    #[test]
    fn version_is_semver() {
        semver::Version::parse(IDL_VERSION).unwrap();
    }

    #[test]
    fn to_schema_files_works() {
        let empty = Api {
            contract_name: "my_contract".to_string(),
            contract_version: "1.2.3".to_string(),
            instantiate: None,
            execute: None,
            query: None,
            migrate: None,
            sudo: None,
            responses: None,
        };

        let files = empty.render().to_schema_files().unwrap();
        assert_eq!(files, []);

        #[derive(schemars::JsonSchema)]
        struct TestMsg {}

        let full = Api {
            contract_name: "my_contract".to_string(),
            contract_version: "1.2.3".to_string(),
            instantiate: Some(schema_for!(TestMsg)),
            execute: Some(schema_for!(TestMsg)),
            query: Some(schema_for!(TestMsg)),
            migrate: Some(schema_for!(TestMsg)),
            sudo: Some(schema_for!(TestMsg)),
            responses: Some(BTreeMap::from([(
                "TestMsg".to_string(),
                schema_for!(TestMsg),
            )])),
        };

        let files = full.render().to_schema_files().unwrap();
        assert_eq!(files.len(), 6);
        assert_eq!(files[0].0, "instantiate.json");
        assert_eq!(files[1].0, "execute.json");
        assert_eq!(files[2].0, "query.json");
        assert_eq!(files[3].0, "migrate.json");
        assert_eq!(files[4].0, "sudo.json");
        assert_eq!(files[5].0, "response_to_TestMsg.json");
    }
}