Skip to content

Commit

Permalink
Cleanup names of enums and remove unnecessary structs
Browse files Browse the repository at this point in the history
  • Loading branch information
RayanRal committed Apr 25, 2024
1 parent 2f5bca8 commit ddea7f9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
41 changes: 16 additions & 25 deletions src/server/commands.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
pub enum CommandEnum {
PutCommand(Put),
GetCommand(Get),
ExistsCommand(Exists),
ExitCommand(Exit),
Put {
key: String,
value: String,
ttl: u64,
},
Get {
key: String,
},
Exists {
key: String,
},
Exit,
}

pub struct Put {
pub key: String,
pub value: String,
pub ttl: u64,
}

pub struct Get {
pub key: String,
}

pub struct Exists {
pub key: String,
}

pub struct Exit {}


pub trait CommandResponse {
fn serialize(&self) -> String;
}
Expand Down Expand Up @@ -92,18 +83,18 @@ pub fn deserialize_command(input: String) -> CommandEnum {
DEFAULT_TTL
})
} else { DEFAULT_TTL };
CommandEnum::PutCommand(Put { key, value, ttl })
CommandEnum::Put { key, value, ttl }
}
Some(&"get") => {
let key = String::from(parts[1]);
CommandEnum::GetCommand(Get { key })
CommandEnum::Get { key }
}
Some(&"exists") => {
let key = String::from(parts[1]);
CommandEnum::ExistsCommand(Exists { key })
CommandEnum::Exists { key }
}
Some(&"exit") => {
CommandEnum::ExitCommand(Exit {})
CommandEnum::Exit {}
}
_ => {
// TODO: proper handling
Expand Down
8 changes: 4 additions & 4 deletions src/server/control_plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ use crate::server::commands::CommandEnum;

pub fn process_command(command: CommandEnum, cache: &mut Cache) -> Box<dyn commands::CommandResponse> {
match command {
CommandEnum::PutCommand(commands::Put { key, value, ttl }) => {
CommandEnum::Put { key, value, ttl } => {
cache.put(&key, &value, ttl);
let response = commands::PutResponse {};
Box::new(response)
}
CommandEnum::GetCommand(commands::Get { key }) => {
CommandEnum::Get { key } => {
let value = cache.get(&key);
let response = commands::GetResponse {
key,
value,
};
Box::new(response)
}
CommandEnum::ExistsCommand(commands::Exists { key }) => {
CommandEnum::Exists { key } => {
let exists = cache.exists(&key);
let response = commands::ExistsResponse { exists };
Box::new(response)
}
CommandEnum::ExitCommand(commands::Exit {}) => {
CommandEnum::Exit {} => {
warn!("Received EXIT command. Wrapping up.");
panic!("Received EXIT command");
}
Expand Down

0 comments on commit ddea7f9

Please sign in to comment.