Skip to content

Commit f5db614

Browse files
committed
Make read_field also do parsing.
1 parent 7af4c24 commit f5db614

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

src/client.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -240,20 +240,13 @@ impl<S: Read + Write> Client<S> {
240240
pub fn push<P: AsRef<str>>(&mut self, path: P) -> Result<Id> {
241241
self.run_command("addid", path.as_ref())
242242
.and_then(|_| self.read_field("Id"))
243-
.and_then(|v| {
244-
self.expect_ok()
245-
.and_then(|_| v.parse().map_err(From::from).map(Id))
246-
})
243+
.map(Id)
247244
}
248245

249246
/// Insert a song into a given position in a queue
250247
pub fn insert<P: AsRef<str>>(&mut self, path: P, pos: usize) -> Result<usize> {
251248
self.run_command("addid", (path.as_ref(), pos))
252249
.and_then(|_| self.read_field("Id"))
253-
.and_then(|v| {
254-
self.expect_ok()
255-
.and_then(|_| v.parse().map_err(From::from))
256-
})
257250
}
258251

259252
/// Delete a song (at some position) or several songs (in a range) from a queue
@@ -407,20 +400,12 @@ impl<S: Read + Write> Client<S> {
407400
pub fn rescan(&mut self) -> Result<u32> {
408401
self.run_command("rescan", ())
409402
.and_then(|_| self.read_field("updating_db"))
410-
.and_then(|v| {
411-
self.expect_ok()
412-
.and_then(|_| v.parse().map_err(From::from))
413-
})
414403
}
415404

416405
/// Run database update, i.e. remove non-existing files from DB
417406
pub fn update(&mut self) -> Result<u32> {
418407
self.run_command("update", ())
419408
.and_then(|_| self.read_field("updating_db"))
420-
.and_then(|v| {
421-
self.expect_ok()
422-
.and_then(|_| v.parse().map_err(From::from))
423-
})
424409
}
425410
// }}}
426411

@@ -492,7 +477,6 @@ impl<S: Read + Write> Client<S> {
492477
pub fn music_directory(&mut self) -> Result<String> {
493478
self.run_command("config", ())
494479
.and_then(|_| self.read_field("music_directory"))
495-
.and_then(|d| self.expect_ok().map(|_| d))
496480
}
497481

498482
/// List all available commands
@@ -665,7 +649,6 @@ impl<S: Read + Write> Client<S> {
665649
pub fn sticker(&mut self, typ: &str, uri: &str, name: &str) -> Result<String> {
666650
self.run_command("sticker set", (typ, uri, name))
667651
.and_then(|_| self.read_field("sticker"))
668-
.and_then(|s| self.expect_ok().map(|_| s))
669652
}
670653

671654
/// Set sticker value for a given object, identified by type and uri

src/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::io::Error as IoError;
1919
use std::num::{ParseFloatError, ParseIntError};
2020
use std::result;
2121
use std::str::FromStr;
22+
use std::string::ParseError as StringParseError;
2223
use time::ParseError as TimeParseError;
2324

2425
// Server errors {{{
@@ -345,6 +346,12 @@ impl From<ParseFloatError> for ParseError {
345346
ParseError::BadFloat(e)
346347
}
347348
}
349+
350+
impl From<StringParseError> for ParseError {
351+
fn from(e: StringParseError) -> ParseError {
352+
match e {}
353+
}
354+
}
348355
// }}}
349356

350357
// Protocol errors {{{

src/proto.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
use bufstream::BufStream;
55
use convert::{FromIter, FromMap};
6-
use error::{Error, ProtoError, Result};
6+
use error::{Error, ProtoError, Result, ParseError};
77

88
use reply::Reply;
99
use std::collections::BTreeMap;
1010
use std::fmt;
1111
use std::io::{self, Lines, Read, Write};
1212
use std::result::Result as StdResult;
13+
use std::str::FromStr;
1314

1415
pub struct Pairs<I>(pub I);
1516

@@ -154,10 +155,14 @@ pub trait Proto {
154155
}
155156
}
156157

157-
fn read_field(&mut self, field: &'static str) -> Result<String> {
158-
let (a, b) = try!(self.read_pair());
158+
fn read_field<T, E>(&mut self, field: &'static str) -> Result<T>
159+
where T: FromStr<Err = E>,
160+
ParseError: From<E>
161+
{
162+
let (a, b) = self.read_pair()?;
163+
self.expect_ok()?;
159164
if &*a == field {
160-
Ok(b)
165+
Ok(b.parse::<T>().map_err(Into::<ParseError>::into)?)
161166
} else {
162167
Err(Error::Proto(ProtoError::NoField(field)))
163168
}

0 commit comments

Comments
 (0)