-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
78 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use crate::command::Command::{Get, Set}; | ||
|
||
pub enum Command { | ||
Set { key: String, body: String }, | ||
Get { key: String }, | ||
} | ||
|
||
pub fn new_command_set<'a>(key: String, body: String) -> Command { | ||
return Set { key, body }; | ||
} | ||
|
||
pub fn new_command_get(key: String) -> Command { | ||
return Get { key }; | ||
} | ||
|
||
impl Command { | ||
pub fn execute(self) -> String { | ||
match self { | ||
Command::Set { key, body } => { | ||
format!("{}\n", key) | ||
} | ||
Command::Get { key } => format!("{}\n", key), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,67 @@ | ||
use std::io::{BufRead, Error, ErrorKind, Read, BufReader}; | ||
use crate::command; | ||
use std::io::{BufRead, BufReader, Error, ErrorKind, Read}; | ||
|
||
pub struct Decoder<R: Read> { | ||
reader: BufReader<R> | ||
reader: BufReader<R>, | ||
} | ||
|
||
pub fn new<R: Read>(reader: R) -> Decoder<R> { | ||
let r = BufReader::new(reader); | ||
Decoder{reader: r} | ||
Decoder { reader: r } | ||
} | ||
|
||
impl<R: Read> Decoder<R> { | ||
pub fn decode(&mut self) -> Result<&str, Error> { | ||
pub fn decode(&mut self) -> Result<command::Command, Error> { | ||
let mut buf = String::new(); | ||
let nbytes = self.reader.read_line(&mut buf)?; | ||
if nbytes == 0 { | ||
return Err(Error::new(ErrorKind::UnexpectedEof, "got eof\n")) | ||
return Err(Error::new(ErrorKind::UnexpectedEof, "got eof\n")); | ||
} | ||
let commands: Vec<&str> = buf.trim().split_whitespace().collect(); | ||
|
||
|
||
commands.clone().first() | ||
commands | ||
.clone() | ||
.first() | ||
.ok_or(Error::new(ErrorKind::InvalidInput, "no content\n")) | ||
.and_then( | ||
move |c| match c { | ||
.and_then(move |c| match c { | ||
&"set" => self.decode_set(commands), | ||
&"get" => self.decode_get(commands), | ||
_ => Err(Error::new(ErrorKind::InvalidInput, format!("unknown command: {}\n", c))) | ||
} | ||
) | ||
_ => Err(Error::new( | ||
ErrorKind::InvalidInput, | ||
format!("unknown command: {}\n", c), | ||
)), | ||
}) | ||
} | ||
|
||
fn decode_set(&mut self, commands: Vec<&str>) -> Result<&str, Error> { | ||
fn decode_set(&mut self, commands: Vec<&str>) -> Result<command::Command, Error> { | ||
if commands.len() != 5 { | ||
return Err(Error::new(ErrorKind::InvalidInput, "set command length must be 5\n")) | ||
return Err(Error::new( | ||
ErrorKind::InvalidInput, | ||
"set command length must be 5\n", | ||
)); | ||
} | ||
let _key = commands[1]; | ||
let _flag = commands[2]; | ||
let _exptime = commands[3]; | ||
let _bytes = commands[4].parse::<usize>().map_err(|e| Error::new(ErrorKind::InvalidInput, e))?; | ||
|
||
let _bytes = commands[4] | ||
.parse::<usize>() | ||
.map_err(|e| Error::new(ErrorKind::InvalidInput, e))?; | ||
let mut buf = String::new(); | ||
let nbytes = self.reader.read_line(&mut buf)?; | ||
if nbytes == 0 { | ||
return Err(Error::new(ErrorKind::UnexpectedEof, "got eof\n")) | ||
return Err(Error::new(ErrorKind::UnexpectedEof, "got eof\n")); | ||
} | ||
Ok("STORED\n") | ||
Ok(command::new_command_set(_key.to_string(), buf)) | ||
} | ||
|
||
fn decode_get(&mut self, commands: Vec<&str>) -> Result<&str, Error> { | ||
fn decode_get(&mut self, commands: Vec<&str>) -> Result<command::Command, Error> { | ||
if commands.len() != 2 { | ||
return Err(Error::new(ErrorKind::InvalidInput, "get command length must be 2\n")) | ||
return Err(Error::new( | ||
ErrorKind::InvalidInput, | ||
"get command length must be 2\n", | ||
)); | ||
} | ||
let _key = commands[1]; | ||
Ok("key\n") | ||
Ok(command::new_command_get(_key.to_string())) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod command; | ||
pub mod decoder; |