orchestra_toolkit/session/api/avial/
factors.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    InsertFactor(entity: Entity, attribute: Attribute, name: String255, key: String255, value: Value, authorization: Token) -> (),
33    {
34        index: i64,
35        offset: i32,
36        instance: i32,
37        deferred: bool,
38    }
39}
40impl<T: SessionTrait> InsertFactor<T> {
41    async fn call_async(self) -> Result<(), CallError> {
42        let _ = self
43            .session
44            .get_async_session()
45            .invoke_entity(self.entity, Method::Insert, self.authorization)
46            .with_aspect(Aspect::Factor)
47            .with_attribute(self.attribute)
48            .with_name(self.name)
49            .with_key(self.key)
50            .with_value(&self.value)
51            .with_index(self.index)
52            .with_instance(self.instance)
53            .with_offset(self.offset)
54            .with_parameter(self.deferred as i64)
55            .await?;
56        Ok(())
57    }
58}
59
60decl_call! {
61    RemoveFactor(entity: Entity, attribute: Attribute, name: String255, index: i64, authorization: Token) -> (),
62    {
63        instance: i32,
64        offset: i32,
65        deferred: bool,
66    }
67}
68impl<T: SessionTrait> RemoveFactor<T> {
69    async fn call_async(self) -> Result<(), CallError> {
70        let _ = self
71            .session
72            .get_async_session()
73            .invoke_entity(self.entity, Method::Remove, self.authorization)
74            .with_aspect(Aspect::Factor)
75            .with_attribute(self.attribute)
76            .with_name(self.name)
77            .with_index(self.index)
78            .with_instance(self.instance)
79            .with_offset(self.offset)
80            .with_parameter(self.deferred as i64)
81            .await?;
82        Ok(())
83    }
84}
85
86decl_call! {
87    ReplaceFactor(entity: Entity, attribute: Attribute, name: String255, key: String255, value: Value, authorization: Token) -> (),
88    {
89        offset: i32,
90        instance: i32,
91        index: i64,
92        deferred: bool,
93    }
94}
95impl<T: SessionTrait> ReplaceFactor<T> {
96    async fn call_async(self) -> Result<(), CallError> {
97        let _ = self
98            .session
99            .get_async_session()
100            .invoke_entity(self.entity, Method::Replace, self.authorization)
101            .with_aspect(Aspect::Factor)
102            .with_attribute(self.attribute)
103            .with_name(self.name)
104            .with_key(self.key)
105            .with_value(&self.value)
106            .with_index(self.index)
107            .with_instance(self.instance)
108            .with_offset(self.offset)
109            .with_parameter(self.deferred as i64)
110            .await?;
111        Ok(())
112    }
113}
114
115decl_call! {
116    FindFactor(entity: Entity, attribute: Attribute, name: String255, value: Value, authorization: Token) -> i64,
117    {
118        offset: i32,
119        instance: i32,
120        index: i64,
121    }
122}
123impl<T: SessionTrait> FindFactor<T> {
124    async fn call_async(self) -> Result<i64, CallError> {
125        let res = self
126            .session
127            .get_async_session()
128            .invoke_entity(self.entity, Method::Find, self.authorization)
129            .with_aspect(Aspect::Factor)
130            .with_attribute(self.attribute)
131            .with_name(self.name)
132            .with_value(&self.value)
133            .with_index(self.index)
134            .with_instance(self.instance)
135            .with_offset(self.offset)
136            .await?;
137        Ok(res.integer()?)
138    }
139}
140
141// -----------------------
142// -- Mapped operations --
143// -----------------------
144
145decl_call! {
146    IncludeFactor(entity: Entity, attribute: Attribute, name: String255, key: String255, value: Value, authorization: Token) -> (),
147    {
148        deferred: bool,
149    }
150}
151impl<T: SessionTrait> IncludeFactor<T> {
152    async fn call_async(self) -> Result<(), CallError> {
153        let _ = self
154            .session
155            .get_async_session()
156            .invoke_entity(self.entity, Method::Include, self.authorization)
157            .with_aspect(Aspect::Factor)
158            .with_attribute(self.attribute)
159            .with_name(self.name)
160            .with_key(self.key)
161            .with_value(&self.value)
162            .with_parameter(self.deferred as i64)
163            .await?;
164        Ok(())
165    }
166}
167
168decl_call! {
169    ExcludeFactor(entity: Entity, attribute: Attribute, name: String255, key: String255, authorization: Token) -> (),
170    {
171        deferred: bool,
172    }
173}
174impl<T: SessionTrait> ExcludeFactor<T> {
175    async fn call_async(self) -> Result<(), CallError> {
176        let _ = self
177            .session
178            .get_async_session()
179            .invoke_entity(self.entity, Method::Exclude, self.authorization)
180            .with_aspect(Aspect::Factor)
181            .with_attribute(self.attribute)
182            .with_name(self.name)
183            .with_key(self.key)
184            .with_parameter(self.deferred as i64)
185            .await?;
186        Ok(())
187    }
188}
189
190// -----------------------
191// -- Hybrid operations --
192// -----------------------
193
194decl_call! {
195    SetFactor(entity: Entity, attribute: Attribute, name: String255, key: String255, value: Value, authorization: Token) -> (),
196    {
197        deferred: bool,
198    }
199}
200impl<T: SessionTrait> SetFactor<T> {
201    async fn call_async(self) -> Result<(), CallError> {
202        let _ = self
203            .session
204            .get_async_session()
205            .invoke_entity(self.entity, Method::Set, self.authorization)
206            .with_aspect(Aspect::Factor)
207            .with_attribute(self.attribute)
208            .with_name(self.name)
209            .with_key(self.key)
210            .with_value(&self.value)
211            .with_parameter(self.deferred as i64)
212            .await?;
213        Ok(())
214    }
215}
216
217decl_call! {
218    GetFactor(entity: Entity, attribute: Attribute, name: String255, key: String255, authorization: Token) -> Value,
219    {
220    }
221}
222impl<T: SessionTrait> GetFactor<T> {
223    async fn call_async(self) -> Result<Value, CallError> {
224        self.session
225            .get_async_session()
226            .invoke_entity(self.entity, Method::Get, self.authorization)
227            .with_aspect(Aspect::Factor)
228            .with_attribute(self.attribute)
229            .with_name(self.name)
230            .with_key(self.key)
231            .await
232    }
233}
234
235decl_call! {
236    ClearFactor(entity: Entity, attribute: Attribute, name: String255, key: String255, authorization: Token) -> (),
237    {
238        deferred: bool,
239    }
240}
241impl<T: SessionTrait> ClearFactor<T> {
242    async fn call_async(self) -> Result<(), CallError> {
243        let _ = self
244            .session
245            .get_async_session()
246            .invoke_entity(self.entity, Method::Clear, self.authorization)
247            .with_aspect(Aspect::Factor)
248            .with_attribute(self.attribute)
249            .with_name(self.name)
250            .with_key(self.key)
251            .with_parameter(self.deferred as i64)
252            .await?;
253        Ok(())
254    }
255}
256
257// -----------------------
258// -- Utility functions --
259// -----------------------
260
261decl_call! {
262    FactorCount(entity: Entity, attribute: Attribute, name: String255, authorization: Token) -> i64,
263    {
264        offset: i32,
265        instance: i32,
266    }
267}
268impl<T: SessionTrait> FactorCount<T> {
269    async fn call_async(self) -> Result<i64, CallError> {
270        let res = self
271            .session
272            .get_async_session()
273            .invoke_entity(self.entity, Method::Count, self.authorization)
274            .with_aspect(Aspect::Factor)
275            .with_attribute(self.attribute)
276            .with_name(self.name)
277            .with_instance(self.instance)
278            .with_offset(self.offset)
279            .await?;
280        Ok(res.integer()?)
281    }
282}
283
284decl_call! {
285    FactorMember(entity: Entity, attribute: Attribute, name: String255, key: String255, authorization: Token) -> bool,
286    {
287        offset: i32,
288        instance: i32,
289    }
290}
291impl<T: SessionTrait> FactorMember<T> {
292    async fn call_async(self) -> Result<bool, CallError> {
293        let res = self
294            .session
295            .get_async_session()
296            .invoke_entity(self.entity, Method::Member, self.authorization)
297            .with_aspect(Aspect::Factor)
298            .with_attribute(self.attribute)
299            .with_name(self.name)
300            .with_key(self.key)
301            .with_instance(self.instance)
302            .with_offset(self.offset)
303            .await?;
304        Ok(res.boolean()?)
305    }
306}
307
308decl_call! {
309    // Just use FacetName instead
310    FactorName(entity: Entity, authorization: Token) -> String255,
311    {
312        attribute: Attribute,
313        key: String255,
314        index: i64,
315        instance: i32,
316        offset: i32,
317    }
318}
319impl<T: SessionTrait> FactorName<T> {
320    async fn call_async(self) -> Result<String255, CallError> {
321        let res = self
322            .session
323            .get_async_session()
324            .invoke_entity(self.entity, Method::Name, self.authorization)
325            .with_aspect(Aspect::Factor)
326            .with_attribute(self.attribute)
327            .with_key(self.key)
328            .with_index(self.index)
329            .with_instance(self.instance)
330            .with_offset(self.offset)
331            .await?;
332        Ok(res
333            .string()?
334            .try_into()
335            .map_err(|e| CallError::InvalidResponse(InvalidResponse::Name(e)))?)
336    }
337}
338
339decl_call! {
340    FactorKey(entity: Entity, attribute: Attribute, name: String255, index: i64, authorization: Token) -> String255,
341    {
342        instance: i32,
343        offset: i32,
344    }
345}
346impl<T: SessionTrait> FactorKey<T> {
347    async fn call_async(self) -> Result<String255, CallError> {
348        let res = self
349            .session
350            .get_async_session()
351            .invoke_entity(self.entity, Method::Key, self.authorization)
352            .with_aspect(Aspect::Factor)
353            .with_attribute(self.attribute)
354            .with_name(self.name)
355            .with_index(self.index)
356            .with_instance(self.instance)
357            .with_offset(self.offset)
358            .await?;
359        Ok(res
360            .string()?
361            .try_into()
362            .map_err(|e| CallError::InvalidResponse(InvalidResponse::Key(e)))?)
363    }
364}
365
366decl_call! {
367    FactorValue(entity: Entity, attribute: Attribute, name: String255, key: String255, authorization: Token) -> Value,
368    {
369        index: i64,
370        instance: i32,
371        offset: i32,
372    }
373}
374impl<T: SessionTrait> FactorValue<T> {
375    async fn call_async(self) -> Result<Value, CallError> {
376        self.session
377            .get_async_session()
378            .invoke_entity(self.entity, Method::Value, self.authorization)
379            .with_aspect(Aspect::Factor)
380            .with_attribute(self.attribute)
381            .with_name(self.name)
382            .with_key(self.key)
383            .with_index(self.index)
384            .with_instance(self.instance)
385            .with_offset(self.offset)
386            .await
387    }
388}
389
390decl_call! {
391    FactorIndex(entity: Entity, attribute: Attribute, name: String255, key: String255, authorization: Token) -> i64,
392    {
393        instance: i32,
394        offset: i32,
395    }
396}
397impl<T: SessionTrait> FactorIndex<T> {
398    async fn call_async(self) -> Result<i64, CallError> {
399        let res = self
400            .session
401            .get_async_session()
402            .invoke_entity(self.entity, Method::Index, self.authorization)
403            .with_aspect(Aspect::Factor)
404            .with_attribute(self.attribute)
405            .with_name(self.name)
406            .with_key(self.key)
407            .with_instance(self.instance)
408            .with_offset(self.offset)
409            .await?;
410        Ok(res.integer()?)
411    }
412}
413
414decl_call! {
415    // Just use FactAttribute instead
416    FactorAttribute(entity: Entity, authorization: Token) -> Attribute,
417    {
418        name: String255,
419        key: String255,
420        offset: i32,
421        instance: i32,
422        index: i64,
423    }
424}
425impl<T: SessionTrait> FactorAttribute<T> {
426    async fn call_async(self) -> Result<Attribute, CallError> {
427        let res = self
428            .session
429            .get_async_session()
430            .invoke_entity(self.entity, Method::Attribute, self.authorization)
431            .with_aspect(Aspect::Factor)
432            .with_name(self.name)
433            .with_key(self.key)
434            .with_index(self.index)
435            .with_instance(self.instance)
436            .with_offset(self.offset)
437            .await?;
438        let num64 = res.integer()?;
439        let num16 = u16::try_from(num64).map_err(|_| InvalidResponse::Attribute(num64))?;
440        let attr =
441            Attribute::try_from_primitive(num16).map_err(|_| InvalidResponse::Attribute(num64))?;
442        Ok(attr)
443    }
444}
445
446// --------------------------
447// -- Plurality operations --
448// --------------------------
449
450decl_call! {
451    SortFactors(entity: Entity, attribute: Attribute, name: String255, authorization: Token) -> (),
452    {
453        offset: i32,
454        instance: i32,
455        deferred: bool,
456    }
457}
458impl<T: SessionTrait> SortFactors<T> {
459    async fn call_async(self) -> Result<(), CallError> {
460        let _ = self
461            .session
462            .get_async_session()
463            .invoke_entity(self.entity, Method::Sort, self.authorization)
464            .with_aspect(Aspect::Factor)
465            .with_attribute(self.attribute)
466            .with_name(self.name)
467            .with_instance(self.instance)
468            .with_offset(self.offset)
469            .with_parameter(self.deferred as i64)
470            .await?;
471        Ok(())
472    }
473}
474
475decl_call! {
476    PurgeFactors(entity: Entity, attribute: Attribute, name: String255, authorization: Token) -> (),
477    {
478        offset: i32,
479        instance: i32,
480        deferred: bool,
481    }
482}
483impl<T: SessionTrait> PurgeFactors<T> {
484    async fn call_async(self) -> Result<(), CallError> {
485        let _ = self
486            .session
487            .get_async_session()
488            .invoke_entity(self.entity, Method::Purge, self.authorization)
489            .with_aspect(Aspect::Factor)
490            .with_attribute(self.attribute)
491            .with_name(self.name)
492            .with_instance(self.instance)
493            .with_offset(self.offset)
494            .with_parameter(self.deferred as i64)
495            .await?;
496        Ok(())
497    }
498}
499
500decl_call! {
501    RetrieveFactors(entity: Entity, attribute: Attribute, name: String255, authorization: Token) -> String,
502    {
503        offset: i32,
504        instance: i32,
505    }
506}
507impl<T: SessionTrait> RetrieveFactors<T> {
508    async fn call_async(self) -> Result<String, CallError> {
509        let res = self
510            .session
511            .get_async_session()
512            .invoke_entity(self.entity, Method::Retrieve, self.authorization)
513            .with_aspect(Aspect::Factor)
514            .with_attribute(self.attribute)
515            .with_name(self.name)
516            .with_instance(self.instance)
517            .with_offset(self.offset)
518            .await?;
519        Ok(res.interchange()?)
520        // TODO: return Vec<Factor> ?
521    }
522}