ad4m_client/
languages.rs

1use std::sync::Arc;
2
3use crate::{util::query, ClientInfo};
4use anyhow::{Context, Result};
5use graphql_client::GraphQLQuery;
6use serde::{Deserialize, Serialize};
7
8#[derive(GraphQLQuery)]
9#[graphql(
10    schema_path = "schema.gql",
11    query_path = "src/languages.gql",
12    response_derives = "Debug"
13)]
14pub struct ByFilter;
15
16pub async fn by_filter(
17    executor_url: String,
18    cap_token: String,
19    filter: String,
20) -> Result<Vec<by_filter::ByFilterLanguages>> {
21    let response_data: by_filter::ResponseData = query(
22        executor_url,
23        cap_token,
24        ByFilter::build_query(by_filter::Variables { filter }),
25    )
26    .await
27    .with_context(|| "Failed to run languages->all query")?;
28    Ok(response_data.languages)
29}
30
31#[derive(GraphQLQuery)]
32#[graphql(
33    schema_path = "schema.gql",
34    query_path = "src/languages.gql",
35    response_derives = "Debug"
36)]
37pub struct ByAddress;
38
39pub async fn by_address(
40    executor_url: String,
41    cap_token: String,
42    address: String,
43) -> Result<Option<by_address::ByAddressLanguage>> {
44    let response_data: by_address::ResponseData = query(
45        executor_url,
46        cap_token,
47        ByAddress::build_query(by_address::Variables { address }),
48    )
49    .await
50    .with_context(|| "Failed to run languages -> by-address query")?;
51    Ok(response_data.language)
52}
53
54#[derive(GraphQLQuery)]
55#[graphql(
56    schema_path = "schema.gql",
57    query_path = "src/languages.gql",
58    response_derives = "Debug"
59)]
60pub struct WriteSettings;
61
62pub async fn write_settings(
63    executor_url: String,
64    cap_token: String,
65    language_address: String,
66    settings: String,
67) -> Result<write_settings::ResponseData> {
68    query(
69        executor_url,
70        cap_token,
71        WriteSettings::build_query(write_settings::Variables {
72            language_address,
73            settings,
74        }),
75    )
76    .await
77    .with_context(|| "Failed to run languages -> write-settings query")
78}
79
80#[derive(GraphQLQuery)]
81#[graphql(
82    schema_path = "schema.gql",
83    query_path = "src/languages.gql",
84    response_derives = "Debug"
85)]
86pub struct ApplyTemplateAndPublish;
87
88pub async fn apply_template_and_publish(
89    executor_url: String,
90    cap_token: String,
91    source: String,
92    template_data: String,
93) -> Result<apply_template_and_publish::ApplyTemplateAndPublishLanguageApplyTemplateAndPublish> {
94    let response_data: apply_template_and_publish::ResponseData = query(
95        executor_url,
96        cap_token,
97        ApplyTemplateAndPublish::build_query(apply_template_and_publish::Variables {
98            source_language_hash: source,
99            template_data,
100        }),
101    )
102    .await
103    .with_context(|| "Failed to run languages -> apply-template-and-publish")?;
104    Ok(response_data.language_apply_template_and_publish)
105}
106
107#[derive(GraphQLQuery, Debug, Serialize, Deserialize)]
108#[graphql(
109    schema_path = "schema.gql",
110    query_path = "src/languages.gql",
111    response_derives = "Debug"
112)]
113pub struct Meta;
114
115pub async fn meta(
116    executor_url: String,
117    cap_token: String,
118    address: String,
119) -> Result<meta::MetaLanguageMeta> {
120    let response_data: meta::ResponseData = query(
121        executor_url,
122        cap_token,
123        Meta::build_query(meta::Variables { address }),
124    )
125    .await
126    .with_context(|| "Failed to run languages -> meta")?;
127    Ok(response_data.language_meta)
128}
129
130#[derive(GraphQLQuery)]
131#[graphql(
132    schema_path = "schema.gql",
133    query_path = "src/languages.gql",
134    response_derives = "Debug"
135)]
136pub struct Publish;
137
138pub async fn publish(
139    executor_url: String,
140    cap_token: String,
141    language_path: String,
142    name: String,
143    description: Option<String>,
144    possible_template_params: Option<Vec<String>>,
145    source_code_link: Option<String>,
146) -> Result<publish::PublishLanguagePublish> {
147    let response_data: publish::ResponseData = query(
148        executor_url,
149        cap_token,
150        Publish::build_query(publish::Variables {
151            language_path,
152            language_meta: publish::LanguageMetaInput {
153                name,
154                description,
155                possible_template_params,
156                source_code_link,
157            },
158        }),
159    )
160    .await
161    .with_context(|| "Failed to run languages -> publish")?;
162    Ok(response_data.language_publish)
163}
164
165#[derive(GraphQLQuery)]
166#[graphql(
167    schema_path = "schema.gql",
168    query_path = "src/languages.gql",
169    response_derives = "Debug"
170)]
171pub struct Source;
172
173pub async fn source(executor_url: String, cap_token: String, address: String) -> Result<String> {
174    let response_data: source::ResponseData = query(
175        executor_url,
176        cap_token,
177        Source::build_query(source::Variables { address }),
178    )
179    .await
180    .with_context(|| "Failed to run languages -> source")?;
181    Ok(response_data.language_source)
182}
183
184#[derive(GraphQLQuery)]
185#[graphql(
186    schema_path = "schema.gql",
187    query_path = "src/languages.gql",
188    response_derives = "Debug"
189)]
190pub struct Remove;
191
192pub async fn remove(executor_url: String, cap_token: String, address: String) -> Result<()> {
193    query::<_, ()>(
194        executor_url,
195        cap_token,
196        Remove::build_query(remove::Variables { address }),
197    )
198    .await
199    .with_context(|| "Failed to run languages -> remove")?;
200    Ok(())
201}
202
203pub struct LanguagesClient {
204    info: Arc<ClientInfo>,
205}
206
207impl LanguagesClient {
208    pub fn new(info: Arc<ClientInfo>) -> Self {
209        Self { info }
210    }
211
212    pub async fn by_filter(
213        &self,
214        filter: Option<String>,
215    ) -> Result<Vec<by_filter::ByFilterLanguages>> {
216        by_filter(
217            self.info.executor_url.clone(),
218            self.info.cap_token.clone(),
219            filter.unwrap_or_default(),
220        )
221        .await
222    }
223
224    pub async fn by_address(
225        &self,
226        address: String,
227    ) -> Result<Option<by_address::ByAddressLanguage>> {
228        by_address(
229            self.info.executor_url.clone(),
230            self.info.cap_token.clone(),
231            address,
232        )
233        .await
234    }
235
236    pub async fn write_settings(
237        &self,
238        language_address: String,
239        settings: String,
240    ) -> Result<write_settings::ResponseData> {
241        write_settings(
242            self.info.executor_url.clone(),
243            self.info.cap_token.clone(),
244            language_address,
245            settings,
246        )
247        .await
248    }
249
250    pub async fn apply_template_and_publish(
251        &self,
252        source: String,
253        template_data: String,
254    ) -> Result<apply_template_and_publish::ApplyTemplateAndPublishLanguageApplyTemplateAndPublish>
255    {
256        apply_template_and_publish(
257            self.info.executor_url.clone(),
258            self.info.cap_token.clone(),
259            source,
260            template_data,
261        )
262        .await
263    }
264
265    pub async fn meta(&self, address: String) -> Result<meta::MetaLanguageMeta> {
266        meta(
267            self.info.executor_url.clone(),
268            self.info.cap_token.clone(),
269            address,
270        )
271        .await
272    }
273
274    pub async fn publish(
275        &self,
276        language_path: String,
277        name: String,
278        description: Option<String>,
279        possible_template_params: Option<Vec<String>>,
280        source_code_link: Option<String>,
281    ) -> Result<publish::PublishLanguagePublish> {
282        publish(
283            self.info.executor_url.clone(),
284            self.info.cap_token.clone(),
285            language_path,
286            name,
287            description,
288            possible_template_params,
289            source_code_link,
290        )
291        .await
292    }
293
294    pub async fn source(&self, address: String) -> Result<String> {
295        source(
296            self.info.executor_url.clone(),
297            self.info.cap_token.clone(),
298            address,
299        )
300        .await
301    }
302
303    pub async fn remove(&self, address: String) -> Result<()> {
304        remove(
305            self.info.executor_url.clone(),
306            self.info.cap_token.clone(),
307            address,
308        )
309        .await
310    }
311}