gestalt-sdk 0.0.1-alpha.26

Rust SDK scaffolding and generated protocol bindings for Gestalt executable providers
Documentation
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Code generated by sdkgen. DO NOT EDIT.

//! Generated native types and clients for cache.proto.

use crate::codec::cache::{
    from_wire_cache_delete_many_response, from_wire_cache_delete_response,
    from_wire_cache_get_many_response, from_wire_cache_get_response,
    from_wire_cache_touch_response, to_wire_cache_delete_many_request,
    to_wire_cache_delete_request, to_wire_cache_get_many_request, to_wire_cache_get_request,
    to_wire_cache_set_many_request, to_wire_cache_set_request, to_wire_cache_touch_request,
};
use crate::codec::host_service::{HostServiceChannel, connect_host_service, plain_channel};
use crate::generated::v1;
use crate::rpc_support::GestaltError;

/// CacheDeleteManyRequest removes multiple cache keys in one RPC.
///
/// Native message type for `gestalt.provider.v1.CacheDeleteManyRequest`.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CacheDeleteManyRequest {
    /// The `keys` field.
    pub keys: Vec<String>,
}

/// CacheDeleteManyResponse reports how many keys were deleted.
///
/// Native message type for `gestalt.provider.v1.CacheDeleteManyResponse`.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CacheDeleteManyResponse {
    /// The `deleted` field.
    pub deleted: i64,
}

/// CacheDeleteRequest removes one cache key.
///
/// Native message type for `gestalt.provider.v1.CacheDeleteRequest`.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CacheDeleteRequest {
    /// The `key` field.
    pub key: String,
}

/// CacheDeleteResponse reports whether one key existed and was deleted.
///
/// Native message type for `gestalt.provider.v1.CacheDeleteResponse`.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CacheDeleteResponse {
    /// The `deleted` field.
    pub deleted: bool,
}

/// CacheGetManyRequest loads multiple cache keys in one RPC.
///
/// Native message type for `gestalt.provider.v1.CacheGetManyRequest`.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CacheGetManyRequest {
    /// The `keys` field.
    pub keys: Vec<String>,
}

/// CacheGetManyResponse returns every lookup result for GetMany.
///
/// Native message type for `gestalt.provider.v1.CacheGetManyResponse`.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CacheGetManyResponse {
    /// The `entries` field.
    pub entries: Vec<CacheResult>,
}

/// CacheGetRequest loads one cache key.
///
/// Native message type for `gestalt.provider.v1.CacheGetRequest`.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CacheGetRequest {
    /// The `key` field.
    pub key: String,
}

/// CacheGetResponse is the result of looking up one cache key.
///
/// Native message type for `gestalt.provider.v1.CacheGetResponse`.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CacheGetResponse {
    /// The `found` field.
    pub found: bool,
    /// The `value` field.
    pub value: Vec<u8>,
}

/// CacheResult is one lookup result returned by GetMany.
///
/// Native message type for `gestalt.provider.v1.CacheResult`.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CacheResult {
    /// The `key` field.
    pub key: String,
    /// The `found` field.
    pub found: bool,
    /// The `value` field.
    pub value: Vec<u8>,
}

/// CacheSetEntry is one key/value pair written by SetMany.
///
/// Native message type for `gestalt.provider.v1.CacheSetEntry`.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CacheSetEntry {
    /// The `key` field.
    pub key: String,
    /// The `value` field.
    pub value: Vec<u8>,
}

/// CacheSetManyRequest writes multiple cache keys in one RPC.
///
/// Native message type for `gestalt.provider.v1.CacheSetManyRequest`.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CacheSetManyRequest {
    /// The `entries` field.
    pub entries: Vec<CacheSetEntry>,
    #[serde(with = "crate::serde_duration")]
    /// The `ttl` field; None when unset.
    pub ttl: Option<std::time::Duration>,
}

/// CacheSetRequest writes one cache key.
///
/// Native message type for `gestalt.provider.v1.CacheSetRequest`.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CacheSetRequest {
    /// The `key` field.
    pub key: String,
    /// The `value` field.
    pub value: Vec<u8>,
    /// ttl applies an optional expiration to the entry.
    ///
    #[serde(with = "crate::serde_duration")]
    /// The `ttl` field; None when unset.
    pub ttl: Option<std::time::Duration>,
}

/// CacheTouchRequest extends the TTL for one cache key.
///
/// Native message type for `gestalt.provider.v1.CacheTouchRequest`.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CacheTouchRequest {
    /// The `key` field.
    pub key: String,
    #[serde(with = "crate::serde_duration")]
    /// The `ttl` field; None when unset.
    pub ttl: Option<std::time::Duration>,
}

/// CacheTouchResponse reports whether a key existed and had its TTL updated.
///
/// Native message type for `gestalt.provider.v1.CacheTouchResponse`.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CacheTouchResponse {
    /// The `touched` field.
    pub touched: bool,
}

/// Cache models the shared Gestalt cache-provider protocol.
///
/// Client for the `gestalt.provider.v1.Cache` service.
pub struct Cache {
    inner: v1::cache_client::CacheClient<HostServiceChannel>,
    timeout: Option<std::time::Duration>,
}

impl Cache {
    /// Creates a client over an established channel.
    pub fn new(channel: tonic::transport::Channel) -> Self {
        Self {
            inner: v1::cache_client::CacheClient::new(plain_channel(channel)),
            timeout: None,
        }
    }

    /// Sets a deadline applied to every unary call; calls that run past it
    /// fail with DEADLINE_EXCEEDED. Streaming calls are unaffected.
    pub fn with_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Connects to the `cache` host service described by the environment.
    pub async fn connect() -> Result<Self, GestaltError> {
        Self::connect_named("").await
    }

    /// Connects to the named `cache` host-service binding.
    pub async fn connect_named(name: &str) -> Result<Self, GestaltError> {
        Ok(Self {
            inner: v1::cache_client::CacheClient::new(connect_host_service("cache", name).await?),
            timeout: None,
        })
    }

    /// Calls `gestalt.provider.v1.Cache.Get`.
    pub async fn get(&mut self, key: String) -> Result<Option<Vec<u8>>, GestaltError> {
        let request = CacheGetRequest { key };
        let mut tonic_request = tonic::Request::new(to_wire_cache_get_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response =
            from_wire_cache_get_response(self.inner.get(tonic_request).await?.into_inner());
        if !response.found {
            return Ok(None);
        }
        Ok(Some(response.value))
    }

    /// Calls `gestalt.provider.v1.Cache.Get` with the full request and response messages.
    pub async fn get_raw(
        &mut self,
        request: CacheGetRequest,
    ) -> Result<CacheGetResponse, GestaltError> {
        let mut tonic_request = tonic::Request::new(to_wire_cache_get_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response = self.inner.get(tonic_request).await?;
        Ok(from_wire_cache_get_response(response.into_inner()))
    }

    /// Calls `gestalt.provider.v1.Cache.GetMany`.
    pub async fn get_many(
        &mut self,
        keys: Vec<String>,
    ) -> Result<std::collections::BTreeMap<String, Vec<u8>>, GestaltError> {
        let request = CacheGetManyRequest { keys };
        let mut tonic_request = tonic::Request::new(to_wire_cache_get_many_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response = from_wire_cache_get_many_response(
            self.inner.get_many(tonic_request).await?.into_inner(),
        );
        let mut out = std::collections::BTreeMap::new();
        for entry in response.entries {
            if entry.found {
                out.insert(entry.key, entry.value);
            }
        }
        Ok(out)
    }

    /// Calls `gestalt.provider.v1.Cache.GetMany` with the full request and response messages.
    pub async fn get_many_raw(
        &mut self,
        request: CacheGetManyRequest,
    ) -> Result<CacheGetManyResponse, GestaltError> {
        let mut tonic_request = tonic::Request::new(to_wire_cache_get_many_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response = self.inner.get_many(tonic_request).await?;
        Ok(from_wire_cache_get_many_response(response.into_inner()))
    }

    /// Calls `gestalt.provider.v1.Cache.Set`.
    pub async fn set(
        &mut self,
        key: String,
        value: Vec<u8>,
        ttl: Option<std::time::Duration>,
    ) -> Result<(), GestaltError> {
        let request = CacheSetRequest { key, value, ttl };
        let mut tonic_request = tonic::Request::new(to_wire_cache_set_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        self.inner.set(tonic_request).await?;
        Ok(())
    }

    /// Calls `gestalt.provider.v1.Cache.Set` with the full request and response messages.
    pub async fn set_raw(&mut self, request: CacheSetRequest) -> Result<(), GestaltError> {
        let mut tonic_request = tonic::Request::new(to_wire_cache_set_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        self.inner.set(tonic_request).await?;
        Ok(())
    }

    /// Calls `gestalt.provider.v1.Cache.SetMany`.
    pub async fn set_many(
        &mut self,
        entries: Vec<CacheSetEntry>,
        ttl: Option<std::time::Duration>,
    ) -> Result<(), GestaltError> {
        let request = CacheSetManyRequest { entries, ttl };
        let mut tonic_request = tonic::Request::new(to_wire_cache_set_many_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        self.inner.set_many(tonic_request).await?;
        Ok(())
    }

    /// Calls `gestalt.provider.v1.Cache.SetMany` with the full request and response messages.
    pub async fn set_many_raw(&mut self, request: CacheSetManyRequest) -> Result<(), GestaltError> {
        let mut tonic_request = tonic::Request::new(to_wire_cache_set_many_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        self.inner.set_many(tonic_request).await?;
        Ok(())
    }

    /// Calls `gestalt.provider.v1.Cache.Delete`.
    pub async fn delete(&mut self, key: String) -> Result<bool, GestaltError> {
        let request = CacheDeleteRequest { key };
        let mut tonic_request = tonic::Request::new(to_wire_cache_delete_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response =
            from_wire_cache_delete_response(self.inner.delete(tonic_request).await?.into_inner());
        Ok(response.deleted)
    }

    /// Calls `gestalt.provider.v1.Cache.Delete` with the full request and response messages.
    pub async fn delete_raw(
        &mut self,
        request: CacheDeleteRequest,
    ) -> Result<CacheDeleteResponse, GestaltError> {
        let mut tonic_request = tonic::Request::new(to_wire_cache_delete_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response = self.inner.delete(tonic_request).await?;
        Ok(from_wire_cache_delete_response(response.into_inner()))
    }

    /// Calls `gestalt.provider.v1.Cache.DeleteMany`.
    pub async fn delete_many(&mut self, keys: Vec<String>) -> Result<i64, GestaltError> {
        let request = CacheDeleteManyRequest { keys };
        let mut tonic_request = tonic::Request::new(to_wire_cache_delete_many_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response = from_wire_cache_delete_many_response(
            self.inner.delete_many(tonic_request).await?.into_inner(),
        );
        Ok(response.deleted)
    }

    /// Calls `gestalt.provider.v1.Cache.DeleteMany` with the full request and response messages.
    pub async fn delete_many_raw(
        &mut self,
        request: CacheDeleteManyRequest,
    ) -> Result<CacheDeleteManyResponse, GestaltError> {
        let mut tonic_request = tonic::Request::new(to_wire_cache_delete_many_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response = self.inner.delete_many(tonic_request).await?;
        Ok(from_wire_cache_delete_many_response(response.into_inner()))
    }

    /// Calls `gestalt.provider.v1.Cache.Touch`.
    pub async fn touch(
        &mut self,
        key: String,
        ttl: Option<std::time::Duration>,
    ) -> Result<bool, GestaltError> {
        let request = CacheTouchRequest { key, ttl };
        let mut tonic_request = tonic::Request::new(to_wire_cache_touch_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response =
            from_wire_cache_touch_response(self.inner.touch(tonic_request).await?.into_inner());
        Ok(response.touched)
    }

    /// Calls `gestalt.provider.v1.Cache.Touch` with the full request and response messages.
    pub async fn touch_raw(
        &mut self,
        request: CacheTouchRequest,
    ) -> Result<CacheTouchResponse, GestaltError> {
        let mut tonic_request = tonic::Request::new(to_wire_cache_touch_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response = self.inner.touch(tonic_request).await?;
        Ok(from_wire_cache_touch_response(response.into_inner()))
    }
}