fredis/commands/interfaces/
lua.rs1use crate::{
2 commands,
3 error::Error,
4 interfaces::{ClientLike, FredResult},
5 types::{
6 FromValue,
7 MultipleKeys,
8 MultipleStrings,
9 MultipleValues,
10 scripts::{FnPolicy, ScriptDebugFlag},
11 },
12};
13use bytes::Bytes;
14use bytes_utils::Str;
15use fred_macros::rm_send_if;
16use futures::Future;
17use std::convert::TryInto;
18
19#[rm_send_if(any(feature = "glommio", feature = "cloudflare"))]
21pub trait LuaInterface: ClientLike + Sized {
22 fn script_load<R, S>(&self, script: S) -> impl Future<Output = FredResult<R>> + Send
29 where
30 R: FromValue,
31 S: Into<Str> + Send,
32 {
33 async move {
34 into!(script);
35 commands::lua::script_load(self, script).await?.convert()
36 }
37 }
38
39 #[cfg(feature = "sha-1")]
43 #[cfg_attr(docsrs, doc(cfg(feature = "sha-1")))]
44 fn script_load_cluster<R, S>(&self, script: S) -> impl Future<Output = FredResult<R>> + Send
45 where
46 R: FromValue,
47 S: Into<Str> + Send,
48 {
49 async move {
50 into!(script);
51 commands::lua::script_load_cluster(self, script).await?.convert()
52 }
53 }
54
55 fn script_kill(&self) -> impl Future<Output = FredResult<()>> + Send {
59 async move { commands::lua::script_kill(self).await }
60 }
61
62 fn script_kill_cluster(&self) -> impl Future<Output = FredResult<()>> + Send {
65 async move { commands::lua::script_kill_cluster(self).await }
66 }
67
68 fn script_flush(&self, r#async: bool) -> impl Future<Output = FredResult<()>> + Send {
72 async move { commands::lua::script_flush(self, r#async).await }
73 }
74
75 fn script_flush_cluster(&self, r#async: bool) -> impl Future<Output = FredResult<()>> + Send {
78 async move { commands::lua::script_flush_cluster(self, r#async).await }
79 }
80
81 fn script_exists<R, H>(&self, hashes: H) -> impl Future<Output = FredResult<R>> + Send
85 where
86 R: FromValue,
87 H: Into<MultipleStrings> + Send,
88 {
89 async move {
90 into!(hashes);
91 commands::lua::script_exists(self, hashes).await?.convert()
92 }
93 }
94
95 fn script_debug(&self, flag: ScriptDebugFlag) -> impl Future<Output = FredResult<()>> + Send {
99 async move { commands::lua::script_debug(self, flag).await }
100 }
101
102 fn evalsha<R, S, K, V>(&self, hash: S, keys: K, args: V) -> impl Future<Output = FredResult<R>> + Send
108 where
109 R: FromValue,
110 S: Into<Str> + Send,
111 K: Into<MultipleKeys> + Send,
112 V: TryInto<MultipleValues> + Send,
113 V::Error: Into<Error> + Send,
114 {
115 async move {
116 into!(hash, keys);
117 try_into!(args);
118 commands::lua::evalsha(self, hash, keys, args).await?.convert()
119 }
120 }
121
122 fn eval<R, S, K, V>(&self, script: S, keys: K, args: V) -> impl Future<Output = FredResult<R>> + Send
128 where
129 R: FromValue,
130 S: Into<Str> + Send,
131 K: Into<MultipleKeys> + Send,
132 V: TryInto<MultipleValues> + Send,
133 V::Error: Into<Error> + Send,
134 {
135 async move {
136 into!(script, keys);
137 try_into!(args);
138 commands::lua::eval(self, script, keys, args).await?.convert()
139 }
140 }
141}
142
143#[rm_send_if(any(feature = "glommio", feature = "cloudflare"))]
145pub trait FunctionInterface: ClientLike + Sized {
146 fn fcall<R, F, K, V>(&self, func: F, keys: K, args: V) -> impl Future<Output = FredResult<R>> + Send
150 where
151 R: FromValue,
152 F: Into<Str> + Send,
153 K: Into<MultipleKeys> + Send,
154 V: TryInto<MultipleValues> + Send,
155 V::Error: Into<Error> + Send,
156 {
157 async move {
158 into!(func);
159 try_into!(keys, args);
160 commands::lua::fcall(self, func, keys, args).await?.convert()
161 }
162 }
163
164 fn fcall_ro<R, F, K, V>(&self, func: F, keys: K, args: V) -> impl Future<Output = FredResult<R>> + Send
168 where
169 R: FromValue,
170 F: Into<Str> + Send,
171 K: Into<MultipleKeys> + Send,
172 V: TryInto<MultipleValues> + Send,
173 V::Error: Into<Error> + Send,
174 {
175 async move {
176 into!(func);
177 try_into!(keys, args);
178 commands::lua::fcall_ro(self, func, keys, args).await?.convert()
179 }
180 }
181
182 fn function_delete<R, S>(&self, library_name: S) -> impl Future<Output = FredResult<R>> + Send
186 where
187 R: FromValue,
188 S: Into<Str> + Send,
189 {
190 async move {
191 into!(library_name);
192 commands::lua::function_delete(self, library_name).await?.convert()
193 }
194 }
195
196 fn function_delete_cluster<S>(&self, library_name: S) -> impl Future<Output = FredResult<()>> + Send
200 where
201 S: Into<Str> + Send,
202 {
203 async move {
204 into!(library_name);
205 commands::lua::function_delete_cluster(self, library_name).await
206 }
207 }
208
209 fn function_dump<R>(&self) -> impl Future<Output = FredResult<R>> + Send
213 where
214 R: FromValue,
215 {
216 async move { commands::lua::function_dump(self).await?.convert() }
217 }
218
219 fn function_flush<R>(&self, r#async: bool) -> impl Future<Output = FredResult<R>> + Send
223 where
224 R: FromValue,
225 {
226 async move { commands::lua::function_flush(self, r#async).await?.convert() }
227 }
228
229 fn function_flush_cluster(&self, r#async: bool) -> impl Future<Output = FredResult<()>> + Send {
233 async move { commands::lua::function_flush_cluster(self, r#async).await }
234 }
235
236 fn function_kill<R>(&self) -> impl Future<Output = FredResult<R>> + Send
243 where
244 R: FromValue,
245 {
246 async move { commands::lua::function_kill(self).await?.convert() }
247 }
248
249 fn function_list<R, S>(&self, library_name: Option<S>, withcode: bool) -> impl Future<Output = FredResult<R>> + Send
253 where
254 R: FromValue,
255 S: Into<Str> + Send,
256 {
257 async move {
258 let library_name = library_name.map(|l| l.into());
259 commands::lua::function_list(self, library_name, withcode)
260 .await?
261 .convert()
262 }
263 }
264
265 fn function_load<R, S>(&self, replace: bool, code: S) -> impl Future<Output = FredResult<R>> + Send
269 where
270 R: FromValue,
271 S: Into<Str> + Send,
272 {
273 async move {
274 into!(code);
275 commands::lua::function_load(self, replace, code).await?.convert()
276 }
277 }
278
279 fn function_load_cluster<R, S>(&self, replace: bool, code: S) -> impl Future<Output = FredResult<R>> + Send
283 where
284 R: FromValue,
285 S: Into<Str> + Send,
286 {
287 async move {
288 into!(code);
289 commands::lua::function_load_cluster(self, replace, code)
290 .await?
291 .convert()
292 }
293 }
294
295 fn function_restore<R, B, P>(&self, serialized: B, policy: P) -> impl Future<Output = FredResult<R>> + Send
301 where
302 R: FromValue,
303 B: Into<Bytes> + Send,
304 P: TryInto<FnPolicy> + Send,
305 P::Error: Into<Error> + Send,
306 {
307 async move {
308 into!(serialized);
309 try_into!(policy);
310 commands::lua::function_restore(self, serialized, policy)
311 .await?
312 .convert()
313 }
314 }
315
316 fn function_restore_cluster<B, P>(&self, serialized: B, policy: P) -> impl Future<Output = FredResult<()>> + Send
322 where
323 B: Into<Bytes> + Send,
324 P: TryInto<FnPolicy> + Send,
325 P::Error: Into<Error> + Send,
326 {
327 async move {
328 into!(serialized);
329 try_into!(policy);
330 commands::lua::function_restore_cluster(self, serialized, policy).await
331 }
332 }
333
334 fn function_stats<R>(&self) -> impl Future<Output = FredResult<R>> + Send
341 where
342 R: FromValue,
343 {
344 async move { commands::lua::function_stats(self).await?.convert() }
345 }
346}