Skip to content

Commit 96c9655

Browse files
authored
Merge pull request kstep#12 from tomprince/no-window-in-query
Don't include window and group in Query.
2 parents f00f019 + bdd27a1 commit 96c9655

File tree

4 files changed

+46
-59
lines changed

4 files changed

+46
-59
lines changed

examples/example.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
extern crate mpd;
22

3-
use mpd::Client;
3+
use mpd::{Client, Query};
44
use std::net::TcpStream;
55

66
fn main() {
77
let mut c = Client::new(TcpStream::connect("127.0.0.1:6600").unwrap()).unwrap();
88
println!("version: {:?}", c.version);
99
println!("status: {:?}", c.status());
10+
println!("stuff: {:?}", c.find(&Query::new(), (1, 2)));
1011
}

src/client.rs

Lines changed: 11 additions & 7 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;
18+
use search::{Query, Window};
1919
use song::{Id, Song};
2020
use stats::Stats;
2121
use status::{ReplayGain, Status};
@@ -421,19 +421,23 @@ impl<S: Read + Write> Client<S> {
421421
// TODO: searchaddpl name type what [...], readcomments
422422

423423
/// Find songs matching Query conditions.
424-
pub fn find(&mut self, query: &Query) -> Result<Vec<Song>> {
425-
self.find_generic("find", query)
424+
pub fn find<W>(&mut self, query: &Query, window: W) -> Result<Vec<Song>>
425+
where W: Into<Window>
426+
{
427+
self.find_generic("find", query, window.into())
426428
}
427429

428430
/// Case-insensitively search for songs matching Query conditions.
429-
pub fn search(&mut self, query: &Query) -> Result<Vec<Song>> {
430-
self.find_generic("search", query)
431+
pub fn search<W>(&mut self, query: &Query, window: W) -> Result<Vec<Song>>
432+
where W: Into<Window>
433+
{
434+
self.find_generic("search", query, window.into())
431435
}
432436

433-
fn find_generic(&mut self, cmd: &str, query: &Query) -> Result<Vec<Song>> {
437+
fn find_generic(&mut self, cmd: &str, query: &Query, window: Window) -> Result<Vec<Song>> {
434438
let args = query.to_string();
435439

436-
self.run_command_fmt(format_args!("{} {}", cmd, args))
440+
self.run_command_fmt(format_args!("{}{}{}", cmd, args, window))
437441
.and_then(|_| {
438442
self.read_pairs()
439443
.split("file")

src/search.rs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,34 @@ impl<'a> Filter<'a> {
3535
}
3636
}
3737

38+
pub struct Window(Option<(u32, u32)>);
39+
40+
impl From<(u32, u32)> for Window {
41+
fn from(window: (u32, u32)) -> Window {
42+
Window(Some(window))
43+
}
44+
}
45+
46+
impl From<Option<(u32, u32)>> for Window {
47+
fn from(window: Option<(u32, u32)>) -> Window {
48+
Window(window)
49+
}
50+
}
51+
52+
#[derive(Default)]
3853
pub struct Query<'a> {
3954
filters: Vec<Filter<'a>>,
40-
groups: Option<Vec<Cow<'a, str>>>,
41-
window: Option<(u32, u32)>,
4255
}
4356

4457
impl<'a> Query<'a> {
4558
pub fn new() -> Query<'a> {
46-
Query {
47-
filters: Vec::new(),
48-
groups: None,
49-
window: None,
50-
}
59+
Query { filters: Vec::new() }
5160
}
5261

5362
pub fn and<'b: 'a, V: 'b + Into<Cow<'b, str>>>(&'a mut self, term: Term<'b>, value: V) -> &'a mut Query<'a> {
5463
self.filters.push(Filter::new(term, value));
5564
self
5665
}
57-
58-
pub fn limit(&'a mut self, offset: u32, limit: u32) -> &'a mut Query<'a> {
59-
self.window = Some((offset, limit));
60-
self
61-
}
62-
63-
pub fn group<'b: 'a, G: 'b + Into<Cow<'b, str>>>(&'a mut self, group: G) -> &'a mut Query<'a> {
64-
match self.groups {
65-
None => self.groups = Some(vec![group.into()]),
66-
Some(ref mut groups) => groups.push(group.into()),
67-
};
68-
self
69-
}
7066
}
7167

7268
impl<'a> fmt::Display for Term<'a> {
@@ -92,16 +88,15 @@ impl<'a> fmt::Display for Query<'a> {
9288
for filter in &self.filters {
9389
try!(filter.fmt(f));
9490
}
91+
Ok(())
92+
}
93+
}
9594

96-
if let Some(ref groups) = self.groups {
97-
for group in groups {
98-
try!(write!(f, " group {}", group));
99-
}
100-
}
101-
102-
match self.window {
103-
Some((a, b)) => write!(f, " window {}:{}", a, b),
104-
None => Ok(()),
95+
impl fmt::Display for Window {
96+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97+
if let Some((a, b)) = self.0 {
98+
write!(f, " window {}:{}", a, b)?;
10599
}
100+
Ok(())
106101
}
107102
}

tests/search.rs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,28 @@ extern crate mpd;
33
mod helpers;
44
use helpers::connect;
55
use mpd::{Query, Term};
6+
use mpd::search::Window;
67

78
#[test]
89
fn search() {
910
let mut mpd = connect();
1011
let mut query = Query::new();
11-
// query.and(mpd::Term::Any, "Soul");
12-
let songs = mpd.find(&query);
12+
let query = query.and(mpd::Term::Any, "Soul");
13+
let songs = mpd.find(query, None);
1314
println!("{:?}", songs);
15+
assert!(songs.is_ok());
1416
}
1517

1618
#[test]
1719
fn find_query_format() {
1820
let mut query = Query::new();
1921
let finished = query.and(Term::Tag("albumartist".into()), "Mac DeMarco")
20-
.and(Term::Tag("album".into()), "Salad Days")
21-
.limit(0, 2);
22-
assert_eq!(&finished.to_string(), " albumartist \"Mac DeMarco\" album \"Salad Days\" window 0:2");
22+
.and(Term::Tag("album".into()), "Salad Days");
23+
assert_eq!(&finished.to_string(), " albumartist \"Mac DeMarco\" album \"Salad Days\"");
2324
}
2425

2526
#[test]
26-
fn count_query_format() {
27-
let mut query = Query::new();
28-
let finished = query.and(Term::Tag("artist".into()), "Courtney Barnett")
29-
.group("album");
30-
assert_eq!(&finished.to_string(), " artist \"Courtney Barnett\" group album");
27+
fn find_window_format() {
28+
let window: Window = (0, 2).into();
29+
assert_eq!(&window.to_string(), " window 0:2");
3130
}
32-
33-
// #[test]
34-
// fn count() {
35-
// let mut mpd = connect();
36-
// let song = mpd.search(mpd::Query {
37-
// clauses: vec![mpd::Clause(mpd::Term::Any, "Soul".to_owned())],
38-
// window: None,
39-
// group: None
40-
// }).unwrap();
41-
// println!("{:?}", song);
42-
// }
43-
//

0 commit comments

Comments
 (0)