Skip to content

Commit 5c21ffc

Browse files
committed
Factor out sticker parsing.
1 parent 2de65b8 commit 5c21ffc

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/client.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use status::{ReplayGain, Status};
2222
use std::convert::From;
2323
use std::io::{BufRead, Lines, Read, Write};
2424
use std::net::{TcpStream, ToSocketAddrs};
25+
use sticker::Sticker;
2526
use version::Version;
2627

2728
// Client {{{
@@ -600,8 +601,8 @@ impl<S: Read + Write> Client<S> {
600601
pub fn sticker(&mut self, typ: &str, uri: &str, name: &str) -> Result<String> {
601602
self.run_command("sticker get", (typ, uri, name))
602603
// TODO: This should parse to a `Sticker` type.
603-
.and_then(|_| self.read_field::<String>("sticker"))
604-
.and_then(|s| s.splitn(2, '=').nth(1).map(str::to_owned).ok_or_else(|| Error::Proto(ProtoError::BadSticker)))
604+
.and_then(|_| self.read_field::<Sticker>("sticker"))
605+
.map(|s| s.value)
605606
}
606607

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

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub mod search;
5151
pub mod message;
5252
pub mod idle;
5353
pub mod mount;
54+
mod sticker;
5455

5556
mod proto;
5657
pub mod client;

src/sticker.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use error::ParseError;
2+
use std::str::FromStr;
3+
4+
pub struct Sticker {
5+
pub name: String,
6+
pub value: String,
7+
}
8+
9+
impl FromStr for Sticker {
10+
type Err = ParseError;
11+
fn from_str(s: &str) -> Result<Sticker, ParseError> {
12+
let mut parts = s.splitn(2, '=');
13+
match (parts.next(), parts.next()) {
14+
(Some(name), Some(value)) => {
15+
Ok(Sticker {
16+
name: name.to_owned(),
17+
value: value.to_owned(),
18+
})
19+
}
20+
_ => Err(ParseError::BadValue(s.to_owned())),
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)