diff --git a/src/cmd/generic.rs b/src/cmd/generic.rs index bea1354..6820e89 100644 --- a/src/cmd/generic.rs +++ b/src/cmd/generic.rs @@ -8,6 +8,7 @@ use crate::cmd::parse::{ParseCommandError, Parser}; #[derive(Debug, Clone)] pub enum GenericCommand { Delete(Vec), + Type(String), } impl GenericCommand { @@ -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))) diff --git a/src/mem/generic/get_type.rs b/src/mem/generic/get_type.rs new file mode 100644 index 0000000..b4e9543 --- /dev/null +++ b/src/mem/generic/get_type.rs @@ -0,0 +1,24 @@ +// Copyright (c) 2024 Xu Shaohua . 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) +} \ No newline at end of file diff --git a/src/mem/generic/mod.rs b/src/mem/generic/mod.rs index b61c88a..18b7954 100644 --- a/src/mem/generic/mod.rs +++ b/src/mem/generic/mod.rs @@ -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), } } }