-
Notifications
You must be signed in to change notification settings - Fork 37
/
client.rs
746 lines (646 loc) · 26.9 KB
/
client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
//! This module defines client data structure — the main entry point to MPD communication
//!
//! Almost every method of the `Client` structure corresponds to some command in [MPD protocol][proto].
//!
//! [proto]: http://www.musicpd.org/doc/protocol/
use std::io::{BufRead, Lines, Read, Write};
use std::convert::From;
use std::fmt::Arguments;
use std::net::{TcpStream, ToSocketAddrs};
use bufstream::BufStream;
use version::Version;
use error::{Error, ProtoError, Result};
use status::{ReplayGain, Status};
use stats::Stats;
use song::{Id, Song};
use output::Output;
use playlist::Playlist;
use plugin::Plugin;
use message::{Channel, Message};
use search::Query;
use mount::{Mount, Neighbor};
use convert::*;
use proto::*;
// Client {{{
/// Client connection
#[derive(Debug)]
pub struct Client<S = TcpStream>
where S: Read + Write
{
socket: BufStream<S>,
/// MPD version
pub version: Version,
}
impl Default for Client<TcpStream> {
fn default() -> Client<TcpStream> {
Client::<TcpStream>::connect("127.0.0.1:6600").unwrap()
}
}
impl Client<TcpStream> {
/// Connect client to some IP address
pub fn connect<A: ToSocketAddrs>(addr: A) -> Result<Client<TcpStream>> {
TcpStream::connect(addr).map_err(Error::Io).and_then(Client::new)
}
}
impl<S: Read + Write> Client<S> {
// Constructors {{{
/// Create client from some arbitrary pre-connected socket
pub fn new(socket: S) -> Result<Client<S>> {
let mut socket = BufStream::new(socket);
let mut banner = String::new();
try!(socket.read_line(&mut banner));
if !banner.starts_with("OK MPD ") {
return Err(From::from(ProtoError::BadBanner));
}
let version = try!(banner[7..].trim().parse::<Version>());
Ok(Client {
socket: socket,
version: version,
})
}
// }}}
// Playback options & status {{{
/// Get MPD status
pub fn status(&mut self) -> Result<Status> {
self.run_command("command_list_begin")
.and_then(|_| self.run_command("status"))
.and_then(|_| self.run_command("replay_gain_status"))
.and_then(|_| self.run_command("command_list_end"))
.and_then(|_| self.read_struct())
}
/// Get MPD playing statistics
pub fn stats(&mut self) -> Result<Stats> {
self.run_command("stats")
.and_then(|_| self.read_struct())
}
/// Clear error state
pub fn clearerror(&mut self) -> Result<()> {
self.run_command("clearerror")
.and_then(|_| self.expect_ok())
}
/// Set volume
pub fn volume(&mut self, volume: i8) -> Result<()> {
self.run_command_fmt(format_args!("setvol {}", volume))
.and_then(|_| self.expect_ok())
}
/// Set repeat state
pub fn repeat(&mut self, value: bool) -> Result<()> {
self.run_command_fmt(format_args!("repeat {}", value as u8))
.and_then(|_| self.expect_ok())
}
/// Set random state
pub fn random(&mut self, value: bool) -> Result<()> {
self.run_command_fmt(format_args!("random {}", value as u8))
.and_then(|_| self.expect_ok())
}
/// Set single state
pub fn single(&mut self, value: bool) -> Result<()> {
self.run_command_fmt(format_args!("single {}", value as u8))
.and_then(|_| self.expect_ok())
}
/// Set consume state
pub fn consume(&mut self, value: bool) -> Result<()> {
self.run_command_fmt(format_args!("consume {}", value as u8))
.and_then(|_| self.expect_ok())
}
/// Set crossfade time in seconds
pub fn crossfade<T: ToSeconds>(&mut self, value: T) -> Result<()> {
self.run_command_fmt(format_args!("crossfade {}", value.to_seconds()))
.and_then(|_| self.expect_ok())
}
/// Set mixramp level in dB
pub fn mixrampdb(&mut self, value: f32) -> Result<()> {
self.run_command_fmt(format_args!("mixrampdb {}", value))
.and_then(|_| self.expect_ok())
}
/// Set mixramp delay in seconds
pub fn mixrampdelay<T: ToSeconds>(&mut self, value: T) -> Result<()> {
self.run_command_fmt(format_args!("mixrampdelay {}", value.to_seconds()))
.and_then(|_| self.expect_ok())
}
/// Set replay gain mode
pub fn replaygain(&mut self, gain: ReplayGain) -> Result<()> {
self.run_command_fmt(format_args!("replay_gain_mode {}", gain))
.and_then(|_| self.expect_ok())
}
// }}}
// Playback control {{{
/// Start playback
pub fn play(&mut self) -> Result<()> {
self.run_command("play").and_then(|_| self.expect_ok())
}
/// Start playback from given song in a queue
pub fn switch<T: ToQueuePlace>(&mut self, place: T) -> Result<()> {
self.run_command_fmt(format_args!("play{} {}", if T::is_id() { "id" } else { "" }, place.to_place()))
.and_then(|_| self.expect_ok())
}
/// Switch to a next song in queue
pub fn next(&mut self) -> Result<()> {
self.run_command("next")
.and_then(|_| self.expect_ok())
}
/// Switch to a previous song in queue
pub fn prev(&mut self) -> Result<()> {
self.run_command("previous")
.and_then(|_| self.expect_ok())
}
/// Stop playback
pub fn stop(&mut self) -> Result<()> {
self.run_command("stop")
.and_then(|_| self.expect_ok())
}
/// Set pause state
pub fn pause(&mut self, value: bool) -> Result<()> {
self.run_command_fmt(format_args!("pause {}", value as u8))
.and_then(|_| self.expect_ok())
}
/// Seek to a given place (in seconds) in a given song
pub fn seek<T: ToSeconds, P: ToQueuePlace>(&mut self, place: P, pos: T) -> Result<()> {
self.run_command_fmt(format_args!("seek{} {} {}", if P::is_id() { "id" } else { "" }, place.to_place(), pos.to_seconds()))
.and_then(|_| self.expect_ok())
}
/// Seek to a given place (in seconds) in the current song
pub fn rewind<T: ToSeconds>(&mut self, pos: T) -> Result<()> {
self.run_command_fmt(format_args!("seekcur {}", pos.to_seconds()))
.and_then(|_| self.expect_ok())
}
// }}}
// Queue control {{{
/// List given song or range of songs in a play queue
pub fn songs<T: ToQueueRangeOrPlace>(&mut self, pos: T) -> Result<Vec<Song>> {
self.run_command_fmt(format_args!("playlist{} {}", if T::is_id() { "id" } else { "info" }, pos.to_range()))
.and_then(|_| self.read_pairs().split("file").map(|v| v.and_then(FromMap::from_map)).collect())
}
/// List all songs in a play queue
pub fn queue(&mut self) -> Result<Vec<Song>> {
self.run_command("playlistinfo")
.and_then(|_| self.read_pairs().split("file").map(|v| v.and_then(FromMap::from_map)).collect())
}
/// Get current playing song
pub fn currentsong(&mut self) -> Result<Option<Song>> {
self.run_command("currentsong")
.and_then(|_| self.read_struct::<Song>().map(|s| if s.place.is_none() { None } else { Some(s) }))
}
/// Clear current queue
pub fn clear(&mut self) -> Result<()> {
self.run_command("clear")
.and_then(|_| self.expect_ok())
}
/// List all changes in a queue since given version
pub fn changes(&mut self, version: u32) -> Result<Vec<Song>> {
self.run_command_fmt(format_args!("plchanges {}", version))
.and_then(|_| self.read_pairs().split("file").map(|v| v.and_then(FromMap::from_map)).collect())
}
/// Append a song into a queue
pub fn push<P: AsRef<str>>(&mut self, path: P) -> Result<Id> {
self.run_command_fmt(format_args!("addid \"{}\"", path.as_ref()))
.and_then(|_| self.read_field("Id"))
.and_then(|v| {
self.expect_ok()
.and_then(|_| v.parse().map_err(From::from).map(Id))
})
}
/// Insert a song into a given position in a queue
pub fn insert<P: AsRef<str>>(&mut self, path: P, pos: usize) -> Result<usize> {
self.run_command_fmt(format_args!("addid \"{}\" {}", path.as_ref(), pos))
.and_then(|_| self.read_field("Id"))
.and_then(|v| {
self.expect_ok()
.and_then(|_| v.parse().map_err(From::from))
})
}
/// Delete a song (at some position) or several songs (in a range) from a queue
pub fn delete<T: ToQueueRangeOrPlace>(&mut self, pos: T) -> Result<()> {
self.run_command_fmt(format_args!("delete{} {}", if T::is_id() { "id" } else { "" }, pos.to_range()))
.and_then(|_| self.expect_ok())
}
/// Move a song (at a some position) or several songs (in a range) to other position in queue
pub fn shift<T: ToQueueRangeOrPlace>(&mut self, from: T, to: usize) -> Result<()> {
self.run_command_fmt(format_args!("move{} {} {}", if T::is_id() { "id" } else { "" }, from.to_range(), to))
.and_then(|_| self.expect_ok())
}
/// Swap to songs in a queue
pub fn swap<T: ToQueuePlace>(&mut self, one: T, two: T) -> Result<()> {
self.run_command_fmt(format_args!("swap{} {} {}", if T::is_id() { "id" } else { "" }, one.to_place(), two.to_place()))
.and_then(|_| self.expect_ok())
}
/// Shuffle queue in a given range (use `..` to shuffle full queue)
pub fn shuffle<T: ToQueueRange>(&mut self, range: T) -> Result<()> {
self.run_command_fmt(format_args!("shuffle {}", range.to_range()))
.and_then(|_| self.expect_ok())
}
/// Set song priority in a queue
pub fn priority<T: ToQueueRangeOrPlace>(&mut self, pos: T, prio: u8) -> Result<()> {
self.run_command_fmt(format_args!("prio{} {} {}", if T::is_id() { "id" } else { "" }, prio, pos.to_range()))
.and_then(|_| self.expect_ok())
}
/// Set song range (in seconds) to play
///
/// Doesn't work for currently playing song.
pub fn range<T: ToSongId, R: ToSongRange>(&mut self, song: T, range: R) -> Result<()> {
self.run_command_fmt(format_args!("rangeid {} {}", song.to_song_id(), range.to_range()))
.and_then(|_| self.expect_ok())
}
/// Add tag to a song
pub fn tag<T: ToSongId>(&mut self, song: T, tag: &str, value: &str) -> Result<()> {
self.run_command_fmt(format_args!("addtagid {} {} \"{}\"", song.to_song_id(), tag, value))
.and_then(|_| self.expect_ok())
}
/// Delete tag from a song
pub fn untag<T: ToSongId>(&mut self, song: T, tag: &str) -> Result<()> {
self.run_command_fmt(format_args!("cleartagid {} {}", song.to_song_id(), tag))
.and_then(|_| self.expect_ok())
}
// }}}
// Connection settings {{{
/// Just pings MPD server, does nothing
pub fn ping(&mut self) -> Result<()> {
self.run_command("ping").and_then(|_| self.expect_ok())
}
/// Close MPD connection
pub fn close(&mut self) -> Result<()> {
self.run_command("close").and_then(|_| self.expect_ok())
}
/// Kill MPD server
pub fn kill(&mut self) -> Result<()> {
self.run_command("kill").and_then(|_| self.expect_ok())
}
/// Login to MPD server with given password
pub fn login(&mut self, password: &str) -> Result<()> {
self.run_command_fmt(format_args!("password \"{}\"", password))
.and_then(|_| self.expect_ok())
}
// }}}
// Playlist methods {{{
/// List all playlists
pub fn playlists(&mut self) -> Result<Vec<Playlist>> {
self.run_command("listplaylists")
.and_then(|_| self.read_pairs().split("playlist").map(|v| v.and_then(FromMap::from_map)).collect())
}
/// List all songs in a playlist
pub fn playlist<N: ToPlaylistName>(&mut self, name: N) -> Result<Vec<Song>> {
self.run_command_fmt(format_args!("listplaylistinfo \"{}\"", name.to_name()))
.and_then(|_| self.read_pairs().split("file").map(|v| v.and_then(FromMap::from_map)).collect())
}
/// Load playlist into queue
///
/// You can give either full range (`..`) to load all songs in a playlist,
/// or some partial range to load only part of playlist.
pub fn load<T: ToQueueRange, N: ToPlaylistName>(&mut self, name: N, range: T) -> Result<()> {
self.run_command_fmt(format_args!("load \"{}\" {}", name.to_name(), range.to_range()))
.and_then(|_| self.expect_ok())
}
/// Save current queue into playlist
///
/// If playlist with given name doesn't exist, create new one.
pub fn save<N: ToPlaylistName>(&mut self, name: N) -> Result<()> {
self.run_command_fmt(format_args!("save \"{}\"", name.to_name()))
.and_then(|_| self.expect_ok())
}
/// Rename playlist
pub fn pl_rename<N: ToPlaylistName>(&mut self, name: N, newname: &str) -> Result<()> {
self.run_command_fmt(format_args!("rename \"{}\" \"{}\"", name.to_name(), newname))
.and_then(|_| self.expect_ok())
}
/// Clear playlist
pub fn pl_clear<N: ToPlaylistName>(&mut self, name: N) -> Result<()> {
self.run_command_fmt(format_args!("playlistclear \"{}\"", name.to_name()))
.and_then(|_| self.expect_ok())
}
/// Delete playlist
pub fn pl_remove<N: ToPlaylistName>(&mut self, name: N) -> Result<()> {
self.run_command_fmt(format_args!("rm \"{}\"", name.to_name()))
.and_then(|_| self.expect_ok())
}
/// Add new songs to a playlist
pub fn pl_push<N: ToPlaylistName, P: AsRef<str>>(&mut self, name: N, path: P) -> Result<()> {
self.run_command_fmt(format_args!("playlistadd \"{}\" \"{}\"", name.to_name(), path.as_ref()))
.and_then(|_| self.expect_ok())
}
/// Delete a song at a given position in a playlist
pub fn pl_delete<N: ToPlaylistName>(&mut self, name: N, pos: u32) -> Result<()> {
self.run_command_fmt(format_args!("playlistdelete \"{}\" {}", name.to_name(), pos))
.and_then(|_| self.expect_ok())
}
/// Move song in a playlist from one position into another
pub fn pl_shift<N: ToPlaylistName>(&mut self, name: N, from: u32, to: u32) -> Result<()> {
self.run_command_fmt(format_args!("playlistmove \"{}\" {} {}", name.to_name(), from, to))
.and_then(|_| self.expect_ok())
}
// }}}
// Database methods {{{
/// Run database rescan, i.e. remove non-existing files from DB
/// as well as add new files to DB
pub fn rescan(&mut self) -> Result<u32> {
self.run_command("rescan")
.and_then(|_| self.read_field("updating_db"))
.and_then(|v| {
self.expect_ok()
.and_then(|_| v.parse().map_err(From::from))
})
}
/// Run database update, i.e. remove non-existing files from DB
pub fn update(&mut self) -> Result<u32> {
self.run_command("update")
.and_then(|_| self.read_field("updating_db"))
.and_then(|v| {
self.expect_ok()
.and_then(|_| v.parse().map_err(From::from))
})
}
// }}}
// Database search {{{
// TODO: count tag needle [...] [group] [grouptag], find type what [...] [window start:end]
// TODO: search type what [...] [window start:end], searchadd type what [...]
// TODO: findadd type what [...], listallinfo [uri], listfiles [uri], lsinfo [uri]
// TODO: list type [filtertype] [filterwhat] [...] [group] [grouptype] [...]
// TODO: searchaddpl name type what [...], readcomments
/// Initiate query to songs database
pub fn query<'a>(&'a mut self) -> Query<'a, S> {
Query::new(self)
}
// }}}
// Output methods {{{
/// List all outputs
pub fn outputs(&mut self) -> Result<Vec<Output>> {
self.run_command("outputs")
.and_then(|_| self.read_pairs().split("outputid").map(|v| v.and_then(FromMap::from_map)).collect())
}
/// Set given output enabled state
pub fn output<T: ToOutputId>(&mut self, id: T, state: bool) -> Result<()> {
if state { self.out_enable(id) } else { self.out_disable(id) }
}
/// Disable given output
pub fn out_disable<T: ToOutputId>(&mut self, id: T) -> Result<()> {
self.run_command_fmt(format_args!("disableoutput {}", id.to_output_id()))
.and_then(|_| self.expect_ok())
}
/// Enable given output
pub fn out_enable<T: ToOutputId>(&mut self, id: T) -> Result<()> {
self.run_command_fmt(format_args!("enableoutput {}", id.to_output_id()))
.and_then(|_| self.expect_ok())
}
/// Toggle given output
pub fn out_toggle<T: ToOutputId>(&mut self, id: T) -> Result<()> {
self.run_command_fmt(format_args!("toggleoutput {}", id.to_output_id()))
.and_then(|_| self.expect_ok())
}
// }}}
// Reflection methods {{{
/// Get current music directory
pub fn music_directory(&mut self) -> Result<String> {
self.run_command("config")
.and_then(|_| self.read_field("music_directory"))
.and_then(|d| self.expect_ok().map(|_| d))
}
/// List all available commands
pub fn commands(&mut self) -> Result<Vec<String>> {
self.run_command("commands")
.and_then(|_| {
self.read_pairs()
.filter(|r| {
r.as_ref()
.map(|&(ref a, _)| *a == "command")
.unwrap_or(true)
})
.map(|r| r.map(|(_, b)| b))
.collect()
})
}
/// List all forbidden commands
pub fn notcommands(&mut self) -> Result<Vec<String>> {
self.run_command("notcommands")
.and_then(|_| {
self.read_pairs()
.filter(|r| {
r.as_ref()
.map(|&(ref a, _)| *a == "command")
.unwrap_or(true)
})
.map(|r| r.map(|(_, b)| b))
.collect()
})
}
/// List all available URL handlers
pub fn urlhandlers(&mut self) -> Result<Vec<String>> {
self.run_command("urlhandlers")
.and_then(|_| {
self.read_pairs()
.filter(|r| {
r.as_ref()
.map(|&(ref a, _)| *a == "handler")
.unwrap_or(true)
})
.map(|r| r.map(|(_, b)| b))
.collect()
})
}
/// List all supported tag types
pub fn tagtypes(&mut self) -> Result<Vec<String>> {
self.run_command("tagtypes")
.and_then(|_| {
self.read_pairs()
.filter(|r| {
r.as_ref()
.map(|&(ref a, _)| *a == "tagtype")
.unwrap_or(true)
})
.map(|r| r.map(|(_, b)| b))
.collect()
})
}
/// List all available decoder plugins
pub fn decoders(&mut self) -> Result<Vec<Plugin>> {
try!(self.run_command("decoders"));
let mut result = Vec::new();
let mut plugin: Option<Plugin> = None;
for reply in self.read_pairs() {
let (a, b) = try!(reply);
match &*a {
"plugin" => {
plugin.map(|p| result.push(p));
plugin = Some(Plugin {
name: b,
suffixes: Vec::new(),
mime_types: Vec::new(),
});
}
"mime_type" => {
plugin.as_mut().map(|p| p.mime_types.push(b));
}
"suffix" => {
plugin.as_mut().map(|p| p.suffixes.push(b));
}
_ => unreachable!(),
}
}
plugin.map(|p| result.push(p));
Ok(result)
}
// }}}
// Messaging {{{
/// List all channels available for current connection
pub fn channels(&mut self) -> Result<Vec<Channel>> {
self.run_command("channels")
.and_then(|_| {
self.read_pairs()
.filter(|r| {
r.as_ref()
.map(|&(ref a, _)| *a == "channel")
.unwrap_or(true)
})
.map(|r| r.map(|(_, b)| unsafe { Channel::new_unchecked(b) }))
.collect()
})
}
/// Read queued messages from subscribed channels
pub fn readmessages(&mut self) -> Result<Vec<Message>> {
self.run_command("readmessages")
.and_then(|_| self.read_pairs().split("channel").map(|v| v.and_then(FromMap::from_map)).collect())
}
/// Send a message to a channel
pub fn sendmessage(&mut self, channel: Channel, message: &str) -> Result<()> {
self.run_command_fmt(format_args!("sendmessage {} \"{}\"", channel, message))
.and_then(|_| self.expect_ok())
}
/// Subscribe to a channel
pub fn subscribe(&mut self, channel: Channel) -> Result<()> {
self.run_command_fmt(format_args!("subscribe {}", channel))
.and_then(|_| self.expect_ok())
}
/// Unsubscribe to a channel
pub fn unsubscribe(&mut self, channel: Channel) -> Result<()> {
self.run_command_fmt(format_args!("unsubscribe {}", channel))
.and_then(|_| self.expect_ok())
}
// }}}
// Mount methods {{{
/// List all (virtual) mounts
///
/// These mounts exist inside MPD process only, thus they can work without root permissions.
pub fn mounts(&mut self) -> Result<Vec<Mount>> {
self.run_command("listmounts")
.and_then(|_| self.read_pairs().split("mount").map(|v| v.and_then(FromMap::from_map)).collect())
}
/// List all network neighbors, which can be potentially mounted
pub fn neighbors(&mut self) -> Result<Vec<Neighbor>> {
self.run_command("listneighbors")
.and_then(|_| self.read_pairs().split("neighbor").map(|v| v.and_then(FromMap::from_map)).collect())
}
/// Mount given neighbor to a mount point
///
/// The mount exists inside MPD process only, thus it can work without root permissions.
pub fn mount(&mut self, path: &str, uri: &str) -> Result<()> {
self.run_command_fmt(format_args!("mount \"{}\" \"{}\"", path, uri))
.and_then(|_| self.expect_ok())
}
/// Unmount given active (virtual) mount
///
/// The mount exists inside MPD process only, thus it can work without root permissions.
pub fn unmount(&mut self, path: &str) -> Result<()> {
self.run_command_fmt(format_args!("unmount \"{}\"", path))
.and_then(|_| self.expect_ok())
}
// }}}
// Sticker methods {{{
/// Show sticker value for a given object, identified by type and uri
pub fn sticker(&mut self, typ: &str, uri: &str, name: &str) -> Result<String> {
self.run_command_fmt(format_args!("sticker set {} \"{}\" {}", typ, uri, name))
.and_then(|_| self.read_field("sticker"))
.and_then(|s| self.expect_ok().map(|_| s))
}
/// Set sticker value for a given object, identified by type and uri
pub fn set_sticker(&mut self, typ: &str, uri: &str, name: &str, value: &str) -> Result<()> {
self.run_command_fmt(format_args!("sticker set {} \"{}\" {} \"{}\"", typ, uri, name, value))
.and_then(|_| self.expect_ok())
}
/// Delete sticker from a given object, identified by type and uri
pub fn delete_sticker(&mut self, typ: &str, uri: &str, name: &str) -> Result<()> {
self.run_command_fmt(format_args!("sticker delete {} \"{}\" {}", typ, uri, name))
.and_then(|_| self.expect_ok())
}
/// Remove all stickers from a given object, identified by type and uri
pub fn clear_stickers(&mut self, typ: &str, uri: &str) -> Result<()> {
self.run_command_fmt(format_args!("sticker delete {} \"{}\"", typ, uri))
.and_then(|_| self.expect_ok())
}
/// List all stickers from a given object, identified by type and uri
pub fn stickers(&mut self, typ: &str, uri: &str) -> Result<Vec<String>> {
self.run_command_fmt(format_args!("sticker list {} \"{}\"", typ, uri))
.and_then(|_| {
self.read_pairs()
.filter(|r| {
r.as_ref()
.map(|&(ref a, _)| *a == "sticker")
.unwrap_or(true)
})
.map(|r| r.map(|(_, b)| b.splitn(2, "=").nth(1).map(|s| s.to_owned()).unwrap()))
.collect()
})
}
/// List all (file, sticker) pairs for sticker name and objects of given type
/// from given directory (identified by uri)
pub fn find_sticker(&mut self, typ: &str, uri: &str, name: &str) -> Result<Vec<(String, String)>> {
self.run_command_fmt(format_args!("sticker find {} \"{}\" {}", typ, uri, name))
.and_then(|_| {
self.read_pairs()
.split("file")
.map(|rmap| {
rmap.map(|mut map| {
(map.remove("file").unwrap(),
map.remove("sticker")
.and_then(|s| s.splitn(2, "=").nth(1).map(|s| s.to_owned()))
.unwrap())
})
})
.collect()
})
}
/// List all files of a given type under given directory (identified by uri)
/// with a tag set to given value
pub fn find_sticker_eq(&mut self, typ: &str, uri: &str, name: &str, value: &str) -> Result<Vec<String>> {
self.run_command_fmt(format_args!("sticker find {} \"{}\" {} = \"{}\"", typ, uri, name, value))
.and_then(|_| {
self.read_pairs()
.filter(|r| {
r.as_ref()
.map(|&(ref a, _)| *a == "file")
.unwrap_or(true)
})
.map(|r| r.map(|(_, b)| b))
.collect()
})
}
// }}}
}
// Helper methods {{{
impl<S: Read + Write> Proto for Client<S> {
type Stream = S;
fn read_line(&mut self) -> Result<String> {
let mut buf = String::new();
try!(self.socket.read_line(&mut buf));
if buf.ends_with("\n") {
buf.pop();
}
Ok(buf)
}
fn read_pairs(&mut self) -> Pairs<Lines<&mut BufStream<S>>> {
Pairs((&mut self.socket).lines())
}
fn run_command(&mut self, command: &str) -> Result<()> {
self.socket
.write_all(command.as_bytes())
.and_then(|_| self.socket.write(&[0x0a]))
.and_then(|_| self.socket.flush())
.map_err(From::from)
}
fn run_command_fmt(&mut self, command: Arguments) -> Result<()> {
self.socket
.write_fmt(command)
.and_then(|_| self.socket.write(&[0x0a]))
.and_then(|_| self.socket.flush())
.map_err(From::from)
}
}
// }}}
// }}}