Refactor: extract Spotify Worker to separate file

This commit is contained in:
Henrik Friedrichsen
2021-04-03 21:38:42 +02:00
parent 8b5bc64dc6
commit 8483653cde
8 changed files with 258 additions and 248 deletions

View File

@@ -1,15 +1,15 @@
use crate::spotify::URIType;
use crate::spotify::UriType;
use url::{Host, Url};
pub struct SpotifyURL {
pub struct SpotifyUrl {
pub id: String,
pub uri_type: URIType,
pub uri_type: UriType,
}
impl SpotifyURL {
fn new(id: &str, uri_type: URIType) -> SpotifyURL {
SpotifyURL {
impl SpotifyUrl {
fn new(id: &str, uri_type: UriType) -> SpotifyUrl {
SpotifyUrl {
id: id.to_string(),
uri_type,
}
@@ -22,7 +22,7 @@ impl SpotifyURL {
/// assert_eq!(result.id, "4uLU6hMCjMI75M1A2tKUQC");
/// assert_eq!(result.uri_type, URIType::Track);
/// ```
pub fn from_url(s: &str) -> Option<SpotifyURL> {
pub fn from_url(s: &str) -> Option<SpotifyUrl> {
let url = Url::parse(s).ok()?;
if url.host() != Some(Host::Domain("open.spotify.com")) {
return None;
@@ -33,12 +33,12 @@ impl SpotifyURL {
let entity = path_segments.next()?;
let uri_type = match entity.to_lowercase().as_str() {
"album" => Some(URIType::Album),
"artist" => Some(URIType::Artist),
"episode" => Some(URIType::Episode),
"playlist" => Some(URIType::Playlist),
"show" => Some(URIType::Show),
"track" => Some(URIType::Track),
"album" => Some(UriType::Album),
"artist" => Some(UriType::Artist),
"episode" => Some(UriType::Episode),
"playlist" => Some(UriType::Playlist),
"show" => Some(UriType::Show),
"track" => Some(UriType::Track),
"user" => {
let _user_id = path_segments.next()?;
let entity = path_segments.next()?;
@@ -47,14 +47,14 @@ impl SpotifyURL {
return None;
}
Some(URIType::Playlist)
Some(UriType::Playlist)
}
_ => None,
}?;
let id = path_segments.next()?;
Some(SpotifyURL::new(id, uri_type))
Some(SpotifyUrl::new(id, uri_type))
}
}
@@ -62,39 +62,39 @@ impl SpotifyURL {
mod tests {
use std::collections::HashMap;
use super::SpotifyURL;
use crate::spotify::URIType;
use super::SpotifyUrl;
use crate::spotify::UriType;
#[test]
fn test_urls() {
let mut test_cases = HashMap::new();
test_cases.insert(
"https://open.spotify.com/playlist/1XFxe8bkTryTODn0lk4CNa?si=FfSpZ6KPQdieClZbwHakOQ",
SpotifyURL::new("1XFxe8bkTryTODn0lk4CNa", URIType::Playlist),
SpotifyUrl::new("1XFxe8bkTryTODn0lk4CNa", UriType::Playlist),
);
test_cases.insert(
"https://open.spotify.com/track/6fRJg3R90w0juYoCJXxj2d",
SpotifyURL::new("6fRJg3R90w0juYoCJXxj2d", URIType::Track),
SpotifyUrl::new("6fRJg3R90w0juYoCJXxj2d", UriType::Track),
);
test_cases.insert(
"https://open.spotify.com/user/~villainy~/playlist/0OgoSs65CLDPn6AF6tsZVg",
SpotifyURL::new("0OgoSs65CLDPn6AF6tsZVg", URIType::Playlist),
SpotifyUrl::new("0OgoSs65CLDPn6AF6tsZVg", UriType::Playlist),
);
test_cases.insert(
"https://open.spotify.com/show/4MZfJbM2MXzZdPbv6gi5lJ",
SpotifyURL::new("4MZfJbM2MXzZdPbv6gi5lJ", URIType::Show),
SpotifyUrl::new("4MZfJbM2MXzZdPbv6gi5lJ", UriType::Show),
);
test_cases.insert(
"https://open.spotify.com/episode/3QE6rfmjRaeqXSqeWcIWF6",
SpotifyURL::new("3QE6rfmjRaeqXSqeWcIWF6", URIType::Episode),
SpotifyUrl::new("3QE6rfmjRaeqXSqeWcIWF6", UriType::Episode),
);
test_cases.insert(
"https://open.spotify.com/artist/6LEeAFiJF8OuPx747e1wxR",
SpotifyURL::new("6LEeAFiJF8OuPx747e1wxR", URIType::Artist),
SpotifyUrl::new("6LEeAFiJF8OuPx747e1wxR", UriType::Artist),
);
for case in test_cases {
let result = SpotifyURL::from_url(case.0).unwrap();
let result = SpotifyUrl::from_url(case.0).unwrap();
assert_eq!(result.id, case.1.id);
assert_eq!(result.uri_type, case.1.uri_type);
}