Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/beanstalkd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ impl Beanstalkd {
self.cmd(commands::put(body, priority, delay, ttr)).map(parse::id)
}

/// Peeks a job from the provided tube
pub fn peek(&mut self,
type_or_id: &str)
-> BeanstalkdResult<(u64, String)> {
self.cmd(commands::peek(type_or_id)).map(|r| (parse::id(r.clone()), parse::body(r)))
}

/// Get the next message out of the queue
pub fn reserve(&mut self) -> BeanstalkdResult<(u64, String)> {
self.cmd(commands::reserve()).map(|r| (parse::id(r.clone()), parse::body(r)))
Expand Down
24 changes: 24 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ pub fn put(body: &str, priority: u32, delay: u32, ttr: u32) -> String {
body)
}

pub fn peek(peek_type_or_id: &str) -> String {
let mut cmd = "peek";

if peek_type_or_id == "ready" {
cmd = "peek-ready";
} else if peek_type_or_id == "delayed" {
cmd = "peek-delayed";
} else if peek_type_or_id == "buried" {
cmd = "peek-buried"
} else {
return build(cmd, vec![peek_type_or_id.to_string()], "")
}

build(cmd, vec![], "")
}

pub fn reserve() -> String {
build("reserve", vec![], "")
}
Expand Down Expand Up @@ -57,6 +73,14 @@ fn put_test() {
"put 0 2 10000 12\r\nsome message\r\n".to_string());
}

#[test]
fn peek_test() {
assert_eq!(peek("ready"), "peek-ready\r\n".to_string());
assert_eq!(peek("delayed"), "peek-delayed\r\n".to_string());
assert_eq!(peek("buried"), "peek-buried\r\n".to_string());
assert_eq!(peek("1"), "peek 1\r\n".to_string());
}

#[test]
fn reserve_test() {
assert_eq!(reserve(), "reserve\r\n".to_string());
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Error for BeanstalkdError {

impl Display for BeanstalkdError {
fn fmt(&self, formatter: &mut Formatter) -> ::std::fmt::Result {
self.description().fmt(formatter)
self.to_string().fmt(formatter)
}
}

Expand Down
18 changes: 14 additions & 4 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ use response::Response;

pub fn id(response: Response) -> u64 {
let line_segments: Vec<&str> = response.data.trim().split(' ').collect();
let id: u64 = FromStr::from_str(line_segments[1]).unwrap();
id

if line_segments.len() != 1 {
return FromStr::from_str(line_segments[1]).unwrap();
}

0
}

pub fn body(response: Response) -> String {
let body_start = response.data.trim().find('\n').unwrap() + 1;
response.data.trim()[body_start..].to_string()
let start_pos = response.data.trim().find('\n');
if !(start_pos.is_none()) {
let body_start = start_pos.unwrap() + 1;

response.data.trim()[body_start..].to_string()
} else {
"".to_string()
}
}

pub fn hashmap(response: Response) -> HashMap<String, String> {
Expand Down
5 changes: 4 additions & 1 deletion src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ impl<'a> Request<'a> {
"DELETED" => Status::DELETED,
"WATCHING" => Status::WATCHING,
"NOT_IGNORED" => Status::NOT_IGNORED,
"FOUND" => Status::FOUND,
"NOT_FOUND" => Status::NOT_FOUND,
_ => return Err(BeanstalkdError::RequestError),
};
let mut data = line.clone();

if status == Status::OK || status == Status::RESERVED {
if status == Status::OK || status == Status::RESERVED || status == Status::FOUND {
let segment_offset = match status {
Status::OK => 1,
Status::RESERVED => 2,
Status::FOUND => 2,
_ => return Err(BeanstalkdError::RequestError),
};
let bytes_count_str = try_option!(line_segments.get(segment_offset));
Expand Down
2 changes: 2 additions & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub enum Status {
DELETED,
WATCHING,
NOT_IGNORED,
FOUND,
NOT_FOUND
}

#[derive(Clone)]
Expand Down