Skip to content

Commit 95f30e0

Browse files
authored
Merge pull request kstep#21 from tomprince/more-commands
Add some more commands.
2 parents e3c8bd6 + 8674770 commit 95f30e0

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

src/client.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use output::Output;
1515
use playlist::Playlist;
1616
use plugin::Plugin;
1717
use proto::*;
18-
use search::{Query, Window};
18+
use search::{Query, Window, Term};
1919
use song::{Id, Song};
2020
use stats::Stats;
2121
use status::{ReplayGain, Status};
@@ -238,15 +238,15 @@ impl<S: Read + Write> Client<S> {
238238
}
239239

240240
/// Append a song into a queue
241-
pub fn push<P: AsRef<str>>(&mut self, path: P) -> Result<Id> {
242-
self.run_command("addid", path.as_ref())
241+
pub fn push<P: ToSongPath>(&mut self, path: P) -> Result<Id> {
242+
self.run_command("addid", path)
243243
.and_then(|_| self.read_field("Id"))
244244
.map(Id)
245245
}
246246

247247
/// Insert a song into a given position in a queue
248-
pub fn insert<P: AsRef<str>>(&mut self, path: P, pos: usize) -> Result<usize> {
249-
self.run_command("addid", (path.as_ref(), pos))
248+
pub fn insert<P: ToSongPath>(&mut self, path: P, pos: usize) -> Result<usize> {
249+
self.run_command("addid", (path, pos))
250250
.and_then(|_| self.read_field("Id"))
251251
}
252252

@@ -377,8 +377,8 @@ impl<S: Read + Write> Client<S> {
377377
}
378378

379379
/// Add new songs to a playlist
380-
pub fn pl_push<N: ToPlaylistName, P: AsRef<str>>(&mut self, name: N, path: P) -> Result<()> {
381-
self.run_command("playlistadd", (name.to_name(), path.as_ref()))
380+
pub fn pl_push<N: ToPlaylistName, P: ToSongPath>(&mut self, name: N, path: P) -> Result<()> {
381+
self.run_command("playlistadd", (name.to_name(), path))
382382
.and_then(|_| self.expect_ok())
383383
}
384384

@@ -413,7 +413,7 @@ impl<S: Read + Write> Client<S> {
413413
// Database search {{{
414414
// TODO: count tag needle [...] [group] [grouptag], find type what [...] [window start:end]
415415
// TODO: search type what [...] [window start:end], searchadd type what [...]
416-
// TODO: findadd type what [...], listallinfo [uri], listfiles [uri], lsinfo [uri]
416+
// TODO: listallinfo [uri], listfiles [uri]
417417
// TODO: list type [filtertype] [filterwhat] [...] [group] [grouptype] [...]
418418
// TODO: searchaddpl name type what [...], readcomments
419419

@@ -436,6 +436,26 @@ impl<S: Read + Write> Client<S> {
436436
.and_then(|_| self.read_structs("file"))
437437
}
438438

439+
/// Lists unique tags values of the specified type for songs matching the given query.
440+
// TODO: list type [filtertype] [filterwhat] [...] [group] [grouptype] [...]
441+
// It isn't clear if or how `group` works
442+
pub fn list(&mut self, term: &Term, query: &Query) -> Result<Vec<String>> {
443+
self.run_command("list", (term, query))
444+
.and_then(|_| self.read_pairs().map(|p| p.map(|p| p.1)).collect())
445+
}
446+
447+
/// Find all songs in the db that match query and adds them to current playlist.
448+
pub fn findadd(&mut self, query: &Query) -> Result<()> {
449+
self.run_command("findadd", query)
450+
.and_then(|_| self.expect_ok())
451+
}
452+
453+
/// Lists the contents of a directory.
454+
pub fn lsinfo<P: ToSongPath>(&mut self, path: P) -> Result<Song> {
455+
self.run_command("lsinfo", path)
456+
.and_then(|_| self.read_struct())
457+
}
458+
439459
// }}}
440460

441461
// Output methods {{{

src/convert.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use error::Error;
55
use output::Output;
66
use playlist::Playlist;
7+
use proto::ToArguments;
78
use song::{self, Id, Song};
89
use std::collections::BTreeMap;
910
use std::ops::{Range, RangeFrom, RangeFull, RangeTo};
@@ -281,4 +282,35 @@ impl ToSongRange for song::Range {
281282
self
282283
}
283284
}
285+
284286
// }}}
287+
288+
pub trait ToSongPath {
289+
fn to_path(&self) -> &str;
290+
}
291+
292+
impl ToSongPath for Song {
293+
fn to_path(&self) -> &str {
294+
&self.file
295+
}
296+
}
297+
298+
impl<'a, T: ToSongPath> ToSongPath for &'a T {
299+
fn to_path(&self) -> &str {
300+
(*self).to_path()
301+
}
302+
}
303+
304+
impl ToSongPath for AsRef<str> {
305+
fn to_path(&self) -> &str {
306+
self.as_ref()
307+
}
308+
}
309+
310+
impl<T: ToSongPath> ToArguments for T {
311+
fn to_arguments<F, E>(&self, f: &mut F) -> Result<(), E>
312+
where F: FnMut(&str) -> Result<(), E>
313+
{
314+
self.to_path().to_arguments(f)
315+
}
316+
}

src/search.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,19 @@ impl<'a> fmt::Display for Term<'a> {
7373
}
7474
}
7575

76+
impl<'a> ToArguments for &'a Term<'a> {
77+
fn to_arguments<F, E>(&self, f: &mut F) -> StdResult<(), E>
78+
where F: FnMut(&str) -> StdResult<(), E>
79+
{
80+
f(&self.to_string())
81+
}
82+
}
83+
7684
impl<'a> ToArguments for &'a Filter<'a> {
7785
fn to_arguments<F, E>(&self, f: &mut F) -> StdResult<(), E>
7886
where F: FnMut(&str) -> StdResult<(), E>
7987
{
80-
f(&self.typ.to_string())?;
88+
(&self.typ).to_arguments(f)?;
8189
f(&self.what)
8290
}
8391
}

0 commit comments

Comments
 (0)