use crate::codec::Codec;
use crate::runtime::input::{Decoded, DecodedPair, Provided};
use crate::runtime::publish::{RawReplyWiring, ReplyWiring};
#[cfg(any(feature = "json", feature = "cbor", feature = "msgpack"))]
use crate::codec::DefaultCodec;
pub trait IncludeDef {
type Form;
}
#[diagnostic::on_unimplemented(
message = "no codec is available to decode this subscriber's input",
label = "nothing in this chain names a codec",
note = "enable a codec feature on `ruststream` (`json`, `cbor` or `msgpack`), name one for \
the scope (`with_broker_codec(broker, JsonCodec, |b| ..)`) or the registration \
(`.codec(JsonCodec)`), or give the input type its own decoding with \
`#[derive(Deserialized)]` so no codec is needed"
)]
#[doc(hidden)]
pub trait MountCodec {
type Codec: Codec + Clone + Send + Sync + 'static;
fn mount_codec(&self) -> Self::Codec;
}
#[cfg(any(feature = "json", feature = "cbor", feature = "msgpack"))]
impl MountCodec for () {
type Codec = DefaultCodec;
fn mount_codec(&self) -> Self::Codec {
DefaultCodec::default()
}
}
impl<C: Codec + Clone + Send + Sync + 'static> MountCodec for C {
type Codec = C;
fn mount_codec(&self) -> Self::Codec {
self.clone()
}
}
#[diagnostic::on_unimplemented(
message = "no codec is available to decode this subscriber's input",
label = "nothing in this chain names a codec",
note = "enable a codec feature on `ruststream` (`json`, `cbor` or `msgpack`), name one for \
the scope (`with_broker_codec(broker, JsonCodec, |b| ..)`) or the registration \
(`.codec(JsonCodec)`), or give the input type its own decoding with \
`#[derive(Deserialized)]` so no codec is needed"
)]
#[doc(hidden)]
pub trait InputCodec<Input> {
type Codec: Clone + Send + Sync + 'static;
fn input_codec(&self) -> Self::Codec;
}
impl<C: MountCodec, T> InputCodec<Decoded<T>> for C {
type Codec = <C as MountCodec>::Codec;
fn input_codec(&self) -> Self::Codec {
self.mount_codec()
}
}
impl<C: MountCodec, H, P> InputCodec<DecodedPair<H, P>> for C {
type Codec = <C as MountCodec>::Codec;
fn input_codec(&self) -> Self::Codec {
self.mount_codec()
}
}
impl<C, F> InputCodec<Provided<F>> for C {
type Codec = ();
fn input_codec(&self) {}
}
#[doc(hidden)]
#[derive(Debug, Clone, Copy, Default)]
pub struct DefaultReply;
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct PublishMount;
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct RawReplyMount;
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct InjectMount;
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct BatchPublishMount;
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct BatchInjectMount;
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct PublishInjectMount;
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct RawReplyInjectMount;
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct BatchPublishInjectMount;
#[doc(hidden)]
pub trait ReplyAttachment<Policy> {
type Wiring;
fn wire(policy: Policy) -> Self::Wiring;
}
macro_rules! impl_encoded_reply_attachment {
($($mount:ident),+ $(,)?) => {$(
impl<Policy> ReplyAttachment<Policy> for $mount {
type Wiring = ReplyWiring<Policy>;
fn wire(policy: Policy) -> ReplyWiring<Policy> {
ReplyWiring::new(policy)
}
}
)+};
}
impl_encoded_reply_attachment!(
PublishMount,
PublishInjectMount,
BatchPublishMount,
BatchPublishInjectMount,
);
macro_rules! impl_raw_reply_attachment {
($($mount:ident),+ $(,)?) => {$(
impl<Policy> ReplyAttachment<Policy> for $mount {
type Wiring = RawReplyWiring<Policy>;
fn wire(policy: Policy) -> RawReplyWiring<Policy> {
RawReplyWiring::new(policy)
}
}
)+};
}
impl_raw_reply_attachment!(RawReplyMount, RawReplyInjectMount);
#[doc(hidden)]
#[diagnostic::on_unimplemented(
message = "this definition's form cannot be mounted on a router",
label = "the form token `{Self}` has no router mount",
note = "every `#[subscriber]` form mounts with .include(..); check that the definition is a \
generated one and that its broker matches the router's"
)]
pub trait RouterMount<R, Def> {
type Out;
fn begin(def: Def, router: R) -> Self::Out;
}