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! {
InsertFeature(entity: Entity, attribute: Attribute, key: String255, name: String255, value: Value, authorization: Token) -> (),
{
index: i64,
instance: i32,
deferred: bool,
}
}
impl<T: SessionTrait> InsertFeature<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::Feature)
.with_attribute(self.attribute)
.with_name(self.name)
.with_key(self.key)
.with_value(&self.value)
.with_index(self.index)
.with_instance(self.instance)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
RemoveFeature(entity: Entity, attribute: Attribute, index: i64, authorization: Token) -> (),
{
instance: i32,
deferred: bool,
}
}
impl<T: SessionTrait> RemoveFeature<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::Feature)
.with_attribute(self.attribute)
.with_index(self.index)
.with_instance(self.instance)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
ReplaceFeature(entity: Entity, attribute: Attribute, key: String255, name: String255, value: Value, authorization: Token) -> (),
{
index: i64,
instance: i32,
deferred: bool,
}
}
impl<T: SessionTrait> ReplaceFeature<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::Feature)
.with_attribute(self.attribute)
.with_name(self.name)
.with_key(self.key)
.with_value(&self.value)
.with_index(self.index)
.with_instance(self.instance)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
FindFeature(entity: Entity, attribute: Attribute, authorization: Token) -> i64,
{
instance: i32,
index: i64,
name: String255,
value: Value,
}
}
impl<T: SessionTrait> FindFeature<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::Feature)
.with_attribute(self.attribute)
.with_name(self.name)
.with_value(&self.value)
.with_index(self.index)
.with_instance(self.instance)
.await?;
Ok(res.integer()?)
}
}
decl_call! {
IncludeFeature(entity: Entity, attribute: Attribute, key: String255, name: String255, value: Value, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> IncludeFeature<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::Feature)
.with_attribute(self.attribute)
.with_name(self.name)
.with_key(self.key)
.with_value(&self.value)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
ExcludeFeature(entity: Entity, attribute: Attribute, key: String255, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> ExcludeFeature<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::Feature)
.with_attribute(self.attribute)
.with_key(self.key)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
SetFeature(entity: Entity, attribute: Attribute, key: String255, name: String255, value: Value, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> SetFeature<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::Feature)
.with_attribute(self.attribute)
.with_name(self.name)
.with_key(self.key)
.with_value(&self.value)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
GetFeature(entity: Entity, attribute: Attribute, key: String255, authorization: Token) -> Value,
{
}
}
impl<T: SessionTrait> GetFeature<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::Feature)
.with_attribute(self.attribute)
.with_key(self.key)
.await
}
}
decl_call! {
ClearFeature(entity: Entity, attribute: Attribute, key: String255, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> ClearFeature<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::Feature)
.with_attribute(self.attribute)
.with_key(self.key)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
FeatureCount(entity: Entity, attribute: Attribute, authorization: Token) -> i64,
{
instance: i32,
}
}
impl<T: SessionTrait> FeatureCount<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::Feature)
.with_attribute(self.attribute)
.with_instance(self.instance)
.await?;
Ok(res.integer()?)
}
}
decl_call! {
FeatureMember(entity: Entity, attribute: Attribute, key: String255, authorization: Token) -> bool,
{
instance: i32,
}
}
impl<T: SessionTrait> FeatureMember<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::Feature)
.with_attribute(self.attribute)
.with_key(self.key)
.with_instance(self.instance)
.await?;
Ok(res.boolean()?)
}
}
decl_call! {
FeatureName(entity: Entity, attribute: Attribute, key: String255, authorization: Token) -> String255,
{
index: i64,
instance: i32,
}
}
impl<T: SessionTrait> FeatureName<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::Feature)
.with_attribute(self.attribute)
.with_key(self.key)
.with_index(self.index)
.with_instance(self.instance)
.await?;
Ok(res
.string()?
.try_into()
.map_err(|e| CallError::InvalidResponse(InvalidResponse::Name(e)))?)
}
}
decl_call! {
FeatureKey(entity: Entity, attribute: Attribute, index: i64, authorization: Token) -> String255,
{
instance: i32,
}
}
impl<T: SessionTrait> FeatureKey<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::Feature)
.with_attribute(self.attribute)
.with_index(self.index)
.with_instance(self.instance)
.await?;
Ok(res
.string()?
.try_into()
.map_err(|e| CallError::InvalidResponse(InvalidResponse::Key(e)))?)
}
}
decl_call! {
FeatureValue(entity: Entity, attribute: Attribute, key: String255, authorization: Token) -> Value,
{
index: i64,
instance: i32,
}
}
impl<T: SessionTrait> FeatureValue<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::Feature)
.with_attribute(self.attribute)
.with_key(self.key)
.with_index(self.index)
.with_instance(self.instance)
.await
}
}
decl_call! {
FeatureIndex(entity: Entity, attribute: Attribute, key: String255, authorization: Token) -> i64,
{
instance: i32,
}
}
impl<T: SessionTrait> FeatureIndex<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::Feature)
.with_attribute(self.attribute)
.with_key(self.key)
.with_instance(self.instance)
.await?;
Ok(res.integer()?)
}
}
decl_call! {
FeatureAttribute(entity: Entity, authorization: Token) -> Attribute,
{
key: String255,
index: i64,
instance: i32,
}
}
impl<T: SessionTrait> FeatureAttribute<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::Feature)
.with_key(self.key)
.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! {
SortFeatures(entity: Entity, attribute: Attribute, authorization: Token) -> (),
{
instance: i32,
deferred: bool,
}
}
impl<T: SessionTrait> SortFeatures<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::Feature)
.with_attribute(self.attribute)
.with_instance(self.instance)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
PurgeFeatures(entity: Entity, attribute: Attribute, authorization: Token) -> (),
{
instance: i32,
deferred: bool,
}
}
impl<T: SessionTrait> PurgeFeatures<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::Feature)
.with_attribute(self.attribute)
.with_instance(self.instance)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
RetrieveFeatures(entity: Entity, attribute: Attribute, authorization: Token) -> String,
{
instance: i32,
}
}
impl<T: SessionTrait> RetrieveFeatures<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::Feature)
.with_attribute(self.attribute)
.with_instance(self.instance)
.await?;
Ok(res.interchange()?)
}
}