use std::sync::Arc;
use uuid::Uuid;
use crate::Result;
use crate::label::Label;
use crate::name::ResourceName;
use crate::object::{Association, Object};
#[async_trait::async_trait]
pub trait ObjectStoreReader<L: Label>: Send + Sync + 'static {
async fn get(&self, id: &Uuid) -> Result<Object<L>>;
async fn get_by_name(&self, label: L, name: &ResourceName) -> Result<Object<L>>;
async fn list(
&self,
label: L,
namespace: Option<&ResourceName>,
max_results: Option<usize>,
page_token: Option<String>,
) -> Result<(Vec<Object<L>>, Option<String>)>;
}
#[async_trait::async_trait]
pub trait ObjectStore<L: Label>: ObjectStoreReader<L> + Send + Sync + 'static {
async fn create(
&self,
label: L,
name: &ResourceName,
properties: Option<serde_json::Value>,
id: Option<Uuid>,
) -> Result<Object<L>>;
async fn update(&self, id: &Uuid, properties: Option<serde_json::Value>) -> Result<Object<L>>;
async fn delete(&self, id: &Uuid) -> Result<()>;
}
#[async_trait::async_trait]
pub trait AssociationStoreReader<L: Label>: Send + Sync + 'static {
async fn list(
&self,
from_id: Uuid,
label: &str,
target_label: Option<L>,
max_results: Option<usize>,
page_token: Option<String>,
) -> Result<(Vec<Association<L>>, Option<String>)>;
}
#[async_trait::async_trait]
pub trait AssociationStore<L: Label>: AssociationStoreReader<L> + Send + Sync + 'static {
async fn add(
&self,
from_id: Uuid,
to_id: Uuid,
label: &str,
properties: Option<serde_json::Value>,
) -> Result<()>;
async fn remove(&self, from_id: Uuid, to_id: Uuid, label: &str) -> Result<()>;
}
#[async_trait::async_trait]
impl<L: Label, T: ObjectStoreReader<L>> ObjectStoreReader<L> for Arc<T> {
async fn get(&self, id: &Uuid) -> Result<Object<L>> {
T::get(self, id).await
}
async fn get_by_name(&self, label: L, name: &ResourceName) -> Result<Object<L>> {
T::get_by_name(self, label, name).await
}
async fn list(
&self,
label: L,
namespace: Option<&ResourceName>,
max_results: Option<usize>,
page_token: Option<String>,
) -> Result<(Vec<Object<L>>, Option<String>)> {
T::list(self, label, namespace, max_results, page_token).await
}
}
#[async_trait::async_trait]
impl<L: Label, T: ObjectStore<L>> ObjectStore<L> for Arc<T> {
async fn create(
&self,
label: L,
name: &ResourceName,
properties: Option<serde_json::Value>,
id: Option<Uuid>,
) -> Result<Object<L>> {
T::create(self, label, name, properties, id).await
}
async fn update(&self, id: &Uuid, properties: Option<serde_json::Value>) -> Result<Object<L>> {
T::update(self, id, properties).await
}
async fn delete(&self, id: &Uuid) -> Result<()> {
T::delete(self, id).await
}
}
#[async_trait::async_trait]
impl<L: Label, T: AssociationStoreReader<L>> AssociationStoreReader<L> for Arc<T> {
async fn list(
&self,
from_id: Uuid,
label: &str,
target_label: Option<L>,
max_results: Option<usize>,
page_token: Option<String>,
) -> Result<(Vec<Association<L>>, Option<String>)> {
T::list(self, from_id, label, target_label, max_results, page_token).await
}
}
#[async_trait::async_trait]
impl<L: Label, T: AssociationStore<L>> AssociationStore<L> for Arc<T> {
async fn add(
&self,
from_id: Uuid,
to_id: Uuid,
label: &str,
properties: Option<serde_json::Value>,
) -> Result<()> {
T::add(self, from_id, to_id, label, properties).await
}
async fn remove(&self, from_id: Uuid, to_id: Uuid, label: &str) -> Result<()> {
T::remove(self, from_id, to_id, label).await
}
}