use harmos_domain::operations::storage::WriteSchema;
use std::sync::Arc;
use crate::operations::{Context, Operation};
use crate::storage::StorageCursor;
use crate::storage::StorageManager;
use crate::{Error, Result};
pub(super) struct Handler<C> {
storage: Arc<StorageManager<C>>,
}
impl<C: 'static> Operation for Handler<C> {
type Declaration = WriteSchema;
type Resources = StorageManager<C>;
type Error = Error;
type Context<'a> = Context<'a>;
type Endpoints = ();
async fn initialize<'ctx>(storage: Arc<StorageManager<C>>, _: Self::Context<'ctx>) -> Result<Self> {
Ok(Self { storage })
}
async fn run<'a>(
&mut self, _: Self::Context<'a>, request: &mut WriteSchema, _: &mut Self::Endpoints,
) -> Result<crate::operations::Response<StorageCursor>> {
let result: ::core::result::Result<StorageCursor, Self::Error> = async move {
self.storage.write_schema_once(&request.mutation, &request.schema.key, request.schema.graph()).await.map_err(Into::into)
}
.await;
result.map(::core::convert::Into::into)
}
fn classify(error: &Error) -> crate::operations::FailureKind {
super::classify(error)
}
}