From 9747bbc470c71ab187e6e9d440ef97a81b857577 Mon Sep 17 00:00:00 2001 From: Paul Lietar Date: Tue, 26 Jan 2016 22:25:13 +0000 Subject: [PATCH] Add link API. --- src/lib.rs | 1 + src/link.rs | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/link.rs diff --git a/src/lib.rs b/src/lib.rs index 0459ca75..aea658e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,5 +41,6 @@ pub mod metadata; pub mod player; pub mod session; pub mod spirc; +pub mod link; mod stream; mod zeroconf; diff --git a/src/link.rs b/src/link.rs new file mode 100644 index 00000000..2bd8e68c --- /dev/null +++ b/src/link.rs @@ -0,0 +1,83 @@ +use util::SpotifyId; +use session::Session; +use metadata::{MetadataRef, Album, Artist, Track}; + +#[derive(Debug,Clone)] +pub enum Link { + Track { + id: SpotifyId, + offset: u32, + }, + + Album { + id: SpotifyId, + }, + + Artist { + id: SpotifyId, + }, + + /* + Search, + Playlist, + Profile, + Starred, + LocalTrack, + Image, + */ +} + +impl Link { + pub fn from_str(uri: &str) -> Result { + let mut parts = uri.split(':'); + + if parts.next() != Some("spotify") { + return Err(()) + } + + match parts.next() { + Some("track") => parts.next() + .map(SpotifyId::from_base62) + .map(|id| Link::Track { + id: id, + offset: 0, + }) + .ok_or(()), + Some("album") => parts.next() + .map(SpotifyId::from_base62) + .map(|id| Link::Album { + id: id, + }) + .ok_or(()), + Some("artist") => parts.next() + .map(SpotifyId::from_base62) + .map(|id| Link::Artist { + id: id, + }) + .ok_or(()), + _ => Err(()) + } + } + + pub fn as_track(&self, session: &Session) -> Option> { + match *self { + Link::Track { id, .. } => Some(session.metadata::(id)), + _ => None, + } + } + + pub fn as_album(&self, session: &Session) -> Option> { + match *self { + Link::Album { id, .. } => Some(session.metadata::(id)), + _ => None, + } + } + + pub fn as_artist(&self, session: &Session) -> Option> { + match *self { + Link::Artist { id, .. } => Some(session.metadata::(id)), + _ => None, + } + } +} +