summary history branches tags files
commit:768aa22650a9542eb003f723657ca30a2130203c
author:Trevor Bentley
committer:Trevor Bentley
date:Tue Apr 11 14:35:17 2017 +0200
parents:27d718e3e1ec45b80422550bb05c00a9127fbd34
Context builder pattern
diff --git a/Cargo.toml b/Cargo.toml
line changes: +0/-1
index 8bdc09e..3259d78
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,4 +12,3 @@ regex = "0.2"
 rustc-serialize = "0.3.23"
 url = "1.4.0"
 rust-ini = "0.9"
-systray = "0.1.1"

diff --git a/src/main.rs b/src/main.rs
line changes: +5/-10
index 93d08f1..3e42113
--- a/src/main.rs
+++ b/src/main.rs
@@ -45,17 +45,12 @@ fn main() {
     println!("Devices:\n{}", device_list);
     println!("State:\n{}", player_state);
 
-    let ctx = connectr::PlayContext {
-        context_uri: Some("spotify:user:mrmekon:playlist:4XqYlbPdDUsranzjicPCgf".to_string()),
-        offset: Some(connectr::PlayContextOffset{position: Some(2),..Default::default()}),
-        ..Default::default()
-    };
-    require(spotify.play(None, Some(&ctx)));
-    require(spotify.pause(None));
+    let ctx = connectr::PlayContext::new()
+        .context_uri("spotify:user:mrmekon:playlist:4XqYlbPdDUsranzjicPCgf")
+        .offset_position(2)
+        .build();
+
     require(spotify.play(None, Some(&ctx)));
     require(spotify.pause(None));
     require(spotify.play(None, None));
-
-    //systray(player_state);
-    //loop {}
 }

diff --git a/src/webapi/mod.rs b/src/webapi/mod.rs
line changes: +50/-0
index d9a4cd5..41d2458
--- a/src/webapi/mod.rs
+++ b/src/webapi/mod.rs
@@ -101,6 +101,14 @@ pub struct PlayContextOffset {
 impl Default for PlayContextOffset {
     fn default() -> PlayContextOffset { PlayContextOffset { position: None, uri: None } }
 }
+impl Clone for PlayContextOffset {
+    fn clone(&self) -> PlayContextOffset {
+        PlayContextOffset {
+            position: self.position.clone(),
+            uri: self.uri.clone(),
+        }
+    }
+}
 
 #[derive(RustcDecodable, RustcEncodable)]
 pub struct PlayContext {
@@ -115,6 +123,48 @@ impl PlayContext {
     pub fn new() -> PlayContext {
         PlayContext::default()
     }
+    pub fn context_uri<'a>(&'a mut self, uri: &str) -> &'a mut PlayContext {
+        self.context_uri = Some(uri.to_string());
+        self
+    }
+    pub fn uri<'a>(&'a mut self, uri: &str) -> &'a mut PlayContext {
+        match self.uris {
+            Some(ref mut uris) => uris.push(uri.to_string()),
+            None => {
+                let mut vec = Vec::<String>::new();
+                vec.push(uri.to_string());
+                self.uris = Some(vec);
+            },
+        };
+        self
+    }
+    pub fn offset_position<'a>(&'a mut self, position: u32) -> &'a mut PlayContext {
+        match self.offset {
+            Some(ref mut o) => o.position = Some(position),
+            None => {
+                let mut o = PlayContextOffset::default();
+                o.position = Some(position);
+                self.offset = Some(o);
+            }
+        };
+        self
+    }
+    pub fn offset_uri<'a>(&'a mut self, uri: &str) -> &'a mut PlayContext {
+        match self.offset {
+            Some(ref mut o) => o.uri = Some(uri.to_string()),
+            None => {
+                let mut o = PlayContextOffset::default();
+                o.uri = Some(uri.to_string());
+                self.offset = Some(o);
+            }
+        };
+        self
+    }
+    pub fn build(&self) -> PlayContext {
+        PlayContext { context_uri: self.context_uri.clone(),
+                      uris: self.uris.clone(),
+                      offset: self.offset.clone() }
+    }
 }
 
 fn device_id_query(device: Option<DeviceId>) -> String {