ad4m_client/
languages.rs

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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use std::sync::Arc;

use crate::{util::query, ClientInfo};
use anyhow::{Context, Result};
use graphql_client::GraphQLQuery;
use serde::{Deserialize, Serialize};

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/languages.gql",
    response_derives = "Debug"
)]
pub struct ByFilter;

pub async fn by_filter(
    executor_url: String,
    cap_token: String,
    filter: String,
) -> Result<Vec<by_filter::ByFilterLanguages>> {
    let response_data: by_filter::ResponseData = query(
        executor_url,
        cap_token,
        ByFilter::build_query(by_filter::Variables { filter }),
    )
    .await
    .with_context(|| "Failed to run languages->all query")?;
    Ok(response_data.languages)
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/languages.gql",
    response_derives = "Debug"
)]
pub struct ByAddress;

pub async fn by_address(
    executor_url: String,
    cap_token: String,
    address: String,
) -> Result<Option<by_address::ByAddressLanguage>> {
    let response_data: by_address::ResponseData = query(
        executor_url,
        cap_token,
        ByAddress::build_query(by_address::Variables { address }),
    )
    .await
    .with_context(|| "Failed to run languages -> by-address query")?;
    Ok(response_data.language)
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/languages.gql",
    response_derives = "Debug"
)]
pub struct WriteSettings;

pub async fn write_settings(
    executor_url: String,
    cap_token: String,
    language_address: String,
    settings: String,
) -> Result<write_settings::ResponseData> {
    query(
        executor_url,
        cap_token,
        WriteSettings::build_query(write_settings::Variables {
            language_address,
            settings,
        }),
    )
    .await
    .with_context(|| "Failed to run languages -> write-settings query")
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/languages.gql",
    response_derives = "Debug"
)]
pub struct ApplyTemplateAndPublish;

pub async fn apply_template_and_publish(
    executor_url: String,
    cap_token: String,
    source: String,
    template_data: String,
) -> Result<apply_template_and_publish::ApplyTemplateAndPublishLanguageApplyTemplateAndPublish> {
    let response_data: apply_template_and_publish::ResponseData = query(
        executor_url,
        cap_token,
        ApplyTemplateAndPublish::build_query(apply_template_and_publish::Variables {
            source_language_hash: source,
            template_data,
        }),
    )
    .await
    .with_context(|| "Failed to run languages -> apply-template-and-publish")?;
    Ok(response_data.language_apply_template_and_publish)
}

#[derive(GraphQLQuery, Debug, Serialize, Deserialize)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/languages.gql",
    response_derives = "Debug"
)]
pub struct Meta;

pub async fn meta(
    executor_url: String,
    cap_token: String,
    address: String,
) -> Result<meta::MetaLanguageMeta> {
    let response_data: meta::ResponseData = query(
        executor_url,
        cap_token,
        Meta::build_query(meta::Variables { address }),
    )
    .await
    .with_context(|| "Failed to run languages -> meta")?;
    Ok(response_data.language_meta)
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/languages.gql",
    response_derives = "Debug"
)]
pub struct Publish;

pub async fn publish(
    executor_url: String,
    cap_token: String,
    language_path: String,
    name: String,
    description: Option<String>,
    possible_template_params: Option<Vec<String>>,
    source_code_link: Option<String>,
) -> Result<publish::PublishLanguagePublish> {
    let response_data: publish::ResponseData = query(
        executor_url,
        cap_token,
        Publish::build_query(publish::Variables {
            language_path,
            language_meta: publish::LanguageMetaInput {
                name,
                description,
                possible_template_params,
                source_code_link,
            },
        }),
    )
    .await
    .with_context(|| "Failed to run languages -> publish")?;
    Ok(response_data.language_publish)
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/languages.gql",
    response_derives = "Debug"
)]
pub struct Source;

pub async fn source(executor_url: String, cap_token: String, address: String) -> Result<String> {
    let response_data: source::ResponseData = query(
        executor_url,
        cap_token,
        Source::build_query(source::Variables { address }),
    )
    .await
    .with_context(|| "Failed to run languages -> source")?;
    Ok(response_data.language_source)
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/languages.gql",
    response_derives = "Debug"
)]
pub struct Remove;

pub async fn remove(executor_url: String, cap_token: String, address: String) -> Result<()> {
    query::<_, ()>(
        executor_url,
        cap_token,
        Remove::build_query(remove::Variables { address }),
    )
    .await
    .with_context(|| "Failed to run languages -> remove")?;
    Ok(())
}

pub struct LanguagesClient {
    info: Arc<ClientInfo>,
}

impl LanguagesClient {
    pub fn new(info: Arc<ClientInfo>) -> Self {
        Self { info }
    }

    pub async fn by_filter(
        &self,
        filter: Option<String>,
    ) -> Result<Vec<by_filter::ByFilterLanguages>> {
        by_filter(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            filter.unwrap_or_default(),
        )
        .await
    }

    pub async fn by_address(
        &self,
        address: String,
    ) -> Result<Option<by_address::ByAddressLanguage>> {
        by_address(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            address,
        )
        .await
    }

    pub async fn write_settings(
        &self,
        language_address: String,
        settings: String,
    ) -> Result<write_settings::ResponseData> {
        write_settings(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            language_address,
            settings,
        )
        .await
    }

    pub async fn apply_template_and_publish(
        &self,
        source: String,
        template_data: String,
    ) -> Result<apply_template_and_publish::ApplyTemplateAndPublishLanguageApplyTemplateAndPublish>
    {
        apply_template_and_publish(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            source,
            template_data,
        )
        .await
    }

    pub async fn meta(&self, address: String) -> Result<meta::MetaLanguageMeta> {
        meta(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            address,
        )
        .await
    }

    pub async fn publish(
        &self,
        language_path: String,
        name: String,
        description: Option<String>,
        possible_template_params: Option<Vec<String>>,
        source_code_link: Option<String>,
    ) -> Result<publish::PublishLanguagePublish> {
        publish(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            language_path,
            name,
            description,
            possible_template_params,
            source_code_link,
        )
        .await
    }

    pub async fn source(&self, address: String) -> Result<String> {
        source(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            address,
        )
        .await
    }

    pub async fn remove(&self, address: String) -> Result<()> {
        remove(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            address,
        )
        .await
    }
}