orchestra_toolkit/session/api/avial/
frames.rs

1/* Copyright 2024-2025 LEDR Technologies Inc.
2* This file is part of the Orchestra library, which helps developer use our Orchestra technology which is based on AvesTerra, owned and developped by Georgetown University, under license agreement with LEDR Technologies Inc.
3*
4* The Orchestra library is a free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
5*
6* The Orchestra library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
7*
8* You should have received a copy of the GNU Lesser General Public License along with the Orchestra library. If not, see <https://www.gnu.org/licenses/>.
9*
10* If you have any questions, feedback or issues about the Orchestra library, you can contact us at support@ledr.io.
11*/
12
13use std::{
14    future::{Future, IntoFuture},
15    pin::Pin,
16};
17
18use num_enum::TryFromPrimitive;
19
20use crate::{
21    taxonomy::*, CallError, Entity, InvalidResponse, Session, SessionAsync, SessionTrait,
22    String255, Token, Value,
23};
24
25use crate::session::api::macros::*;
26
27// ------------------------
28// -- Indexed operations --
29// ------------------------
30
31decl_call! {
32    InsertFrame(entity: Entity, attribute: Attribute, key: String255, authorization: Token) -> (),
33    {
34        index: i64,
35        instance: i32,
36        deferred: bool,
37    }
38}
39impl<T: SessionTrait> InsertFrame<T> {
40    async fn call_async(self) -> Result<(), CallError> {
41        let _ = self
42            .session
43            .get_async_session()
44            .invoke_entity(self.entity, Method::Insert, self.authorization)
45            .with_aspect(Aspect::Frame)
46            .with_attribute(self.attribute)
47            .with_key(self.key)
48            .with_index(self.index)
49            .with_instance(self.instance)
50            .with_parameter(self.deferred as i64)
51            .await?;
52        Ok(())
53    }
54}
55
56decl_call! {
57    RemoveFrame(entity: Entity, attribute: Attribute, index: i64, authorization: Token) -> (),
58    {
59        instance: i32,
60        deferred: bool,
61    }
62}
63impl<T: SessionTrait> RemoveFrame<T> {
64    async fn call_async(self) -> Result<(), CallError> {
65        let _ = self
66            .session
67            .get_async_session()
68            .invoke_entity(self.entity, Method::Remove, self.authorization)
69            .with_aspect(Aspect::Frame)
70            .with_attribute(self.attribute)
71            .with_index(self.index)
72            .with_instance(self.instance)
73            .with_parameter(self.deferred as i64)
74            .await?;
75        Ok(())
76    }
77}
78
79decl_call! {
80    ReplaceFrame(entity: Entity, attribute: Attribute, index: i64, key: String255, authorization: Token) -> (),
81    {
82        instance: i32,
83        deferred: bool,
84    }
85}
86impl<T: SessionTrait> ReplaceFrame<T> {
87    async fn call_async(self) -> Result<(), CallError> {
88        let _ = self
89            .session
90            .get_async_session()
91            .invoke_entity(self.entity, Method::Replace, self.authorization)
92            .with_aspect(Aspect::Frame)
93            .with_attribute(self.attribute)
94            .with_key(self.key)
95            .with_index(self.index)
96            .with_instance(self.instance)
97            .with_parameter(self.deferred as i64)
98            .await?;
99        Ok(())
100    }
101}
102
103decl_call! {
104    FindFrame(entity: Entity, authorization: Token) -> i64,
105    {
106        attribute: Attribute,
107        name: String255,
108        key: String255,
109        value: Value,
110        instance: i32,
111        index: i64,
112        offset: i32,
113    }
114}
115impl<T: SessionTrait> FindFrame<T> {
116    async fn call_async(self) -> Result<i64, CallError> {
117        let res = self
118            .session
119            .get_async_session()
120            .invoke_entity(self.entity, Method::Find, self.authorization)
121            .with_aspect(Aspect::Frame)
122            .with_attribute(self.attribute)
123            .with_name(self.name)
124            .with_key(self.key)
125            .with_value(&self.value)
126            .with_index(self.index)
127            .with_instance(self.instance)
128            .with_offset(self.offset)
129            .await?;
130        Ok(res.integer()?)
131    }
132}
133
134// -----------------------
135// -- Mapped operations --
136// -----------------------
137
138decl_call! {
139    IncludeFrame(entity: Entity, attribute: Attribute, key: String255, authorization: Token) -> (),
140    {
141        deferred: bool,
142    }
143}
144impl<T: SessionTrait> IncludeFrame<T> {
145    async fn call_async(self) -> Result<(), CallError> {
146        let _ = self
147            .session
148            .get_async_session()
149            .invoke_entity(self.entity, Method::Include, self.authorization)
150            .with_aspect(Aspect::Frame)
151            .with_attribute(self.attribute)
152            .with_key(self.key)
153            .with_parameter(self.deferred as i64)
154            .await?;
155        Ok(())
156    }
157}
158
159decl_call! {
160    ExcludeFrame(entity: Entity, attribute: Attribute, key: String255, authorization: Token) -> (),
161    {
162        deferred: bool,
163    }
164}
165impl<T: SessionTrait> ExcludeFrame<T> {
166    async fn call_async(self) -> Result<(), CallError> {
167        let _ = self
168            .session
169            .get_async_session()
170            .invoke_entity(self.entity, Method::Exclude, self.authorization)
171            .with_aspect(Aspect::Frame)
172            .with_attribute(self.attribute)
173            .with_key(self.key)
174            .with_parameter(self.deferred as i64)
175            .await?;
176        Ok(())
177    }
178}
179
180// -----------------------
181// -- Hybrid operations --
182// -----------------------
183
184decl_call! {
185    SetFrame(entity: Entity, attribute: Attribute, key: String255, name: String255, value: Value, authorization: Token) -> (),
186    {
187        instance: i32,
188        index: i64,
189        offset: i32,
190        deferred: bool,
191    }
192}
193impl<T: SessionTrait> SetFrame<T> {
194    async fn call_async(self) -> Result<(), CallError> {
195        let _ = self
196            .session
197            .get_async_session()
198            .invoke_entity(self.entity, Method::Set, self.authorization)
199            .with_aspect(Aspect::Frame)
200            .with_attribute(self.attribute)
201            .with_name(self.name)
202            .with_key(self.key)
203            .with_value(&self.value)
204            .with_index(self.index)
205            .with_instance(self.instance)
206            .with_offset(self.offset)
207            .with_parameter(self.deferred as i64)
208            .await?;
209        Ok(())
210    }
211}
212
213decl_call! {
214    GetFrame(entity: Entity, attribute: Attribute, key: String255, name: String255, authorization: Token) -> Value,
215    {
216        instance: i32,
217        index: i64,
218        offset: i32,
219    }
220}
221impl<T: SessionTrait> GetFrame<T> {
222    async fn call_async(self) -> Result<Value, CallError> {
223        self.session
224            .get_async_session()
225            .invoke_entity(self.entity, Method::Get, self.authorization)
226            .with_aspect(Aspect::Frame)
227            .with_attribute(self.attribute)
228            .with_name(self.name)
229            .with_key(self.key)
230            .with_index(self.index)
231            .with_instance(self.instance)
232            .with_offset(self.offset)
233            .await
234    }
235}
236
237decl_call! {
238    ClearFrame(entity: Entity, attribute: Attribute, key: String255, name: String255, authorization: Token) -> (),
239    {
240        instance: i32,
241        index: i64,
242        offset: i32,
243        deferred: bool,
244    }
245}
246impl<T: SessionTrait> ClearFrame<T> {
247    async fn call_async(self) -> Result<(), CallError> {
248        let _ = self
249            .session
250            .get_async_session()
251            .invoke_entity(self.entity, Method::Clear, self.authorization)
252            .with_aspect(Aspect::Frame)
253            .with_attribute(self.attribute)
254            .with_name(self.name)
255            .with_key(self.key)
256            .with_index(self.index)
257            .with_instance(self.instance)
258            .with_offset(self.offset)
259            .with_parameter(self.deferred as i64)
260            .await?;
261        Ok(())
262    }
263}
264
265// -----------------------
266// -- Utility functions --
267// -----------------------
268
269decl_call! {
270    FrameCount(entity: Entity, attribute: Attribute, authorization: Token) -> i64,
271    {
272        instance: i32,
273    }
274}
275impl<T: SessionTrait> FrameCount<T> {
276    async fn call_async(self) -> Result<i64, CallError> {
277        let res = self
278            .session
279            .get_async_session()
280            .invoke_entity(self.entity, Method::Count, self.authorization)
281            .with_aspect(Aspect::Frame)
282            .with_attribute(self.attribute)
283            .with_instance(self.instance)
284            .await?;
285        Ok(res.integer()?)
286    }
287}
288
289decl_call! {
290    FrameMember(entity: Entity, attribute: Attribute, key: String255, authorization: Token) -> bool,
291    {
292        instance: i32,
293    }
294}
295impl<T: SessionTrait> FrameMember<T> {
296    async fn call_async(self) -> Result<bool, CallError> {
297        let res = self
298            .session
299            .get_async_session()
300            .invoke_entity(self.entity, Method::Member, self.authorization)
301            .with_aspect(Aspect::Frame)
302            .with_attribute(self.attribute)
303            .with_key(self.key)
304            .with_instance(self.instance)
305            .await?;
306        Ok(res.boolean()?)
307    }
308}
309
310decl_call! {
311    FrameName(entity: Entity, attribute: Attribute, offset: i32, authorization: Token) -> String255,
312    {
313        instance: i32,
314    }
315}
316impl<T: SessionTrait> FrameName<T> {
317    async fn call_async(self) -> Result<String255, CallError> {
318        let res = self
319            .session
320            .get_async_session()
321            .invoke_entity(self.entity, Method::Name, self.authorization)
322            .with_aspect(Aspect::Frame)
323            .with_attribute(self.attribute)
324            .with_instance(self.instance)
325            .with_offset(self.offset)
326            .await?;
327        Ok(res
328            .string()?
329            .try_into()
330            .map_err(|e| CallError::InvalidResponse(InvalidResponse::Name(e)))?)
331    }
332}
333
334decl_call! {
335    FrameKey(entity: Entity, attribute: Attribute, index: i64, authorization: Token) -> String255,
336    {
337        instance: i32,
338    }
339}
340impl<T: SessionTrait> FrameKey<T> {
341    async fn call_async(self) -> Result<String255, CallError> {
342        let res = self
343            .session
344            .get_async_session()
345            .invoke_entity(self.entity, Method::Key, self.authorization)
346            .with_aspect(Aspect::Frame)
347            .with_attribute(self.attribute)
348            .with_index(self.index)
349            .with_instance(self.instance)
350            .await?;
351        Ok(res
352            .string()?
353            .try_into()
354            .map_err(|e| CallError::InvalidResponse(InvalidResponse::Key(e)))?)
355    }
356}
357
358decl_call! {
359    FrameValue(entity: Entity, attribute: Attribute, key: String255, name: String255, authorization: Token) -> Value,
360    {
361        instance: i32,
362        index: i64,
363        offset: i32,
364    }
365}
366impl<T: SessionTrait> FrameValue<T> {
367    async fn call_async(self) -> Result<Value, CallError> {
368        self.session
369            .get_async_session()
370            .invoke_entity(self.entity, Method::Value, self.authorization)
371            .with_aspect(Aspect::Frame)
372            .with_attribute(self.attribute)
373            .with_name(self.name)
374            .with_key(self.key)
375            .with_index(self.index)
376            .with_instance(self.instance)
377            .with_offset(self.offset)
378            .await
379    }
380}
381
382decl_call! {
383    FrameIndex(entity: Entity, attribute: Attribute, key: String255, authorization: Token) -> i64,
384    {
385        instance: i32,
386    }
387}
388impl<T: SessionTrait> FrameIndex<T> {
389    async fn call_async(self) -> Result<i64, CallError> {
390        let res = self
391            .session
392            .get_async_session()
393            .invoke_entity(self.entity, Method::Index, self.authorization)
394            .with_aspect(Aspect::Frame)
395            .with_attribute(self.attribute)
396            .with_key(self.key)
397            .with_instance(self.instance)
398            .await?;
399        Ok(res.integer()?)
400    }
401}
402
403decl_call! {
404    // Just use FactAttribute
405    FrameAttribute(entity: Entity, authorization: Token) -> Attribute,
406    {
407        key: String255,
408        index: i64,
409        instance: i32,
410    }
411}
412impl<T: SessionTrait> FrameAttribute<T> {
413    async fn call_async(self) -> Result<Attribute, CallError> {
414        let res = self
415            .session
416            .get_async_session()
417            .invoke_entity(self.entity, Method::Attribute, self.authorization)
418            .with_aspect(Aspect::Frame)
419            .with_key(self.key)
420            .with_index(self.index)
421            .with_instance(self.instance)
422            .await?;
423        let num64 = res.integer()?;
424        let num16 = u16::try_from(num64).map_err(|_| InvalidResponse::Attribute(num64))?;
425        let attr =
426            Attribute::try_from_primitive(num16).map_err(|_| InvalidResponse::Attribute(num64))?;
427        Ok(attr)
428    }
429}
430
431// --------------------------
432// -- Plurality operations --
433// --------------------------
434
435decl_call! {
436    SortFrames(entity: Entity, attribute: Attribute, authorization: Token) -> (),
437    {
438        instance: i32,
439        deferred: bool,
440    }
441}
442impl<T: SessionTrait> SortFrames<T> {
443    async fn call_async(self) -> Result<(), CallError> {
444        let _ = self
445            .session
446            .get_async_session()
447            .invoke_entity(self.entity, Method::Sort, self.authorization)
448            .with_aspect(Aspect::Frame)
449            .with_attribute(self.attribute)
450            .with_instance(self.instance)
451            .with_parameter(self.deferred as i64)
452            .await?;
453        Ok(())
454    }
455}
456
457decl_call! {
458    PurgeFrames(entity: Entity, attribute: Attribute, authorization: Token) -> (),
459    {
460        instance: i32,
461        deferred: bool,
462    }
463}
464impl<T: SessionTrait> PurgeFrames<T> {
465    async fn call_async(self) -> Result<(), CallError> {
466        let _ = self
467            .session
468            .get_async_session()
469            .invoke_entity(self.entity, Method::Purge, self.authorization)
470            .with_aspect(Aspect::Frame)
471            .with_attribute(self.attribute)
472            .with_instance(self.instance)
473            .with_parameter(self.deferred as i64)
474            .await?;
475        Ok(())
476    }
477}
478
479decl_call! {
480    RetrieveFrames(entity: Entity, attribute: Attribute, authorization: Token) -> String,
481    {
482        instance: i32,
483    }
484}
485impl<T: SessionTrait> RetrieveFrames<T> {
486    async fn call_async(self) -> Result<String, CallError> {
487        let res = self
488            .session
489            .get_async_session()
490            .invoke_entity(self.entity, Method::Retrieve, self.authorization)
491            .with_aspect(Aspect::Frame)
492            .with_attribute(self.attribute)
493            .with_instance(self.instance)
494            .await?;
495        Ok(res.interchange()?)
496        // TODO: return Vec<Frame> ?
497    }
498}