kade_proto/cmd/
get.rs

1use crate::prelude::*;
2
3use bytes::Bytes;
4use tracing::{debug, instrument};
5
6#[derive(Debug)]
7pub struct Get {
8    key: String,
9}
10
11impl Get {
12    pub fn new(key: impl ToString) -> Get { Get { key: key.to_string() } }
13
14    pub fn key(&self) -> &str { &self.key }
15
16    pub fn parse_frames(parse: &mut Parse) -> crate::Result<Get> {
17        let key = parse.next_string()?;
18
19        Ok(Get { key })
20    }
21
22    #[instrument(skip(self, db, dst))]
23    pub async fn apply(self, db: &Db, dst: &mut Connection) -> crate::Result<()> {
24        let response = if let Some(value) = db.get(&self.key) { Frame::Bulk(value) } else { Frame::Null };
25        debug!(?response);
26
27        dst.write_frame(&response).await?;
28
29        Ok(())
30    }
31
32    pub fn into_frame(self) -> Frame {
33        let mut frame = Frame::array();
34        frame.push_bulk(Bytes::from("get".as_bytes()));
35        frame.push_bulk(Bytes::from(self.key.into_bytes()));
36        frame
37    }
38}