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