implement client set (#11)
This commit is contained in:
@@ -1,21 +1,32 @@
|
||||
use crate::cmd::{Parse, ParseError};
|
||||
use crate::{Connection, Frame, Db};
|
||||
use crate::cmd::{
|
||||
utils::{bytes_from_str, duration_from_ms_str},
|
||||
Parse, ParseError,
|
||||
};
|
||||
use crate::{Connection, Db, Frame};
|
||||
use clap::Clap;
|
||||
|
||||
use bytes::Bytes;
|
||||
use std::io;
|
||||
use std::time::Duration;
|
||||
use tracing::{debug, instrument};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Clap, Debug)]
|
||||
pub struct Set {
|
||||
key: String,
|
||||
value: Bytes,
|
||||
expire: Option<Duration>,
|
||||
/// the lookup key
|
||||
pub(crate) key: String,
|
||||
|
||||
/// the value to be stored
|
||||
#[clap(parse(from_str = bytes_from_str))]
|
||||
pub(crate) value: Bytes,
|
||||
|
||||
/// duration in milliseconds
|
||||
#[clap(parse(try_from_str = duration_from_ms_str))]
|
||||
pub(crate) expire: Option<Duration>,
|
||||
}
|
||||
|
||||
impl Set {
|
||||
#[instrument]
|
||||
pub(crate) fn parse(parse: &mut Parse) -> Result<Set, ParseError> {
|
||||
pub(crate) fn parse_frames(parse: &mut Parse) -> Result<Set, ParseError> {
|
||||
use ParseError::EndOfStream;
|
||||
|
||||
let key = parse.next_string()?;
|
||||
@@ -50,4 +61,12 @@ impl Set {
|
||||
debug!(?response);
|
||||
dst.write_frame(&response).await
|
||||
}
|
||||
|
||||
pub(crate) fn into_frame(self) -> Frame {
|
||||
let mut frame = Frame::array();
|
||||
frame.push_bulk(Bytes::from("set".as_bytes()));
|
||||
frame.push_bulk(Bytes::from(self.key.into_bytes()));
|
||||
frame.push_bulk(self.value);
|
||||
frame
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user