use std::fmt::{self, Debug};
use crate::{
capability::Operation,
core::resolve::{RequestHandle, Resolvable, ResolveError},
};
pub struct Request<Op>
where
Op: Operation,
{
pub operation: Op,
pub handle: RequestHandle<Op::Output>,
}
impl<Op> Request<Op>
where
Op: Operation,
{
pub fn resolve(&mut self, output: Op::Output) -> Result<(), ResolveError> {
self.handle.resolve(output)
}
pub fn split(self) -> (Op, RequestHandle<Op::Output>) {
(self.operation, self.handle)
}
pub(crate) fn resolves_never(operation: Op) -> Self {
Self {
operation,
handle: RequestHandle::Never,
}
}
pub(crate) fn resolves_once<F>(operation: Op, resolve: F) -> Self
where
F: FnOnce(Op::Output) + Send + 'static,
{
Self {
operation,
handle: RequestHandle::Once(Box::new(resolve)),
}
}
pub(crate) fn resolves_many_times<F>(operation: Op, resolve: F) -> Self
where
F: Fn(Op::Output) -> Result<(), ()> + Send + 'static,
{
Self {
operation,
handle: RequestHandle::Many(Box::new(resolve)),
}
}
}
impl<Op> Resolvable<Op::Output> for Request<Op>
where
Op: Operation,
{
fn resolve(&mut self, output: Op::Output) -> Result<(), ResolveError> {
self.resolve(output)
}
}
impl<Op> fmt::Debug for Request<Op>
where
Op: Operation + Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Request").field(&self.operation).finish()
}
}