Skip to content

Commit

Permalink
generic: Add type command
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jun 11, 2024
1 parent cdc87c9 commit 324b80d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/cmd/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::cmd::parse::{ParseCommandError, Parser};
#[derive(Debug, Clone)]
pub enum GenericCommand {
Delete(Vec<String>),
Type(String),
}

impl GenericCommand {
Expand All @@ -20,6 +21,10 @@ impl GenericCommand {
let keys = parser.remaining_strings()?;
Self::Delete(keys)
}
"type" => {
let key = parser.next_string()?;
Self::Type(key)
}
_ => return Ok(None),
};
Ok(Some(Command::Generic(generic_cmd)))
Expand Down
24 changes: 24 additions & 0 deletions src/mem/generic/get_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by GNU Affero General Public License
// that can be found in the LICENSE file.

use crate::cmd::reply_frame::ReplyFrame;
use crate::mem::db::{Db, MemObject};

/// Returns the string representation of the type of the value stored at key.
///
/// The different types that can be returned are:
/// - string
/// - list
/// - set
/// - zset
/// - hash
/// - stream
pub fn get_type(db: &Db, key: &str) -> ReplyFrame {
let obj_type = match db.get(key) {
Some(MemObject::Str(_)) => "string",
Some(MemObject::List(_)) => "list",
None => "none",
};
ReplyFrame::ConstStatus(obj_type)
}
2 changes: 2 additions & 0 deletions src/mem/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use crate::cmd::reply_frame::ReplyFrame;
use crate::mem::Mem;

mod delete;
mod get_type;


impl Mem {
pub fn handle_generic_command(&mut self, command: GenericCommand) -> ReplyFrame {
match command {
GenericCommand::Delete(keys) => delete::delete(&mut self.db, keys),
GenericCommand::Type(key) => get_type::get_type(&self.db, &key),
}
}
}

0 comments on commit 324b80d

Please sign in to comment.