@@ -209,19 +209,20 @@ impl<S: Read + Write> Client<S> {
209
209
"playlistinfo"
210
210
} ;
211
211
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" ) )
213
213
}
214
214
215
215
/// List all songs in a play queue
216
216
pub fn queue ( & mut self ) -> Result < Vec < Song > > {
217
217
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" ) )
219
219
}
220
220
221
221
/// Get current playing song
222
222
pub fn currentsong ( & mut self ) -> Result < Option < Song > > {
223
223
self . run_command ( "currentsong" , ( ) )
224
- . and_then ( |_| self . read_struct :: < Song > ( ) . map ( |s| if s. place . is_none ( ) { None } else { Some ( s) } ) )
224
+ . and_then ( |_| self . read_struct :: < Song > ( ) )
225
+ . map ( |s| if s. place . is_none ( ) { None } else { Some ( s) } )
225
226
}
226
227
227
228
/// Clear current queue
@@ -233,27 +234,20 @@ impl<S: Read + Write> Client<S> {
233
234
/// List all changes in a queue since given version
234
235
pub fn changes ( & mut self , version : u32 ) -> Result < Vec < Song > > {
235
236
self . run_command ( "plchanges" , version)
236
- . and_then ( |_| self . read_pairs ( ) . split ( "file" ) . map ( |v| v . and_then ( FromMap :: from_map ) ) . collect ( ) )
237
+ . and_then ( |_| self . read_structs ( "file" ) )
237
238
}
238
239
239
240
/// Append a song into a queue
240
241
pub fn push < P : AsRef < str > > ( & mut self , path : P ) -> Result < Id > {
241
242
self . run_command ( "addid" , path. as_ref ( ) )
242
243
. 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
- } )
244
+ . map ( Id )
247
245
}
248
246
249
247
/// Insert a song into a given position in a queue
250
248
pub fn insert < P : AsRef < str > > ( & mut self , path : P , pos : usize ) -> Result < usize > {
251
249
self . run_command ( "addid" , ( path. as_ref ( ) , pos) )
252
250
. and_then ( |_| self . read_field ( "Id" ) )
253
- . and_then ( |v| {
254
- self . expect_ok ( )
255
- . and_then ( |_| v. parse ( ) . map_err ( From :: from) )
256
- } )
257
251
}
258
252
259
253
/// Delete a song (at some position) or several songs (in a range) from a queue
@@ -338,13 +332,13 @@ impl<S: Read + Write> Client<S> {
338
332
/// List all playlists
339
333
pub fn playlists ( & mut self ) -> Result < Vec < Playlist > > {
340
334
self . run_command ( "listplaylists" , ( ) )
341
- . and_then ( |_| self . read_pairs ( ) . split ( "playlist" ) . map ( |v| v . and_then ( FromMap :: from_map ) ) . collect ( ) )
335
+ . and_then ( |_| self . read_structs ( "playlist" ) )
342
336
}
343
337
344
338
/// List all songs in a playlist
345
339
pub fn playlist < N : ToPlaylistName > ( & mut self , name : N ) -> Result < Vec < Song > > {
346
340
self . run_command ( "listplaylistinfo" , name. to_name ( ) )
347
- . and_then ( |_| self . read_pairs ( ) . split ( "file" ) . map ( |v| v . and_then ( FromMap :: from_map ) ) . collect ( ) )
341
+ . and_then ( |_| self . read_structs ( "file" ) )
348
342
}
349
343
350
344
/// Load playlist into queue
@@ -407,20 +401,12 @@ impl<S: Read + Write> Client<S> {
407
401
pub fn rescan ( & mut self ) -> Result < u32 > {
408
402
self . run_command ( "rescan" , ( ) )
409
403
. 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
- } )
414
404
}
415
405
416
406
/// Run database update, i.e. remove non-existing files from DB
417
407
pub fn update ( & mut self ) -> Result < u32 > {
418
408
self . run_command ( "update" , ( ) )
419
409
. 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
- } )
424
410
}
425
411
// }}}
426
412
@@ -447,12 +433,7 @@ impl<S: Read + Write> Client<S> {
447
433
448
434
fn find_generic ( & mut self , cmd : & str , query : & Query , window : Window ) -> Result < Vec < Song > > {
449
435
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
- } )
436
+ . and_then ( |_| self . read_structs ( "file" ) )
456
437
}
457
438
458
439
// }}}
@@ -461,7 +442,7 @@ impl<S: Read + Write> Client<S> {
461
442
/// List all outputs
462
443
pub fn outputs ( & mut self ) -> Result < Vec < Output > > {
463
444
self . run_command ( "outputs" , ( ) )
464
- . and_then ( |_| self . read_pairs ( ) . split ( "outputid" ) . map ( |v| v . and_then ( FromMap :: from_map ) ) . collect ( ) )
445
+ . and_then ( |_| self . read_structs ( "outputid" ) )
465
446
}
466
447
467
448
/// Set given output enabled state
@@ -497,121 +478,50 @@ impl<S: Read + Write> Client<S> {
497
478
pub fn music_directory ( & mut self ) -> Result < String > {
498
479
self . run_command ( "config" , ( ) )
499
480
. and_then ( |_| self . read_field ( "music_directory" ) )
500
- . and_then ( |d| self . expect_ok ( ) . map ( |_| d) )
501
481
}
502
482
503
483
/// List all available commands
504
484
pub fn commands ( & mut self ) -> Result < Vec < String > > {
505
485
self . run_command ( "commands" , ( ) )
506
- . and_then ( |_| {
507
- self . read_pairs ( )
508
- . filter ( |r| {
509
- r. as_ref ( )
510
- . map ( |& ( ref a, _) | * a == "command" )
511
- . unwrap_or ( true )
512
- } )
513
- . map ( |r| r. map ( |( _, b) | b) )
514
- . collect ( )
515
- } )
486
+ . and_then ( |_| self . read_list ( "command" ) )
516
487
}
517
488
518
489
/// List all forbidden commands
519
490
pub fn notcommands ( & mut self ) -> Result < Vec < String > > {
520
491
self . run_command ( "notcommands" , ( ) )
521
- . and_then ( |_| {
522
- self . read_pairs ( )
523
- . filter ( |r| {
524
- r. as_ref ( )
525
- . map ( |& ( ref a, _) | * a == "command" )
526
- . unwrap_or ( true )
527
- } )
528
- . map ( |r| r. map ( |( _, b) | b) )
529
- . collect ( )
530
- } )
492
+ . and_then ( |_| self . read_list ( "command" ) )
531
493
}
532
494
533
495
/// List all available URL handlers
534
496
pub fn urlhandlers ( & mut self ) -> Result < Vec < String > > {
535
497
self . run_command ( "urlhandlers" , ( ) )
536
- . and_then ( |_| {
537
- self . read_pairs ( )
538
- . filter ( |r| {
539
- r. as_ref ( )
540
- . map ( |& ( ref a, _) | * a == "handler" )
541
- . unwrap_or ( true )
542
- } )
543
- . map ( |r| r. map ( |( _, b) | b) )
544
- . collect ( )
545
- } )
498
+ . and_then ( |_| self . read_list ( "handler" ) )
546
499
}
547
500
548
501
/// List all supported tag types
549
502
pub fn tagtypes ( & mut self ) -> Result < Vec < String > > {
550
503
self . run_command ( "tagtypes" , ( ) )
551
- . and_then ( |_| {
552
- self . read_pairs ( )
553
- . filter ( |r| {
554
- r. as_ref ( )
555
- . map ( |& ( ref a, _) | * a == "tagtype" )
556
- . unwrap_or ( true )
557
- } )
558
- . map ( |r| r. map ( |( _, b) | b) )
559
- . collect ( )
560
- } )
504
+ . and_then ( |_| self . read_list ( "tagtype" ) )
561
505
}
562
506
563
507
/// List all available decoder plugins
564
508
pub fn decoders ( & mut self ) -> Result < Vec < Plugin > > {
565
- try!( self . run_command ( "decoders" , ( ) ) ) ;
566
-
567
- let mut result = Vec :: new ( ) ;
568
- let mut plugin: Option < Plugin > = None ;
569
- for reply in self . read_pairs ( ) {
570
- let ( a, b) = try!( reply) ;
571
- match & * a {
572
- "plugin" => {
573
- plugin. map ( |p| result. push ( p) ) ;
574
-
575
- plugin = Some ( Plugin {
576
- name : b,
577
- suffixes : Vec :: new ( ) ,
578
- mime_types : Vec :: new ( ) ,
579
- } ) ;
580
- }
581
- "mime_type" => {
582
- plugin. as_mut ( ) . map ( |p| p. mime_types . push ( b) ) ;
583
- }
584
- "suffix" => {
585
- plugin. as_mut ( ) . map ( |p| p. suffixes . push ( b) ) ;
586
- }
587
- _ => unreachable ! ( ) ,
588
- }
589
- }
590
- plugin. map ( |p| result. push ( p) ) ;
591
- Ok ( result)
509
+ self . run_command ( "decoders" , ( ) ) . and_then ( |_| self . read_struct ( ) )
592
510
}
593
511
// }}}
594
512
595
513
// Messaging {{{
596
514
/// List all channels available for current connection
597
515
pub fn channels ( & mut self ) -> Result < Vec < Channel > > {
598
516
self . run_command ( "channels" , ( ) )
599
- . and_then ( |_| {
600
- self . read_pairs ( )
601
- . filter ( |r| {
602
- r. as_ref ( )
603
- . map ( |& ( ref a, _) | * a == "channel" )
604
- . unwrap_or ( true )
605
- } )
606
- . map ( |r| r. map ( |( _, b) | unsafe { Channel :: new_unchecked ( b) } ) )
607
- . collect ( )
608
- } )
517
+ . and_then ( |_| self . read_list ( "channel" ) )
518
+ . map ( |v| v. into_iter ( ) . map ( |b| unsafe { Channel :: new_unchecked ( b) } ) . collect ( ) )
609
519
}
610
520
611
521
/// Read queued messages from subscribed channels
612
522
pub fn readmessages ( & mut self ) -> Result < Vec < Message > > {
613
523
self . run_command ( "readmessages" , ( ) )
614
- . and_then ( |_| self . read_pairs ( ) . split ( "channel" ) . map ( |v| v . and_then ( FromMap :: from_map ) ) . collect ( ) )
524
+ . and_then ( |_| self . read_structs ( "channel" ) )
615
525
}
616
526
617
527
/// Send a message to a channel
@@ -639,13 +549,13 @@ impl<S: Read + Write> Client<S> {
639
549
/// These mounts exist inside MPD process only, thus they can work without root permissions.
640
550
pub fn mounts ( & mut self ) -> Result < Vec < Mount > > {
641
551
self . run_command ( "listmounts" , ( ) )
642
- . and_then ( |_| self . read_pairs ( ) . split ( "mount" ) . map ( |v| v . and_then ( FromMap :: from_map ) ) . collect ( ) )
552
+ . and_then ( |_| self . read_structs ( "mount" ) )
643
553
}
644
554
645
555
/// List all network neighbors, which can be potentially mounted
646
556
pub fn neighbors ( & mut self ) -> Result < Vec < Neighbor > > {
647
557
self . run_command ( "listneighbors" , ( ) )
648
- . and_then ( |_| self . read_pairs ( ) . split ( "neighbor" ) . map ( |v| v . and_then ( FromMap :: from_map ) ) . collect ( ) )
558
+ . and_then ( |_| self . read_structs ( "neighbor" ) )
649
559
}
650
560
651
561
/// Mount given neighbor to a mount point
@@ -670,7 +580,6 @@ impl<S: Read + Write> Client<S> {
670
580
pub fn sticker ( & mut self , typ : & str , uri : & str , name : & str ) -> Result < String > {
671
581
self . run_command ( "sticker set" , ( typ, uri, name) )
672
582
. and_then ( |_| self . read_field ( "sticker" ) )
673
- . and_then ( |s| self . expect_ok ( ) . map ( |_| s) )
674
583
}
675
584
676
585
/// Set sticker value for a given object, identified by type and uri
@@ -694,16 +603,8 @@ impl<S: Read + Write> Client<S> {
694
603
/// List all stickers from a given object, identified by type and uri
695
604
pub fn stickers ( & mut self , typ : & str , uri : & str ) -> Result < Vec < String > > {
696
605
self . run_command ( "sticker list" , ( typ, uri) )
697
- . and_then ( |_| {
698
- self . read_pairs ( )
699
- . filter ( |r| {
700
- r. as_ref ( )
701
- . map ( |& ( ref a, _) | * a == "sticker" )
702
- . unwrap_or ( true )
703
- } )
704
- . map ( |r| r. map ( |( _, b) | b. splitn ( 2 , '=' ) . nth ( 1 ) . map ( |s| s. to_owned ( ) ) . unwrap ( ) ) )
705
- . collect ( )
706
- } )
606
+ . and_then ( |_| self . read_list ( "sticker" ) )
607
+ . map ( |v| v. into_iter ( ) . map ( |b| b. splitn ( 2 , '=' ) . nth ( 1 ) . map ( |s| s. to_owned ( ) ) . unwrap ( ) ) . collect ( ) )
707
608
}
708
609
709
610
/// List all (file, sticker) pairs for sticker name and objects of given type
@@ -729,16 +630,7 @@ impl<S: Read + Write> Client<S> {
729
630
/// with a tag set to given value
730
631
pub fn find_sticker_eq ( & mut self , typ : & str , uri : & str , name : & str , value : & str ) -> Result < Vec < String > > {
731
632
self . run_command ( "sticker find" , ( typ, uri, name, value) )
732
- . and_then ( |_| {
733
- self . read_pairs ( )
734
- . filter ( |r| {
735
- r. as_ref ( )
736
- . map ( |& ( ref a, _) | * a == "file" )
737
- . unwrap_or ( true )
738
- } )
739
- . map ( |r| r. map ( |( _, b) | b) )
740
- . collect ( )
741
- } )
633
+ . and_then ( |_| self . read_list ( "file" ) )
742
634
}
743
635
// }}}
744
636
}
0 commit comments