embedded_redis/commands/ping.rs
1//! Abstraction of PING command.
2//!
3//! For general information about this command, see the [Redis documentation](<https://redis.io/commands/ping/>).
4//!
5//! # Basic usage (client shorthand)
6//! Internally it is checked whether the server answers with PONG. If not, an error is returned.
7//! ```
8//! # async_std::task::block_on(async {
9//!# use core::str::FromStr;
10//!# use core::net::SocketAddr;
11//!# use std_embedded_nal_async::Stack;
12//!# use std_embedded_time::StandardClock;
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 response = client.ping().await.unwrap();
22//! # });
23//! ```
24//! # Verbose command
25//! Sending a `PingCommand` as alternative to client shorthand.
26//! ```
27//! # async_std::task::block_on(async {
28//!# use core::str::FromStr;
29//!# use core::net::SocketAddr;
30//!# use std_embedded_nal_async::Stack;
31//!# use std_embedded_time::StandardClock;
32//!# use embedded_redis::commands::ping::PingCommand;
33//!# use embedded_redis::network::ConnectionHandler;
34//!#
35//!# let 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(&stack, Some(&clock)).await.unwrap();
40//!#
41//! let command = PingCommand::new(None);
42//! let response = client.send(command).await.unwrap();
43//! # });
44//! ```
45//! # Custom argument
46//! Optionally, a user-defined argument can be specified.
47//!
48//! The abstraction compares the server's response with the argument and returns an error if there is no match.
49//! ```
50//! # async_std::task::block_on(async {
51//!# use core::str::FromStr;
52//!# use core::net::SocketAddr;
53//!# use std_embedded_nal_async::Stack;
54//!# use std_embedded_time::StandardClock;
55//!# use embedded_redis::commands::ping::PingCommand;
56//!# use embedded_redis::network::ConnectionHandler;
57//!#
58//!# let stack = Stack::default();
59//!# let clock = StandardClock::default();
60//!#
61//!# let mut connection_handler = ConnectionHandler::resp2(SocketAddr::from_str("127.0.0.1:6379").unwrap());
62//!# let client = connection_handler.connect(&stack, Some(&clock)).await.unwrap();
63//!#
64//! let command = PingCommand::new(Some("hello world".into()));
65//! let response = client.send(command).await.unwrap();
66//! # });
67//! ```
68use crate::commands::auth::AuthCommand;
69use crate::commands::builder::{CommandBuilder, ToStringOption};
70use crate::commands::hello::HelloCommand;
71use crate::commands::{Command, ResponseTypeError};
72use crate::network::protocol::Protocol;
73use crate::network::{Client, CommandErrors};
74use bytes::Bytes;
75use embedded_io_async::{Read, Write};
76use embedded_time::Clock;
77
78/// Abstraction for PING command
79pub struct PingCommand {
80 argument: Option<Bytes>,
81}
82
83impl PingCommand {
84 pub fn new(argument: Option<Bytes>) -> Self {
85 PingCommand { argument }
86 }
87}
88
89static PONG: Bytes = Bytes::from_static(b"PONG");
90
91impl<F> Command<F> for PingCommand
92where
93 F: From<CommandBuilder> + ToStringOption,
94{
95 type Response = ();
96
97 fn encode(&self) -> F {
98 CommandBuilder::new("PING").arg_option(self.argument.as_ref()).into()
99 }
100
101 fn eval_response(&self, frame: F) -> Result<Self::Response, ResponseTypeError> {
102 let response = frame.to_string_option().ok_or(ResponseTypeError {})?;
103 let pong = &PONG;
104 let expected = self.argument.as_ref().unwrap_or(pong);
105
106 if response.as_bytes() != expected.as_ref() {
107 return Err(ResponseTypeError {});
108 }
109
110 Ok(())
111 }
112}
113
114impl<'a, T: Read + Write, C: Clock, P: Protocol> Client<'a, T, C, P>
115where
116 AuthCommand: Command<<P as Protocol>::FrameType>,
117 HelloCommand: Command<<P as Protocol>::FrameType>,
118{
119 /// Shorthand for [PingCommand]
120 pub async fn ping(&self) -> Result<(), CommandErrors>
121 where
122 <P as Protocol>::FrameType: ToStringOption,
123 <P as Protocol>::FrameType: From<CommandBuilder>,
124 {
125 self.send(PingCommand::new(None)).await
126 }
127}