Skip to content

Commit 7af4c24

Browse files
committed
Add a helper to read lists of structs.
1 parent b9683ac commit 7af4c24

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/client.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,13 @@ impl<S: Read + Write> Client<S> {
209209
"playlistinfo"
210210
};
211211
self.run_command(command, pos.to_range())
212-
.and_then(|_| self.read_pairs().split("file").map(|v| v.and_then(FromMap::from_map)).collect())
212+
.and_then(|_| self.read_structs("file"))
213213
}
214214

215215
/// List all songs in a play queue
216216
pub fn queue(&mut self) -> Result<Vec<Song>> {
217217
self.run_command("playlistinfo", ())
218-
.and_then(|_| self.read_pairs().split("file").map(|v| v.and_then(FromMap::from_map)).collect())
218+
.and_then(|_| self.read_structs("file"))
219219
}
220220

221221
/// Get current playing song
@@ -233,7 +233,7 @@ impl<S: Read + Write> Client<S> {
233233
/// List all changes in a queue since given version
234234
pub fn changes(&mut self, version: u32) -> Result<Vec<Song>> {
235235
self.run_command("plchanges", version)
236-
.and_then(|_| self.read_pairs().split("file").map(|v| v.and_then(FromMap::from_map)).collect())
236+
.and_then(|_| self.read_structs("file"))
237237
}
238238

239239
/// Append a song into a queue
@@ -338,13 +338,13 @@ impl<S: Read + Write> Client<S> {
338338
/// List all playlists
339339
pub fn playlists(&mut self) -> Result<Vec<Playlist>> {
340340
self.run_command("listplaylists", ())
341-
.and_then(|_| self.read_pairs().split("playlist").map(|v| v.and_then(FromMap::from_map)).collect())
341+
.and_then(|_| self.read_structs("playlist"))
342342
}
343343

344344
/// List all songs in a playlist
345345
pub fn playlist<N: ToPlaylistName>(&mut self, name: N) -> Result<Vec<Song>> {
346346
self.run_command("listplaylistinfo", name.to_name())
347-
.and_then(|_| self.read_pairs().split("file").map(|v| v.and_then(FromMap::from_map)).collect())
347+
.and_then(|_| self.read_structs("file"))
348348
}
349349

350350
/// Load playlist into queue
@@ -447,12 +447,7 @@ impl<S: Read + Write> Client<S> {
447447

448448
fn find_generic(&mut self, cmd: &str, query: &Query, window: Window) -> Result<Vec<Song>> {
449449
self.run_command(cmd, (query, window))
450-
.and_then(|_| {
451-
self.read_pairs()
452-
.split("file")
453-
.map(|v| v.and_then(FromMap::from_map))
454-
.collect()
455-
})
450+
.and_then(|_| self.read_structs("file"))
456451
}
457452

458453
// }}}
@@ -461,7 +456,7 @@ impl<S: Read + Write> Client<S> {
461456
/// List all outputs
462457
pub fn outputs(&mut self) -> Result<Vec<Output>> {
463458
self.run_command("outputs", ())
464-
.and_then(|_| self.read_pairs().split("outputid").map(|v| v.and_then(FromMap::from_map)).collect())
459+
.and_then(|_| self.read_structs("outputid"))
465460
}
466461

467462
/// Set given output enabled state
@@ -611,7 +606,7 @@ impl<S: Read + Write> Client<S> {
611606
/// Read queued messages from subscribed channels
612607
pub fn readmessages(&mut self) -> Result<Vec<Message>> {
613608
self.run_command("readmessages", ())
614-
.and_then(|_| self.read_pairs().split("channel").map(|v| v.and_then(FromMap::from_map)).collect())
609+
.and_then(|_| self.read_structs("channel"))
615610
}
616611

617612
/// Send a message to a channel
@@ -639,13 +634,13 @@ impl<S: Read + Write> Client<S> {
639634
/// These mounts exist inside MPD process only, thus they can work without root permissions.
640635
pub fn mounts(&mut self) -> Result<Vec<Mount>> {
641636
self.run_command("listmounts", ())
642-
.and_then(|_| self.read_pairs().split("mount").map(|v| v.and_then(FromMap::from_map)).collect())
637+
.and_then(|_| self.read_structs("mount"))
643638
}
644639

645640
/// List all network neighbors, which can be potentially mounted
646641
pub fn neighbors(&mut self) -> Result<Vec<Neighbor>> {
647642
self.run_command("listneighbors", ())
648-
.and_then(|_| self.read_pairs().split("neighbor").map(|v| v.and_then(FromMap::from_map)).collect())
643+
.and_then(|_| self.read_structs("neighbor"))
649644
}
650645

651646
/// Mount given neighbor to a mount point

src/proto.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![allow(missing_docs)]
33

44
use bufstream::BufStream;
5-
use convert::FromIter;
5+
use convert::{FromIter, FromMap};
66
use error::{Error, ProtoError, Result};
77

88
use reply::Reply;
@@ -108,6 +108,12 @@ pub trait Proto {
108108
self.read_pairs().collect()
109109
}
110110

111+
fn read_structs<'a, T>(&'a mut self, key: &'static str) -> Result<Vec<T>>
112+
where T: 'a + FromMap
113+
{
114+
self.read_pairs().split(key).map(|v| v.and_then(FromMap::from_map)).collect()
115+
}
116+
111117
fn read_struct<'a, T>(&'a mut self) -> Result<T>
112118
where T: 'a + FromIter,
113119
Self::Stream: 'a

0 commit comments

Comments
 (0)