2015-06-23 14:38:29 +00:00
|
|
|
use std;
|
2017-01-29 17:54:32 +00:00
|
|
|
use std::fmt;
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2018-09-28 18:10:22 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-10-07 22:27:26 +00:00
|
|
|
pub enum SpotifyAudioType {
|
2018-09-28 18:10:22 +00:00
|
|
|
Track,
|
|
|
|
Podcast,
|
2019-10-09 17:49:13 +00:00
|
|
|
NonPlayable,
|
2018-09-28 18:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub struct SpotifyId {
|
|
|
|
pub id: u128,
|
2019-10-07 22:27:26 +00:00
|
|
|
pub audio_type: SpotifyAudioType,
|
2018-09-28 18:10:22 +00:00
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2018-02-25 14:47:57 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct SpotifyIdError;
|
|
|
|
|
2019-10-08 09:31:18 +00:00
|
|
|
const BASE62_DIGITS: &'static [u8] =
|
|
|
|
b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
2015-06-23 14:38:29 +00:00
|
|
|
const BASE16_DIGITS: &'static [u8] = b"0123456789abcdef";
|
|
|
|
|
|
|
|
impl SpotifyId {
|
2018-09-28 18:10:22 +00:00
|
|
|
fn as_track(n: u128) -> SpotifyId {
|
|
|
|
SpotifyId {
|
|
|
|
id: n.to_owned(),
|
2019-10-07 22:27:26 +00:00
|
|
|
audio_type: SpotifyAudioType::Track,
|
2018-09-28 18:10:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 14:47:57 +00:00
|
|
|
pub fn from_base16(id: &str) -> Result<SpotifyId, SpotifyIdError> {
|
2015-06-23 14:38:29 +00:00
|
|
|
let data = id.as_bytes();
|
|
|
|
|
2019-07-23 19:04:17 +00:00
|
|
|
let mut n = 0u128;
|
2015-06-23 14:38:29 +00:00
|
|
|
for c in data {
|
2018-02-25 14:47:57 +00:00
|
|
|
let d = match BASE16_DIGITS.iter().position(|e| e == c) {
|
|
|
|
None => return Err(SpotifyIdError),
|
2019-07-23 19:04:17 +00:00
|
|
|
Some(x) => x as u128,
|
2018-02-25 14:47:57 +00:00
|
|
|
};
|
2019-07-23 19:04:17 +00:00
|
|
|
n = n * 16;
|
|
|
|
n = n + d;
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 18:10:22 +00:00
|
|
|
Ok(SpotifyId::as_track(n))
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 14:47:57 +00:00
|
|
|
pub fn from_base62(id: &str) -> Result<SpotifyId, SpotifyIdError> {
|
2015-06-23 14:38:29 +00:00
|
|
|
let data = id.as_bytes();
|
|
|
|
|
2019-07-23 19:04:17 +00:00
|
|
|
let mut n = 0u128;
|
2015-06-23 14:38:29 +00:00
|
|
|
for c in data {
|
2018-02-25 14:47:57 +00:00
|
|
|
let d = match BASE62_DIGITS.iter().position(|e| e == c) {
|
|
|
|
None => return Err(SpotifyIdError),
|
2019-07-23 19:04:17 +00:00
|
|
|
Some(x) => x as u128,
|
2018-02-25 14:47:57 +00:00
|
|
|
};
|
2019-07-23 19:04:17 +00:00
|
|
|
n = n * 62;
|
|
|
|
n = n + d;
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
2018-09-28 18:10:22 +00:00
|
|
|
Ok(SpotifyId::as_track(n))
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 14:47:57 +00:00
|
|
|
pub fn from_raw(data: &[u8]) -> Result<SpotifyId, SpotifyIdError> {
|
|
|
|
if data.len() != 16 {
|
2018-02-25 15:33:32 +00:00
|
|
|
return Err(SpotifyIdError);
|
2018-02-25 14:47:57 +00:00
|
|
|
};
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2019-07-23 19:04:17 +00:00
|
|
|
let mut arr: [u8; 16] = Default::default();
|
|
|
|
arr.copy_from_slice(&data[0..16]);
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2018-09-28 18:10:22 +00:00
|
|
|
Ok(SpotifyId::as_track(u128::from_be_bytes(arr)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_uri(uri: &str) -> Result<SpotifyId, SpotifyIdError> {
|
|
|
|
let parts = uri.split(":").collect::<Vec<&str>>();
|
2019-10-09 17:49:13 +00:00
|
|
|
let gid = parts.last().unwrap();
|
|
|
|
if uri.contains(":episode:") {
|
|
|
|
let mut spotify_id = SpotifyId::from_base62(gid).unwrap();
|
2019-10-07 22:27:26 +00:00
|
|
|
let _ = std::mem::replace(&mut spotify_id.audio_type, SpotifyAudioType::Podcast);
|
2018-09-28 18:10:22 +00:00
|
|
|
Ok(spotify_id)
|
2019-10-09 17:49:13 +00:00
|
|
|
} else if uri.contains(":track:") {
|
|
|
|
SpotifyId::from_base62(gid)
|
2018-09-28 18:10:22 +00:00
|
|
|
} else {
|
2019-10-09 17:49:13 +00:00
|
|
|
// show/playlist/artist/album/??
|
|
|
|
let mut spotify_id = SpotifyId::from_base62(gid).unwrap();
|
|
|
|
let _ = std::mem::replace(&mut spotify_id.audio_type, SpotifyAudioType::NonPlayable);
|
|
|
|
Ok(spotify_id)
|
2018-09-28 18:10:22 +00:00
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_base16(&self) -> String {
|
2018-09-28 18:10:22 +00:00
|
|
|
format!("{:032x}", self.id)
|
2018-02-28 12:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_base62(&self) -> String {
|
2018-09-28 18:10:22 +00:00
|
|
|
let &SpotifyId { id: mut n, .. } = self;
|
2018-02-28 12:28:57 +00:00
|
|
|
|
|
|
|
let mut data = [0u8; 22];
|
|
|
|
for i in 0..22 {
|
2019-07-23 19:04:17 +00:00
|
|
|
data[21 - i] = BASE62_DIGITS[(n % 62) as usize];
|
|
|
|
n /= 62;
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 11:20:37 +00:00
|
|
|
std::str::from_utf8(&data).unwrap().to_owned()
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 11:29:13 +00:00
|
|
|
pub fn to_uri(&self) -> String {
|
|
|
|
match self.audio_type {
|
2020-03-02 11:36:27 +00:00
|
|
|
SpotifyAudioType::Track => format!("spotify:track:{}", self.to_base62()),
|
|
|
|
SpotifyAudioType::Podcast => format!("spotify:episode:{}", self.to_base62()),
|
|
|
|
SpotifyAudioType::NonPlayable => format!("spotify:unknown:{}", self.to_base62()),
|
2020-03-02 11:29:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 14:38:29 +00:00
|
|
|
pub fn to_raw(&self) -> [u8; 16] {
|
2018-09-28 18:10:22 +00:00
|
|
|
self.id.to_be_bytes()
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:37:08 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2017-01-29 17:54:32 +00:00
|
|
|
pub struct FileId(pub [u8; 20]);
|
|
|
|
|
2015-07-07 21:40:31 +00:00
|
|
|
impl FileId {
|
|
|
|
pub fn to_base16(&self) -> String {
|
2016-01-02 15:19:39 +00:00
|
|
|
self.0
|
|
|
|
.iter()
|
2015-07-07 21:40:31 +00:00
|
|
|
.map(|b| format!("{:02x}", b))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.concat()
|
|
|
|
}
|
|
|
|
}
|
2017-01-29 17:54:32 +00:00
|
|
|
|
|
|
|
impl fmt::Debug for FileId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_tuple("FileId").field(&self.to_base16()).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for FileId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.write_str(&self.to_base16())
|
|
|
|
}
|
|
|
|
}
|