Add doc about Ping cmd to README, change the internal data type of Ping cmd to Bytes, and rm unnecessary reference (#118)

This commit is contained in:
Ting Sun
2023-03-28 22:53:33 +08:00
committed by GitHub
parent aab4e39d97
commit b1e365b62f
7 changed files with 14 additions and 12 deletions

View File

@@ -28,7 +28,8 @@ struct Cli {
enum Command {
Ping {
/// Message to ping
msg: Option<String>,
#[clap(parse(from_str = bytes_from_str))]
msg: Option<Bytes>,
},
/// Get the value of key.
Get {

View File

@@ -110,7 +110,7 @@ impl Client {
/// }
/// ```
#[instrument(skip(self))]
pub async fn ping(&mut self, msg: Option<String>) -> crate::Result<Bytes> {
pub async fn ping(&mut self, msg: Option<Bytes>) -> crate::Result<Bytes> {
let frame = Ping::new(msg).into_frame();
debug!(request = ?frame);
@@ -459,7 +459,7 @@ impl Subscriber {
/// Unsubscribe to a list of new channels
#[instrument(skip(self))]
pub async fn unsubscribe(&mut self, channels: &[String]) -> crate::Result<()> {
let frame = Unsubscribe::new(&channels).into_frame();
let frame = Unsubscribe::new(channels).into_frame();
debug!(request = ?frame);

View File

@@ -1,6 +1,6 @@
use crate::{Connection, Frame, Parse, ParseError};
use bytes::Bytes;
use tracing::instrument;
use tracing::{debug, instrument};
/// Returns PONG if no argument is provided, otherwise
/// return a copy of the argument as a bulk.
@@ -10,12 +10,12 @@ use tracing::instrument;
#[derive(Debug, Default)]
pub struct Ping {
/// optional message to be returned
msg: Option<String>,
msg: Option<Bytes>,
}
impl Ping {
/// Create a new `Ping` command with optional `msg`.
pub fn new(msg: Option<String>) -> Ping {
pub fn new(msg: Option<Bytes>) -> Ping {
Ping { msg }
}
@@ -40,7 +40,7 @@ impl Ping {
/// PING [message]
/// ```
pub(crate) fn parse_frames(parse: &mut Parse) -> crate::Result<Ping> {
match parse.next_string() {
match parse.next_bytes() {
Ok(msg) => Ok(Ping::new(Some(msg))),
Err(ParseError::EndOfStream) => Ok(Ping::default()),
Err(e) => Err(e.into()),
@@ -55,9 +55,11 @@ impl Ping {
pub(crate) async fn apply(self, dst: &mut Connection) -> crate::Result<()> {
let response = match self.msg {
None => Frame::Simple("PONG".to_string()),
Some(msg) => Frame::Bulk(Bytes::from(msg)),
Some(msg) => Frame::Bulk(msg),
};
debug!(?response);
// Write the response back to the client
dst.write_frame(&response).await?;
@@ -72,7 +74,7 @@ impl Ping {
let mut frame = Frame::array();
frame.push_bulk(Bytes::from("ping".as_bytes()));
if let Some(msg) = self.msg {
frame.push_bulk(Bytes::from(msg));
frame.push_bulk(msg);
}
frame
}