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! {
InsertProperty(entity: Entity, key: String255, name: String255, value: Value, authorization: Token) -> (),
{
index: i64,
deferred: bool,
}
}
impl<T: SessionTrait> InsertProperty<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::Property)
.with_name(self.name)
.with_key(self.key)
.with_value(&self.value)
.with_index(self.index)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
RemoveProperty(entity: Entity, index: i64, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> RemoveProperty<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::Property)
.with_index(self.index)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
ReplaceProperty(entity: Entity, index: i64, key: String255, name: String255, value: Value, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> ReplaceProperty<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::Property)
.with_name(self.name)
.with_key(self.key)
.with_value(&self.value)
.with_index(self.index)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
FindProperty(entity: Entity, name: String255, value: Value, authorization: Token) -> i64,
{
index: i64,
}
}
impl<T: SessionTrait> FindProperty<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::Property)
.with_name(self.name)
.with_value(&self.value)
.with_index(self.index)
.await?;
Ok(res.integer()?)
}
}
decl_call! {
IncludeProperty(entity: Entity, key: String255, name: String255, value: Value, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> IncludeProperty<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::Property)
.with_name(self.name)
.with_key(self.key)
.with_value(&self.value)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
ExcludeProperty(entity: Entity, key: String255, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> ExcludeProperty<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::Property)
.with_key(self.key)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
SetProperty(entity: Entity, key: String255, name: String255, value: Value, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> SetProperty<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::Property)
.with_name(self.name)
.with_key(self.key)
.with_value(&self.value)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
GetProperty(entity: Entity, key: String255, authorization: Token) -> Value,
{
}
}
impl<T: SessionTrait> GetProperty<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::Property)
.with_key(self.key)
.await
}
}
decl_call! {
ClearProperty(entity: Entity, key: String255, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> ClearProperty<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::Property)
.with_key(self.key)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
PropertyCount(entity: Entity, authorization: Token) -> i64,
{
}
}
impl<T: SessionTrait> PropertyCount<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::Property)
.await?;
Ok(res.integer()?)
}
}
decl_call! {
PropertyMember(entity: Entity, key: String255, authorization: Token) -> bool,
{
}
}
impl<T: SessionTrait> PropertyMember<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::Property)
.with_key(self.key)
.await?;
Ok(res.boolean()?)
}
}
decl_call! {
PropertyName(entity: Entity, key: String255, authorization: Token) -> String255,
{
index: i64,
}
}
impl<T: SessionTrait> PropertyName<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::Property)
.with_key(self.key)
.with_index(self.index)
.await?;
Ok(res
.string()?
.try_into()
.map_err(|e| CallError::InvalidResponse(InvalidResponse::Name(e)))?)
}
}
decl_call! {
PropertyKey(entity: Entity, index: i64, authorization: Token) -> String255,
{
}
}
impl<T: SessionTrait> PropertyKey<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::Property)
.with_index(self.index)
.await?;
Ok(res
.string()?
.try_into()
.map_err(|e| CallError::InvalidResponse(InvalidResponse::Key(e)))?)
}
}
decl_call! {
PropertyValue(entity: Entity, key: String255, authorization: Token) -> Value,
{
index: i64,
}
}
impl<T: SessionTrait> PropertyValue<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::Property)
.with_key(self.key)
.with_index(self.index)
.await
}
}
decl_call! {
PropertyIndex(entity: Entity, key: String255, authorization: Token) -> i64,
{
}
}
impl<T: SessionTrait> PropertyIndex<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::Property)
.with_key(self.key)
.await?;
Ok(res.integer()?)
}
}
decl_call! {
PropertyAttribute(entity: Entity, authorization: Token) -> Attribute,
{
key: String255,
index: i64,
}
}
impl<T: SessionTrait> PropertyAttribute<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::Property)
.with_key(self.key)
.with_index(self.index)
.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! {
SortProperties(entity: Entity, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> SortProperties<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::Property)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
PurgeProperties(entity: Entity, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> PurgeProperties<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::Property)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
RetrieveProperties(entity: Entity, authorization: Token) -> String,
{
}
}
impl<T: SessionTrait> RetrieveProperties<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::Property)
.await?;
Ok(res.interchange()?)
}
}