-
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.
* build: parser * docs: write readme * docs: more docs
- Loading branch information
Showing
5 changed files
with
89 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,31 @@ | ||
# Yet another kvs store | ||
|
||
## Todo: | ||
* compact log file | ||
## run | ||
Launch server first | ||
``` | ||
cargo run --bin kvs-server --addr 127.0.0.1:4000 | ||
``` | ||
then launch client | ||
``` | ||
cargo run --bin kvs-client set key value --addr 127.0.0.1:4000 | ||
``` | ||
|
||
available commands: | ||
``` | ||
set key value | ||
rm key | ||
get key | ||
``` | ||
|
||
## build | ||
``` | ||
cargo build | ||
``` | ||
## benchmark | ||
``` | ||
cargo bench | ||
``` | ||
## test | ||
``` | ||
cargo test | ||
``` |
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
File renamed without changes.
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,2 +1,3 @@ | ||
mod connection; | ||
mod protocol; | ||
mod frame; | ||
mod parser; |
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,57 @@ | ||
use std::{str, vec}; | ||
|
||
use super::frame::Frame; | ||
use crate::error::{Error, Result}; | ||
use atoi::atoi; | ||
use bytes::Bytes; | ||
|
||
/// Parse a frame to command | ||
pub struct Parser { | ||
contents: vec::IntoIter<Frame>, | ||
} | ||
|
||
impl Parser { | ||
pub fn new(&self, frame: Frame) -> Result<Self> { | ||
let array = match frame { | ||
Frame::Array(array) => array, | ||
_ => return Err(Error::from("invalid frame".to_string())), | ||
}; | ||
|
||
let contents = array.into_iter(); | ||
|
||
Ok(Parser { contents }) | ||
} | ||
|
||
pub fn next(&mut self) -> Result<Frame> { | ||
self.contents | ||
.next() | ||
.ok_or(Error::from("frame empty".to_string())) | ||
} | ||
|
||
pub fn next_string(&mut self) -> Result<String> { | ||
match self.next()? { | ||
Frame::Simple(v) => Ok(v), | ||
Frame::Bulk(v) => Ok(str::from_utf8(&v)?.to_string()), | ||
_ => Err(Error::from("invalid frame".to_string())), | ||
} | ||
} | ||
|
||
pub fn next_int(&mut self) -> Result<u64> { | ||
match self.next()? { | ||
Frame::Simple(v) => { | ||
atoi(&v.as_bytes()).ok_or(Error::from(format!("cannot convert {} to int", v))) | ||
} | ||
Frame::Integers(v) => Ok(v), | ||
Frame::Bulk(v) => atoi(&v).ok_or(Error::from(format!("cannot convert {:?} to int", v))), | ||
_ => Err(Error::from("invalid frame".to_string())), | ||
} | ||
} | ||
|
||
pub fn next_bytes(&mut self) -> Result<Bytes> { | ||
match self.next()? { | ||
Frame::Simple(v) => Ok(Bytes::from(v.into_bytes())), | ||
Frame::Bulk(v) => Ok(v), | ||
_ => Err(Error::from("invalid frame".to_string())), | ||
} | ||
} | ||
} |