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! {
InsertFact(entity: Entity, attribute: Attribute, value: Value, authorization: Token) -> (),
{
index: i64,
deferred: bool,
}
}
impl<T: SessionTrait> InsertFact<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::Fact)
.with_attribute(self.attribute)
.with_value(&self.value)
.with_index(self.index)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
RemoveFact(entity: Entity, index: i64, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> RemoveFact<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::Fact)
.with_index(self.index)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
ReplaceFact(entity: Entity, index: i64, attribute: Attribute, value: Value, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> ReplaceFact<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::Fact)
.with_attribute(self.attribute)
.with_value(&self.value)
.with_index(self.index)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
FindFact(entity: Entity, value: Value, authorization: Token) -> i64,
{
index: i64,
}
}
impl<T: SessionTrait> FindFact<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::Fact)
.with_value(&self.value)
.with_index(self.index)
.await?;
Ok(res.integer()?)
}
}
decl_call! {
IncludeFact(entity: Entity, attribute: Attribute, value: Value, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> IncludeFact<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::Fact)
.with_attribute(self.attribute)
.with_value(&self.value)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
ExcludeFact(entity: Entity, attribute: Attribute, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> ExcludeFact<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::Fact)
.with_attribute(self.attribute)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
SetFact(entity: Entity, attribute: Attribute, value: Value, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> SetFact<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::Fact)
.with_attribute(self.attribute)
.with_value(&self.value)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
GetFact(entity: Entity, attribute: Attribute, authorization: Token) -> Value,
{
}
}
impl<T: SessionTrait> GetFact<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::Fact)
.with_attribute(self.attribute)
.await
}
}
decl_call! {
ClearFact(entity: Entity, attribute: Attribute, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> ClearFact<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::Fact)
.with_attribute(self.attribute)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
FactCount(entity: Entity, authorization: Token) -> i64,
{
}
}
impl<T: SessionTrait> FactCount<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::Fact)
.await?;
Ok(res.integer()?)
}
}
decl_call! {
FactMember(entity: Entity, attribute: Attribute, authorization: Token) -> bool,
{
}
}
impl<T: SessionTrait> FactMember<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::Fact)
.with_attribute(self.attribute)
.await?;
Ok(res.boolean()?)
}
}
decl_call! {
FactName(entity: Entity, attribute: Attribute, authorization: Token) -> String255,
{
index: i64,
}
}
impl<T: SessionTrait> FactName<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::Fact)
.with_attribute(self.attribute)
.with_index(self.index)
.await?;
Ok(res
.string()?
.try_into()
.map_err(|e| CallError::InvalidResponse(InvalidResponse::Name(e)))?)
}
}
decl_call! {
FactKey(entity: Entity, attribute: Attribute, authorization: Token) -> String255,
{
index: i64,
}
}
impl<T: SessionTrait> FactKey<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::Fact)
.with_attribute(self.attribute)
.with_index(self.index)
.await?;
Ok(res
.string()?
.try_into()
.map_err(|e| CallError::InvalidResponse(InvalidResponse::Key(e)))?)
}
}
decl_call! {
FactValue(entity: Entity, attribute: Attribute, authorization: Token) -> Value,
{
index: i64,
}
}
impl<T: SessionTrait> FactValue<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::Fact)
.with_attribute(self.attribute)
.with_index(self.index)
.await
}
}
decl_call! {
FactIndex(entity: Entity, attribute: Attribute, authorization: Token) -> i64,
{
}
}
impl<T: SessionTrait> FactIndex<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::Fact)
.with_attribute(self.attribute)
.await?;
Ok(res.integer()?)
}
}
decl_call! {
FactAttribute(entity: Entity, index: i64, authorization: Token) -> Attribute,
{
}
}
impl<T: SessionTrait> FactAttribute<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::Fact)
.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! {
SortFacts(entity: Entity, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> SortFacts<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::Fact)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
PurgeFacts(entity: Entity, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> PurgeFacts<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::Fact)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
RetrieveFacts(entity: Entity, authorization: Token) -> String,
{
}
}
impl<T: SessionTrait> RetrieveFacts<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::Fact)
.await?;
Ok(res.interchange()?)
}
}