embedded_redis/commands/
publish.rs

1//! Abstraction of PUBLISH command.
2//!
3//! For general information about this command, see the [Redis documentation](<https://redis.io/commands/publish/>).
4//!
5//! # Using command object
6//! ```
7//!# use core::str::FromStr;
8//!# use core::net::SocketAddr;
9//!# use std_embedded_nal::Stack;
10//!# use std_embedded_time::StandardClock;
11//!# use embedded_redis::commands::publish::PublishCommand;
12//!# use embedded_redis::network::ConnectionHandler;
13//!#
14//! let mut stack = Stack::default();
15//! let clock = StandardClock::default();
16//!
17//! let mut connection_handler = ConnectionHandler::resp2(SocketAddr::from_str("127.0.0.1:6379").unwrap());
18//! let client = connection_handler.connect(&mut stack, Some(&clock)).unwrap();
19//!
20//! let command = PublishCommand::new("channel", "message");
21//! let response = client.send(command).unwrap().wait().unwrap();
22//!
23//! // Returns the number of clients that received the message
24//! assert_eq!(0, response)
25//! ```
26//! # Shorthand
27//! [Client](Client#method.publish) provides a shorthand method.
28//! ```
29//!# use core::str::FromStr;
30//!# use core::net::SocketAddr;
31//!# use std_embedded_nal::Stack;
32//!# use std_embedded_time::StandardClock;
33//!# use embedded_redis::network::ConnectionHandler;
34//!#
35//!# let mut stack = Stack::default();
36//!# let clock = StandardClock::default();
37//!#
38//!# let mut connection_handler = ConnectionHandler::resp2(SocketAddr::from_str("127.0.0.1:6379").unwrap());
39//!# let client = connection_handler.connect(&mut stack, Some(&clock)).unwrap();
40//!#
41//! let _ = client.publish("channel", "message");
42//! ```
43use crate::commands::auth::AuthCommand;
44use crate::commands::builder::{CommandBuilder, ToInteger};
45use crate::commands::hello::HelloCommand;
46use crate::commands::{Command, ResponseTypeError};
47use crate::network::client::{Client, CommandErrors};
48use crate::network::future::Future;
49use crate::network::protocol::Protocol;
50use bytes::Bytes;
51use embedded_nal::TcpClientStack;
52use embedded_time::Clock;
53
54/// Abstraction for PUBLISH command
55pub struct PublishCommand {
56    channel: Bytes,
57    message: Bytes,
58}
59
60impl PublishCommand {
61    pub fn new<C, M>(channel: C, message: M) -> Self
62    where
63        Bytes: From<C>,
64        Bytes: From<M>,
65    {
66        PublishCommand {
67            channel: channel.into(),
68            message: message.into(),
69        }
70    }
71}
72
73impl<F> Command<F> for PublishCommand
74where
75    F: From<CommandBuilder> + ToInteger,
76{
77    /// the number of clients that received the message
78    type Response = i64;
79
80    fn encode(&self) -> F {
81        CommandBuilder::new("PUBLISH").arg(&self.channel).arg(&self.message).into()
82    }
83
84    fn eval_response(&self, frame: F) -> Result<Self::Response, ResponseTypeError> {
85        frame.to_integer().ok_or(ResponseTypeError {})
86    }
87}
88
89impl<'a, N: TcpClientStack, C: Clock, P: Protocol> Client<'a, N, C, P>
90where
91    AuthCommand: Command<<P as Protocol>::FrameType>,
92    HelloCommand: Command<<P as Protocol>::FrameType>,
93{
94    /// Shorthand for [PublishCommand]
95    pub fn publish<K, V>(
96        &'a self,
97        channel: K,
98        message: V,
99    ) -> Result<Future<'a, N, C, P, PublishCommand>, CommandErrors>
100    where
101        <P as Protocol>::FrameType: ToInteger,
102        <P as Protocol>::FrameType: From<CommandBuilder>,
103        Bytes: From<K>,
104        Bytes: From<V>,
105    {
106        self.send(PublishCommand::new(channel, message))
107    }
108}