librespot/core/src/spotify_id.rs

119 lines
3.1 KiB
Rust
Raw Normal View History

2018-02-11 11:37:08 +00:00
use byteorder::{BigEndian, ByteOrder};
2018-02-28 12:29:24 +00:00
use extprim::u128::u128;
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();
let mut n: u128 = u128::zero();
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),
2018-02-28 12:28:57 +00:00
Some(x) => x as u64,
2018-02-25 14:47:57 +00:00
};
2018-02-28 12:28:57 +00:00
n = n * u128::new(16);
n = n + u128::new(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();
let mut n: u128 = u128::zero();
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),
2018-02-28 12:28:57 +00:00
Some(x) => x as u64,
2018-02-25 14:47:57 +00:00
};
2018-02-28 12:28:57 +00:00
n = n * u128::new(62);
n = n + u128::new(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
let high = BigEndian::read_u64(&data[0..8]);
let low = BigEndian::read_u64(&data[8..16]);
2018-02-25 02:04:07 +00:00
Ok(SpotifyId(u128::from_parts(high, low)))
2015-06-23 14:38:29 +00:00
}
pub fn to_base16(&self) -> String {
let &SpotifyId(ref n) = self;
let mut data = [0u8; 32];
2018-02-28 12:28:57 +00:00
for i in 0..32 {
data[31 - i] = BASE16_DIGITS[(n.wrapping_shr(4 * i as u32).low64() & 0xF) as usize];
2015-06-23 14:38:29 +00:00
}
2018-02-28 12:28:57 +00:00
std::str::from_utf8(&data).unwrap().to_owned()
}
pub fn to_base62(&self) -> String {
let &SpotifyId(mut n) = self;
let mut data = [0u8; 22];
let sixty_two = u128::new(62);
for i in 0..22 {
2018-02-28 12:29:24 +00:00
data[21 - i] = BASE62_DIGITS[(n % sixty_two).low64() as usize];
2018-02-28 12:28:57 +00:00
n /= sixty_two;
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] {
let &SpotifyId(ref n) = self;
let mut data = [0u8; 16];
2018-02-28 12:28:57 +00:00
BigEndian::write_u64(&mut data[0..8], n.high64());
BigEndian::write_u64(&mut data[8..16], n.low64());
2015-06-23 14:38:29 +00:00
data
}
}
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())
}
}