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-02-11 11:37:08 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2015-06-23 14:38:29 +00:00
|
|
|
pub struct SpotifyId(u128);
|
|
|
|
|
2018-02-25 14:47:57 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct SpotifyIdError;
|
|
|
|
|
2018-02-11 11:37:08 +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-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-02-25 02:04:07 +00:00
|
|
|
Ok(SpotifyId(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-02-25 02:04:07 +00:00
|
|
|
Ok(SpotifyId(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
|
|
|
|
2019-07-23 19:04:17 +00:00
|
|
|
Ok(SpotifyId(u128::from_be_bytes(arr)))
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_base16(&self) -> String {
|
2019-10-22 17:31:18 +00:00
|
|
|
format!("{:032x}", self.0)
|
2018-02-28 12:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_base62(&self) -> String {
|
|
|
|
let &SpotifyId(mut n) = self;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_raw(&self) -> [u8; 16] {
|
2019-07-23 19:04:17 +00:00
|
|
|
self.0.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())
|
|
|
|
}
|
|
|
|
}
|