diff --git a/examples/example.rs b/examples/example.rs index 1c240c06..1bc5ba6a 100644 --- a/examples/example.rs +++ b/examples/example.rs @@ -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}; @@ -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()); diff --git a/src/client.rs b/src/client.rs index 92c59e54..7025c83c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -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::*; @@ -421,19 +421,23 @@ impl Client { // TODO: searchaddpl name type what [...], readcomments /// Find songs matching Query conditions. - pub fn find(&mut self, query: &Query) -> Result> { - self.find_generic("find", query) + pub fn find(&mut self, query: &Query, window: W) -> Result> + where W: Into + { + self.find_generic("find", query, window.into()) } /// Case-insensitively search for songs matching Query conditions. - pub fn search(&mut self, query: &Query) -> Result> { - self.find_generic("search", query) + pub fn search(&mut self, query: &Query, window: W) -> Result> + where W: Into + { + self.find_generic("search", query, window.into()) } - fn find_generic(&mut self, cmd: &str, query: &Query) -> Result> { + fn find_generic(&mut self, cmd: &str, query: &Query, window: Window) -> Result> { 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") diff --git a/src/search.rs b/src/search.rs index 93f1a573..16ebaf32 100644 --- a/src/search.rs +++ b/src/search.rs @@ -35,19 +35,28 @@ 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> for Window { + fn from(window: Option<(u32, u32)>) -> Window { + Window(window) + } +} + +#[derive(Default)] pub struct Query<'a> { filters: Vec>, - groups: Option>>, - 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>>(&'a mut self, term: Term<'b>, value: V) -> &'a mut Query<'a> { @@ -55,18 +64,6 @@ impl<'a> Query<'a> { 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>>(&'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> { @@ -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(()) } } diff --git a/tests/search.rs b/tests/search.rs index 2a320961..bd43b086 100644 --- a/tests/search.rs +++ b/tests/search.rs @@ -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"); } -*/