use futures::{future::BoxFuture, Stream};
use serde::{Deserialize, Serialize};
use tower::layer::util::Identity;
use std::{fmt::Debug, pin::Pin};
use crate::{
data::Extensions,
error::Error,
poller::Poller,
task::{attempt::Attempt, namespace::Namespace, task_id::TaskId},
worker::WorkerId,
Backend,
};
#[derive(Serialize, Debug, Deserialize, Clone, Default)]
pub struct Request<Args, Ctx> {
pub args: Args,
pub parts: Parts<Ctx>,
}
#[non_exhaustive]
#[derive(Serialize, Debug, Deserialize, Clone, Default)]
pub struct Parts<Ctx> {
pub task_id: TaskId,
#[serde(skip)]
pub data: Extensions,
pub attempt: Attempt,
pub context: Ctx,
#[serde(skip)]
pub namespace: Option<Namespace>,
}
impl<T, Ctx: Default> Request<T, Ctx> {
pub fn new(args: T) -> Self {
Self::new_with_data(args, Extensions::default(), Ctx::default())
}
pub fn new_with_parts(args: T, parts: Parts<Ctx>) -> Self {
Self { args, parts }
}
pub fn new_with_ctx(req: T, ctx: Ctx) -> Self {
Self {
args: req,
parts: Parts {
context: ctx,
..Default::default()
},
}
}
pub fn new_with_data(req: T, data: Extensions, ctx: Ctx) -> Self {
Self {
args: req,
parts: Parts {
context: ctx,
data,
..Default::default()
},
}
}
pub fn take_parts(self) -> (T, Parts<Ctx>) {
(self.args, self.parts)
}
}
impl<T, Ctx> std::ops::Deref for Request<T, Ctx> {
type Target = Extensions;
fn deref(&self) -> &Self::Target {
&self.parts.data
}
}
impl<T, Ctx> std::ops::DerefMut for Request<T, Ctx> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.parts.data
}
}
pub type BoxStream<'a, T> = Pin<Box<dyn Stream<Item = T> + Send + 'a>>;
pub type RequestFuture<T> = BoxFuture<'static, T>;
pub type RequestStream<T> = BoxStream<'static, Result<Option<T>, Error>>;
impl<T, Res, Ctx> Backend<Request<T, Ctx>, Res> for RequestStream<Request<T, Ctx>> {
type Stream = Self;
type Layer = Identity;
fn poll<Svc>(self, _worker: WorkerId) -> Poller<Self::Stream> {
Poller {
stream: self,
heartbeat: Box::pin(async {}),
layer: Identity::new(),
}
}
}