orchestra-toolkit 0.6.1

Client to interract with Orchestra system, uses HGTP protocol
Documentation
/* Copyright 2024-2025 LEDR Technologies Inc.
* 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.
*
* 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.
*
* 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.
*
* 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/>.
*
* If you have any questions, feedback or issues about the Orchestra library, you can contact us at support@ledr.io.
*/

use std::{
    future::{Future, IntoFuture},
    pin::Pin,
};

use crate::{
    taxonomy::*, AvialModel, CallError, Entity, InvalidResponse, Locutor, Session, SessionAsync,
    SessionTrait, String255, Token, Value,
};

use super::macros::*;

mod annotations;
mod create_outlet;
mod facets;
mod factors;
mod facts;
mod features;
mod fields;
mod frames;
mod properties;
mod registries;

decl_call! {
    SaveEntity(entity: Entity, authorization: Token) -> (),
    {
    }
}
impl<T: SessionTrait> SaveEntity<T> {
    pub async fn call_async(self) -> Result<(), CallError> {
        self.session
            .get_async_session()
            .invoke_entity(self.entity, Method::Save, self.authorization)
            .await?;
        Ok(())
    }
}

decl_call! {
    RestoreEntity(entity: Entity, authorization: Token) -> (),
    {
    }
}
impl<T: SessionTrait> RestoreEntity<T> {
    pub async fn call_async(self) -> Result<(), CallError> {
        self.session
            .get_async_session()
            .invoke_entity(self.entity, Method::Load, self.authorization)
            .await?;
        Ok(())
    }
}

decl_call! {
    RetrieveEntity(entity: Entity, authorization: Token) -> AvialModel,
    {
    }
}
impl<T: SessionTrait> RetrieveEntity<T> {
    pub async fn call_async(self) -> Result<AvialModel, CallError> {
        let val = self
            .session
            .get_async_session()
            .invoke_entity(self.entity, Method::Retrieve, self.authorization)
            .await?;
        let interchange = val.interchange().map_err(InvalidResponse::ValueTag)?;
        Ok(serde_json::from_str(&interchange).map_err(InvalidResponse::ValueDeser)?)
    }
}

decl_call! {
    StoreEntity(entity: Entity, model: AvialModel, authorization: Token) -> (),
    {
        mode: Mode,
    }
}
impl<T: SessionTrait> StoreEntity<T> {
    pub async fn call_async(self) -> Result<(), CallError> {
        // UNWRAP SAFETY:
        // serde_json::to_string documentation states:
        //
        // > Serialization can fail if `T`'s implementation of `Serialize` decides to
        // > fail, or if `T` contains a map with non-string keys.
        //
        // We control the `AvialModel` type and it is guaranteed to be serializable.
        let val = Value::Interchange(serde_json::to_string(&self.model).unwrap());
        self.session
            .get_async_session()
            .invoke_entity(self.entity, Method::Store, self.authorization)
            .with_mode(self.mode)
            .with_value(&val)
            .await?;
        Ok(())
    }
}

decl_call! {
    PurgeEntity(entity: Entity, authorization: Token) -> (),
    {
        instance: i32,
    }
}
impl<T: SessionTrait> PurgeEntity<T> {
    pub async fn call_async(self) -> Result<(), CallError> {
        self.session
            .get_async_session()
            .invoke_entity(self.entity, Method::Purge, self.authorization)
            .with_instance(self.instance)
            .await?;
        Ok(())
    }
}

decl_call! {
    Redirect(from: Entity, to: Entity, authorization: Token) -> (),
    {
        server: Entity,
    }
}
impl<T: SessionTrait> Redirect<T> {
    pub async fn call_async(self) -> Result<(), CallError> {
        self.session
            .get_async_session()
            .entity_redirect(self.from, self.to, self.authorization)
            .with_server(self.server)
            .await?;
        Ok(())
    }
}

decl_call! {
    CreateOutlet(name: String255, authorization: Token) -> Entity,
    {
        outlet: Entity,
        server: Entity,
        key: String255,
        context: Context,
        category: Category,
        class: Class,
        method: Method,
        attribute: Attribute,
        event: Event,
        precedence: u16,
        timeout: i64,
    }
}
impl<T: SessionTrait> CreateOutlet<T> {
    async fn call_async(self) -> Result<Entity, CallError> {
        let session = self.session.get_async_session();

        let e = session
            .create_entity(self.name, self.authorization)
            .with_outlet(self.outlet)
            .with_server(self.server)
            .with_key(self.key)
            .with_context(self.context)
            .with_category(self.category)
            .with_class(self.class)
            .with_method(self.method)
            .with_attribute(self.attribute)
            .with_event(self.event)
            .with_precedence(self.precedence)
            .with_timeout(self.timeout)
            .await?;

        session.activate_outlet(e, self.authorization).await?;

        Ok(e)
    }
}

decl_call! {
    CreateObject(name: String255, authorization: Token) -> Entity,
    {
        key: String255,
        value: Value,
        context: Context,
        category: Category,
        class: Class,
        mode: Mode,
        outlet: Entity,
        server: Entity,
    }
}
impl<T: SessionTrait> CreateObject<T> {
    async fn call_async(self) -> Result<Entity, CallError> {
        let session = self.session.get_async_session();

        let outlet = match self.outlet {
            Entity::NULL => Entity::new(0, 0, 11),
            e => e,
        };

        let e = session
            .invoke_entity(outlet, Method::Create, self.authorization)
            .with_name(self.name)
            .with_key(self.key)
            .with_value(&self.value)
            .with_context(self.context)
            .with_category(self.category)
            .with_class(self.class)
            .with_mode(self.mode)
            .with_ancillary(self.server)
            .await?;

        Ok(e.entity()?)
    }
}

decl_call! {
    DeleteObject(object: Entity, authorization: Token) -> (),
    {}
}
impl<T: SessionTrait> DeleteObject<T> {
    async fn call_async(self) -> Result<(), CallError> {
        let session = self.session.get_async_session();

        session
            .invoke_entity(self.object, Method::Delete, self.authorization)
            .await?;

        Ok(())
    }
}

decl_call! {
    CreateGeneral(name: String255, authorization: Token) -> Entity,
    {
        key: String255,
        value: Value,
        context: Context,
        category: Category,
        class: Class,
        mode: Mode,
        outlet: Entity,
        server: Entity,
    }
}
impl<T: SessionTrait> CreateGeneral<T> {
    async fn call_async(self) -> Result<Entity, CallError> {
        let session = self.session.get_async_session();

        let outlet = match self.outlet {
            Entity::NULL => Entity::new(0, 0, 14),
            e => e,
        };

        let e = session
            .invoke_entity(outlet, Method::Create, self.authorization)
            .with_name(self.name)
            .with_key(self.key)
            .with_value(&self.value)
            .with_context(self.context)
            .with_category(self.category)
            .with_class(self.class)
            .with_mode(self.mode)
            .with_ancillary(self.server)
            .await?;

        Ok(e.entity()?)
    }
}

decl_call! {
    DeleteGeneral(general: Entity, authorization: Token) -> (),
    {}
}
impl<T: SessionTrait> DeleteGeneral<T> {
    async fn call_async(self) -> Result<(), CallError> {
        let session = self.session.get_async_session();

        session
            .invoke_entity(self.general, Method::Delete, self.authorization)
            .await?;

        Ok(())
    }
}

decl_call! {
    InvokeLocutor(locutor: Locutor) -> Value,
    {}
}
impl<T: SessionTrait> InvokeLocutor<T> {
    async fn call_async(self) -> Result<Value, CallError> {
        let session = self.session.get_async_session();

        session
            .invoke_entity(
                self.locutor.entity,
                self.locutor.method,
                self.locutor.authorization,
            )
            .with_auxiliary(self.locutor.auxiliary)
            .with_ancillary(self.locutor.ancillary)
            .with_attribute(self.locutor.attribute)
            .with_instance(self.locutor.instance)
            .with_offset(self.locutor.offset)
            .with_name(self.locutor.name)
            .with_key(self.locutor.key)
            .with_value(&self.locutor.value)
            .with_parameter(self.locutor.parameter)
            .with_resultant(self.locutor.resultant)
            .with_index(self.locutor.index)
            .with_count(self.locutor.count)
            .with_aspect(self.locutor.aspect)
            .with_context(self.locutor.context)
            .with_category(self.locutor.category)
            .with_class(self.locutor.class)
            .with_event(self.locutor.event)
            .with_mode(self.locutor.mode)
            .with_state(self.locutor.state)
            .with_condition(self.locutor.condition)
            .with_precedence(self.locutor.precedence)
            .with_time(self.locutor.time)
            .with_timeout(self.locutor.timeout)
            .with_authority(self.locutor.authority)
            .await
    }
}