Skip to content

Commit

Permalink
Don't include window and group in Query.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomprince committed Jan 6, 2017
1 parent a43b0ff commit d4eae91
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 64 deletions.
3 changes: 2 additions & 1 deletion examples/example.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate mpd;

use mpd::{Client, Query};
use std::net::TcpStream;
use mpd::Client;
//use mpd::playlists::MpdPlaylist;
//use mpd::outputs::MpdOutput;
//use mpd::idle::{MpdEvent, PLAYER, UPDATE};
Expand All @@ -12,6 +12,7 @@ fn main() {
let mut c = Client::new(TcpStream::connect("127.0.0.1:6600").unwrap()).unwrap();
println!("version: {:?}", c.version);
println!("status: {:?}", c.status());
println!("stuff: {:?}", c.find(&Query::new(), (1, 2)));
//println!("stats: {:?}", c.stats());
////println!("song: {:?}", c.current_song());
//println!("queue: {:?}", c.queue());
Expand Down
18 changes: 11 additions & 7 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use output::Output;
use playlist::Playlist;
use plugin::Plugin;
use message::{Channel, Message};
use search::Query;
use search::{Query, Window};
use mount::{Mount, Neighbor};

use convert::*;
Expand Down Expand Up @@ -421,19 +421,23 @@ impl<S: Read + Write> Client<S> {
// TODO: searchaddpl name type what [...], readcomments

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

/// Case-insensitively search for songs matching Query conditions.
pub fn search(&mut self, query: &Query) -> Result<Vec<Song>> {
self.find_generic("search", query)
pub fn search<W>(&mut self, query: &Query, window: W) -> Result<Vec<Song>>
where W: Into<Window>
{
self.find_generic("search", query, window.into())
}

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

self.run_command_fmt(format_args!("{} {}", cmd, args))
self.run_command_fmt(format_args!("{}{}{}", cmd, args, window))
.and_then(|_| {
self.read_pairs()
.split("file")
Expand Down
52 changes: 24 additions & 28 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,35 @@ impl<'a> Filter<'a> {
}
}

pub struct Window(Option<(u32, u32)>);

impl From<(u32, u32)> for Window {
fn from(window: (u32, u32)) -> Window {
Window(Some(window))
}
}

impl From<Option<(u32, u32)>> for Window {
fn from(window: Option<(u32, u32)>) -> Window {
Window(window)
}
}

#[derive(Default)]
pub struct Query<'a> {
filters: Vec<Filter<'a>>,
groups: Option<Vec<Cow<'a, str>>>,
window: Option<(u32, u32)>,
}

impl<'a> Query<'a> {
pub fn new() -> Query<'a> {
Query {
filters: Vec::new(),
groups: None,
window: None,
}
Query { filters: Vec::new() }
}

pub fn and<'b: 'a, V: 'b + Into<Cow<'b, str>>>(&'a mut self, term: Term<'b>, value: V) -> &'a mut Query<'a> {
self.filters.push(Filter::new(term, value));
self
}

pub fn limit(&'a mut self, offset: u32, limit: u32) -> &'a mut Query<'a> {
self.window = Some((offset, limit));
self
}

pub fn group<'b: 'a, G: 'b + Into<Cow<'b, str>>>(&'a mut self, group: G) -> &'a mut Query<'a> {
match self.groups {
None => self.groups = Some(vec![group.into()]),
Some(ref mut groups) => groups.push(group.into()),
};
self
}
}

impl<'a> fmt::Display for Term<'a> {
Expand All @@ -92,16 +89,15 @@ impl<'a> fmt::Display for Query<'a> {
for filter in &self.filters {
try!(filter.fmt(f));
}
Ok(())
}
}

if let Some(ref groups) = self.groups {
for group in groups {
try!(write!(f, " group {}", group));
}
}

match self.window {
Some((a, b)) => write!(f, " window {}:{}", a, b),
None => Ok(()),
impl fmt::Display for Window {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some((a, b)) = self.0 {
write!(f, " window {}:{}", a, b)?;
}
Ok(())
}
}
38 changes: 10 additions & 28 deletions tests/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,28 @@ extern crate mpd;
mod helpers;
use helpers::connect;
use mpd::{Query, Term};
use mpd::search::Window;

#[test]
fn search() {
let mut mpd = connect();
let mut query = Query::new();
//query.and(mpd::Term::Any, "Soul");
let songs = mpd.find(&query);
let query = query.and(mpd::Term::Any, "Soul");
let songs = mpd.find(query, None);
println!("{:?}", songs);
assert!(songs.is_ok());
}

#[test]
fn find_query_format() {
let mut query = Query::new();
let finished = query
.and(Term::Tag("albumartist".into()), "Mac DeMarco")
.and(Term::Tag("album".into()), "Salad Days")
.limit(0, 2);
assert_eq!(&finished.to_string(),
" albumartist \"Mac DeMarco\" album \"Salad Days\" window 0:2");
let finished = query.and(Term::Tag("albumartist".into()), "Mac DeMarco")
.and(Term::Tag("album".into()), "Salad Days");
assert_eq!(&finished.to_string(), " albumartist \"Mac DeMarco\" album \"Salad Days\"");
}

#[test]
fn count_query_format() {
let mut query = Query::new();
let finished = query
.and(Term::Tag("artist".into()), "Courtney Barnett")
.group("album");
assert_eq!(&finished.to_string(),
" artist \"Courtney Barnett\" group album");
}

/*
#[test]
fn count() {
let mut mpd = connect();
let song = mpd.search(mpd::Query {
clauses: vec![mpd::Clause(mpd::Term::Any, "Soul".to_owned())],
window: None,
group: None
}).unwrap();
println!("{:?}", song);
fn find_window_format() {
let window: Window = (0, 2).into();
assert_eq!(&window.to_string(), " window 0:2");
}
*/

0 comments on commit d4eae91

Please sign in to comment.