use crate::commands::auth::AuthCommand;
use crate::commands::builder::{CommandBuilder, ToInteger};
use crate::commands::hello::HelloCommand;
use crate::commands::{Command, ResponseTypeError};
use crate::network::client::{Client, CommandErrors};
use crate::network::future::Future;
use crate::network::protocol::Protocol;
use bytes::Bytes;
use embedded_nal::TcpClientStack;
use embedded_time::Clock;
pub struct PublishCommand {
channel: Bytes,
message: Bytes,
}
impl PublishCommand {
pub fn new<C, M>(channel: C, message: M) -> Self
where
Bytes: From<C>,
Bytes: From<M>,
{
PublishCommand {
channel: channel.into(),
message: message.into(),
}
}
}
impl<F> Command<F> for PublishCommand
where
F: From<CommandBuilder> + ToInteger,
{
type Response = i64;
fn encode(&self) -> F {
CommandBuilder::new("PUBLISH").arg(&self.channel).arg(&self.message).into()
}
fn eval_response(&self, frame: F) -> Result<Self::Response, ResponseTypeError> {
frame.to_integer().ok_or(ResponseTypeError {})
}
}
impl<'a, N: TcpClientStack, C: Clock, P: Protocol> Client<'a, N, C, P>
where
AuthCommand: Command<<P as Protocol>::FrameType>,
HelloCommand: Command<<P as Protocol>::FrameType>,
{
pub fn publish<K, V>(
&'a self,
channel: K,
message: V,
) -> Result<Future<'a, N, C, P, PublishCommand>, CommandErrors>
where
<P as Protocol>::FrameType: ToInteger,
<P as Protocol>::FrameType: From<CommandBuilder>,
Bytes: From<K>,
Bytes: From<V>,
{
self.send(PublishCommand::new(channel, message))
}
}