mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
Add custom SpotifyIdError type
This commit is contained in:
parent
084646e21b
commit
0abad9a8f8
2 changed files with 21 additions and 12 deletions
|
@ -193,9 +193,10 @@ pub fn get_credentials<F: FnOnce(&String) -> String>(
|
|||
Some(credentials.clone())
|
||||
}
|
||||
|
||||
(Some(username), None, _) => {
|
||||
Some(Credentials::with_password(username.clone(), prompt(&username)))
|
||||
}
|
||||
(Some(username), None, _) => Some(Credentials::with_password(
|
||||
username.clone(),
|
||||
prompt(&username),
|
||||
)),
|
||||
|
||||
(None, _, Some(credentials)) => Some(credentials),
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use byteorder::{BigEndian, ByteOrder};
|
||||
use std;
|
||||
use std::fmt;
|
||||
use std::io::Result;
|
||||
use util::u128;
|
||||
// Unneeded since 1.21
|
||||
#[allow(unused_imports)]
|
||||
|
@ -10,17 +9,22 @@ use std::ascii::AsciiExt;
|
|||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct SpotifyId(u128);
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct SpotifyIdError;
|
||||
|
||||
const BASE62_DIGITS: &'static [u8] = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const BASE16_DIGITS: &'static [u8] = b"0123456789abcdef";
|
||||
|
||||
impl SpotifyId {
|
||||
pub fn from_base16(id: &str) -> Result<SpotifyId> {
|
||||
assert!(id.is_ascii());
|
||||
pub fn from_base16(id: &str) -> Result<SpotifyId, SpotifyIdError> {
|
||||
let data = id.as_bytes();
|
||||
|
||||
let mut n: u128 = u128::zero();
|
||||
for c in data {
|
||||
let d = BASE16_DIGITS.iter().position(|e| e == c).unwrap() as u8;
|
||||
let d = match BASE16_DIGITS.iter().position(|e| e == c) {
|
||||
None => return Err(SpotifyIdError),
|
||||
Some(x) => x as u8,
|
||||
};
|
||||
n = n * u128::from(16);
|
||||
n = n + u128::from(d);
|
||||
}
|
||||
|
@ -28,13 +32,15 @@ impl SpotifyId {
|
|||
Ok(SpotifyId(n))
|
||||
}
|
||||
|
||||
pub fn from_base62(id: &str) -> Result<SpotifyId> {
|
||||
assert!(id.is_ascii());
|
||||
pub fn from_base62(id: &str) -> Result<SpotifyId, SpotifyIdError> {
|
||||
let data = id.as_bytes();
|
||||
|
||||
let mut n: u128 = u128::zero();
|
||||
for c in data {
|
||||
let d = BASE62_DIGITS.iter().position(|e| e == c).unwrap() as u8;
|
||||
let d = match BASE62_DIGITS.iter().position(|e| e == c) {
|
||||
None => return Err(SpotifyIdError),
|
||||
Some(x) => x as u8,
|
||||
};
|
||||
n = n * u128::from(62);
|
||||
n = n + u128::from(d);
|
||||
}
|
||||
|
@ -42,8 +48,10 @@ impl SpotifyId {
|
|||
Ok(SpotifyId(n))
|
||||
}
|
||||
|
||||
pub fn from_raw(data: &[u8]) -> Result<SpotifyId> {
|
||||
assert_eq!(data.len(), 16);
|
||||
pub fn from_raw(data: &[u8]) -> Result<SpotifyId, SpotifyIdError> {
|
||||
if data.len() != 16 {
|
||||
return Err(SpotifyIdError)
|
||||
};
|
||||
|
||||
let high = BigEndian::read_u64(&data[0..8]);
|
||||
let low = BigEndian::read_u64(&data[8..16]);
|
||||
|
|
Loading…
Reference in a new issue