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! {
InsertAnnotation(entity: Entity, key: String255, index: i64, attribute: Attribute, value: Value, authorization: Token) -> (),
{
instance: i32,
deferred: bool,
}
}
impl<T: SessionTrait> InsertAnnotation<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::Annotation)
.with_attribute(self.attribute)
.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! {
RemoveAnnotation(entity: Entity, key: String255, authorization: Token) -> (),
{
instance: i32,
index: i64,
deferred: bool,
}
}
impl<T: SessionTrait> RemoveAnnotation<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::Annotation)
.with_key(self.key)
.with_index(self.index)
.with_instance(self.instance)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
ReplaceAnnotation(entity: Entity, key: String255, attribute: Attribute, value: Value, authorization: Token) -> (),
{
instance: i32,
index: i64,
deferred: bool,
}
}
impl<T: SessionTrait> ReplaceAnnotation<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::Annotation)
.with_attribute(self.attribute)
.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! {
FindAnnotation(entity: Entity, key: String255, value: Value, authorization: Token) -> i64,
{
instance: i32,
index: i64,
}
}
impl<T: SessionTrait> FindAnnotation<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::Annotation)
.with_key(self.key)
.with_value(&self.value)
.with_index(self.index)
.with_instance(self.instance)
.await?;
Ok(res.integer()?)
}
}
decl_call! {
IncludeAnnotation(entity: Entity, key: String255, attribute: Attribute, value: Value, authorization: Token) -> (),
{
instance: i32,
deferred: bool,
}
}
impl<T: SessionTrait> IncludeAnnotation<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::Annotation)
.with_attribute(self.attribute)
.with_key(self.key)
.with_value(&self.value)
.with_instance(self.instance)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
ExcludeAnnotation(entity: Entity, key: String255, attribute: Attribute, authorization: Token) -> (),
{
instance: i32,
deferred: bool,
}
}
impl<T: SessionTrait> ExcludeAnnotation<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::Annotation)
.with_attribute(self.attribute)
.with_key(self.key)
.with_instance(self.instance)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
SetAnnotation(entity: Entity, key: String255, attribute: Attribute, value: Value, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> SetAnnotation<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::Annotation)
.with_attribute(self.attribute)
.with_key(self.key)
.with_value(&self.value)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
GetAnnotation(entity: Entity, key: String255, attribute: Attribute, authorization: Token) -> Value,
{
}
}
impl<T: SessionTrait> GetAnnotation<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::Annotation)
.with_attribute(self.attribute)
.with_key(self.key)
.await
}
}
decl_call! {
ClearAnnotation(entity: Entity, key: String255, attribute: Attribute, authorization: Token) -> (),
{
deferred: bool,
}
}
impl<T: SessionTrait> ClearAnnotation<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::Annotation)
.with_attribute(self.attribute)
.with_key(self.key)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
AnnotationCount(entity: Entity, key: String255, authorization: Token) -> i64,
{
instance: i32,
}
}
impl<T: SessionTrait> AnnotationCount<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::Annotation)
.with_key(self.key)
.with_instance(self.instance)
.await?;
Ok(res.integer()?)
}
}
decl_call! {
AnnotationMember(entity: Entity, key: String255, attribute: Attribute, authorization: Token) -> bool,
{
instance: i32,
}
}
impl<T: SessionTrait> AnnotationMember<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::Annotation)
.with_attribute(self.attribute)
.with_key(self.key)
.with_instance(self.instance)
.await?;
Ok(res.boolean()?)
}
}
decl_call! {
AnnotationName(entity: Entity, key: String255, attribute: Attribute, authorization: Token) -> String255,
{
instance: i32,
index: i64,
}
}
impl<T: SessionTrait> AnnotationName<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::Annotation)
.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! {
AnnotationKey(entity: Entity, attribute: Attribute, authorization: Token) -> String255,
{
instance: i32,
index: i64,
}
}
impl<T: SessionTrait> AnnotationKey<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::Annotation)
.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! {
AnnotationValue(entity: Entity, key: String255, attribute: Attribute, authorization: Token) -> Value,
{
instance: i32,
index: i64,
}
}
impl<T: SessionTrait> AnnotationValue<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::Annotation)
.with_attribute(self.attribute)
.with_key(self.key)
.with_index(self.index)
.with_instance(self.instance)
.await
}
}
decl_call! {
AnnotationIndex(entity: Entity, key: String255, attribute: Attribute, authorization: Token) -> i64,
{
instance: i32,
}
}
impl<T: SessionTrait> AnnotationIndex<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::Annotation)
.with_attribute(self.attribute)
.with_key(self.key)
.with_instance(self.instance)
.await?;
Ok(res.integer()?)
}
}
decl_call! {
AnnotationAttribute(entity: Entity, key: String255, instance: i32, authorization: Token) -> Attribute,
{
index: i64,
}
}
impl<T: SessionTrait> AnnotationAttribute<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::Annotation)
.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! {
PurgeAnnotations(entity: Entity, key: String255, authorization: Token) -> (),
{
instance: i32,
deferred: bool,
}
}
impl<T: SessionTrait> PurgeAnnotations<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::Annotation)
.with_key(self.key)
.with_instance(self.instance)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
SortAnnotations(entity: Entity, key: String255, authorization: Token) -> (),
{
instance: i32,
deferred: bool,
}
}
impl<T: SessionTrait> SortAnnotations<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::Annotation)
.with_key(self.key)
.with_instance(self.instance)
.with_parameter(self.deferred as i64)
.await?;
Ok(())
}
}
decl_call! {
RetrieveAnnotations(entity: Entity, key: String255, authorization: Token) -> String,
{
instance: i32,
}
}
impl<T: SessionTrait> RetrieveAnnotations<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::Annotation)
.with_key(self.key)
.with_instance(self.instance)
.await?;
Ok(res.interchange()?)
}
}