use std::{
future::{Future, IntoFuture},
pin::Pin,
};
use num_enum::TryFromPrimitive;
use crate::{
taxonomy::*, CallError, Entity, InvalidResponse, Session, SessionAsync, SessionTrait,
String255, Token, Value,
};
use crate::session::api::macros::*;
decl_call! {
InsertField(entity: Entity, attribute: Attribute, name: String255, value: Value, authorization: Token) -> (),
{
index: i64,
instance: i32,
deferred: bool,
}
}
impl<T: SessionTrait> InsertField<T> {
async fn call_async(self) -> Result<(), CallError> {
let _ = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Insert, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_name(self.name)
.with_value(&self.value)
.with_index(self.index)
.with_instance(self.instance)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
RemoveField(entity: Entity, attribute: Attribute, index: i64, authorization: Token) -> (),
{
instance: i32,
deferred: bool,
}
}
impl<T: SessionTrait> RemoveField<T> {
async fn call_async(self) -> Result<(), CallError> {
let _ = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Remove, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_index(self.index)
.with_instance(self.instance)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
ReplaceField(entity: Entity, attribute: Attribute, name: String255, value: Value, authorization: Token) -> (),
{
index: i64,
instance: i32,
deferred: bool,
}
}
impl<T: SessionTrait> ReplaceField<T> {
async fn call_async(self) -> Result<(), CallError> {
let _ = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Replace, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_name(self.name)
.with_value(&self.value)
.with_index(self.index)
.with_instance(self.instance)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
FindField(entity: Entity, attribute: Attribute, value: Value, authorization: Token) -> i64,
{
index: i64,
instance: i32,
}
}
impl<T: SessionTrait> FindField<T> {
async fn call_async(self) -> Result<i64, CallError> {
let res = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Find, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_value(&self.value)
.with_index(self.index)
.with_instance(self.instance)
.await?;
Ok(res.integer()?)
}
}
decl_call! {
IncludeField(entity: Entity, attribute: Attribute, name: String255, value: Value, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> IncludeField<T> {
async fn call_async(self) -> Result<(), CallError> {
let _ = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Include, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_name(self.name)
.with_value(&self.value)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
ExcludeField(entity: Entity, attribute: Attribute, name: String255, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> ExcludeField<T> {
async fn call_async(self) -> Result<(), CallError> {
let _ = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Exclude, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_name(self.name)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
SetField(entity: Entity, attribute: Attribute, name: String255, value: Value, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> SetField<T> {
async fn call_async(self) -> Result<(), CallError> {
let _ = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Set, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_name(self.name)
.with_value(&self.value)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
GetField(entity: Entity, attribute: Attribute, name: String255, authorization: Token) -> Value,
{
}
}
impl<T: SessionTrait> GetField<T> {
async fn call_async(self) -> Result<Value, CallError> {
self.session
.get_async_session()
.invoke_entity(self.entity, Method::Get, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_name(self.name)
.await
}
}
decl_call! {
ClearField(entity: Entity, attribute: Attribute, name: String255, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> ClearField<T> {
async fn call_async(self) -> Result<(), CallError> {
let _ = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Clear, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_name(self.name)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
FieldCount(entity: Entity, attribute: Attribute, authorization: Token) -> i64,
{
instance: i32,
}
}
impl<T: SessionTrait> FieldCount<T> {
async fn call_async(self) -> Result<i64, CallError> {
let res = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Count, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_instance(self.instance)
.await?;
Ok(res.integer()?)
}
}
decl_call! {
FieldMember(entity: Entity, attribute: Attribute, name: String255, authorization: Token) -> bool,
{
instance: i32,
}
}
impl<T: SessionTrait> FieldMember<T> {
async fn call_async(self) -> Result<bool, CallError> {
let res = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Member, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_name(self.name)
.with_instance(self.instance)
.await?;
Ok(res.boolean()?)
}
}
decl_call! {
FieldName(entity: Entity, attribute: Attribute, index: i64, authorization: Token) -> String255,
{
instance: i32,
}
}
impl<T: SessionTrait> FieldName<T> {
async fn call_async(self) -> Result<String255, CallError> {
let res = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Name, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_index(self.index)
.with_instance(self.instance)
.await?;
Ok(res
.string()?
.try_into()
.map_err(|e| CallError::InvalidResponse(InvalidResponse::Name(e)))?)
}
}
decl_call! {
FieldKey(entity: Entity, attribute: Attribute, name: String255, authorization: Token) -> String255,
{
index: i64,
instance: i32,
}
}
impl<T: SessionTrait> FieldKey<T> {
async fn call_async(self) -> Result<String255, CallError> {
let res = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Key, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_name(self.name)
.with_index(self.index)
.with_instance(self.instance)
.await?;
Ok(res
.string()?
.try_into()
.map_err(|e| CallError::InvalidResponse(InvalidResponse::Key(e)))?)
}
}
decl_call! {
FieldValue(entity: Entity, attribute: Attribute, name: String255, authorization: Token) -> Value,
{
index: i64,
instance: i32,
}
}
impl<T: SessionTrait> FieldValue<T> {
async fn call_async(self) -> Result<Value, CallError> {
self.session
.get_async_session()
.invoke_entity(self.entity, Method::Value, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_name(self.name)
.with_index(self.index)
.with_instance(self.instance)
.await
}
}
decl_call! {
FieldIndex(entity: Entity, attribute: Attribute, name: String255, authorization: Token) -> i64,
{
instance: i32,
}
}
impl<T: SessionTrait> FieldIndex<T> {
async fn call_async(self) -> Result<i64, CallError> {
let res = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Index, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_name(self.name)
.with_instance(self.instance)
.await?;
Ok(res.integer()?)
}
}
decl_call! {
FieldAttribute(entity: Entity, authorization: Token) -> Attribute,
{
name: String255,
index: i64,
instance: i32,
}
}
impl<T: SessionTrait> FieldAttribute<T> {
async fn call_async(self) -> Result<Attribute, CallError> {
let res = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Attribute, self.authorization)
.with_aspect(Aspect::Field)
.with_name(self.name)
.with_index(self.index)
.with_instance(self.instance)
.await?;
let num64 = res.integer()?;
let num16 = u16::try_from(num64).map_err(|_| InvalidResponse::Attribute(num64))?;
let attr =
Attribute::try_from_primitive(num16).map_err(|_| InvalidResponse::Attribute(num64))?;
Ok(attr)
}
}
decl_call! {
SortFields(entity: Entity, attribute: Attribute, authorization: Token) -> (),
{
instance: i32,
deferred: bool,
}
}
impl<T: SessionTrait> SortFields<T> {
async fn call_async(self) -> Result<(), CallError> {
let _ = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Sort, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_instance(self.instance)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
PurgeFields(entity: Entity, attribute: Attribute, authorization: Token) -> (),
{
instance: i32,
deferred: bool,
}
}
impl<T: SessionTrait> PurgeFields<T> {
async fn call_async(self) -> Result<(), CallError> {
let _ = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Purge, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_instance(self.instance)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
RetrieveFields(entity: Entity, attribute: Attribute, authorization: Token) -> String,
{
instance: i32,
}
}
impl<T: SessionTrait> RetrieveFields<T> {
async fn call_async(self) -> Result<String, CallError> {
let res = self
.session
.get_async_session()
.invoke_entity(self.entity, Method::Retrieve, self.authorization)
.with_aspect(Aspect::Field)
.with_attribute(self.attribute)
.with_instance(self.instance)
.await?;
Ok(res.interchange()?)
}
}