Skip to content

Commit

Permalink
refactor: move range parser to parser module
Browse files Browse the repository at this point in the history
  • Loading branch information
0b01 committed Dec 17, 2017
1 parent 35bb5ba commit b431d82
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
24 changes: 2 additions & 22 deletions src/bin/server/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn gen_response (string : &str, state: &mut State) -> ReturnType {
Exists(dbname.to_owned())
} else

if string.starts_with("ADD ") {
if string.starts_with("ADD ") || string.starts_with("INSERT ") {
let parsed = if string.contains(" INTO ") {
parser::parse_add_into(&string)
} else {
Expand All @@ -122,27 +122,7 @@ pub fn gen_response (string : &str, state: &mut State) -> ReturnType {
let count : Vec<&str> = count.split(" ").collect();
let count = count[0].parse::<u32>().unwrap();

let ranged = string.contains(" FROM ");
let range = if ranged {
// range to query
let from_epoch = string.clone()[(string.find(" FROM ").unwrap()+6)..]
.split(" ")
.collect::<Vec<&str>>()
[0]
.parse::<u64>()
.unwrap()
* 1000;
let to_epoch = string.clone()[(string.find(" TO ").unwrap()+4)..]
.split(" ")
.collect::<Vec<&str>>()
[0]
.parse::<u64>()
.unwrap()
* 1000;
Some((from_epoch, to_epoch))
} else {
None
};
let range = parser::parse_get_range(string);

// test if json
let format = if string.contains(" AS JSON") { GetFormat::JSON } else { GetFormat::DTF };
Expand Down
22 changes: 22 additions & 0 deletions src/bin/server/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ pub fn parse_add_into(string: &str) -> (Option<Update>, Option<String>) {
}
}

pub fn parse_get_range(string: &str) -> Option<(u64, u64)> {
if string.contains(" FROM ") {
// range to query
let from_epoch = string.clone()[(string.find(" FROM ").unwrap()+6)..]
.split(" ")
.collect::<Vec<&str>>()
[0]
.parse::<u64>()
.unwrap()
* 1000;
let to_epoch = string.clone()[(string.find(" TO ").unwrap()+4)..]
.split(" ")
.collect::<Vec<&str>>()
[0]
.parse::<u64>()
.unwrap()
* 1000;
Some((from_epoch, to_epoch))
} else {
None
}
}

#[cfg(test)]
mod tests {
Expand Down
8 changes: 7 additions & 1 deletion src/bin/server/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,13 @@ impl State {
let result = ups_from_fs;

match count {
ReqCount::Count(c) => return self._return_aux(&result[..c as usize], format),
ReqCount::Count(c) => {
if result.len() >= c as usize {
return self._return_aux(&result[..(c as usize -1)], format);
} else {
return Some(ReturnType::Error(format!("Requested {} but only have {}.", c, result.len())))
}
}
ReqCount::All => self._return_aux(&result, format),
}
}
Expand Down

0 comments on commit b431d82

Please sign in to comment.