diff --git a/core/src/authentication.rs b/core/src/authentication.rs index 2946f477..c1fbed0d 100644 --- a/core/src/authentication.rs +++ b/core/src/authentication.rs @@ -193,9 +193,10 @@ pub fn get_credentials 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), diff --git a/core/src/spotify_id.rs b/core/src/spotify_id.rs index 0472ca3f..2169ee6f 100644 --- a/core/src/spotify_id.rs +++ b/core/src/spotify_id.rs @@ -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 { - assert!(id.is_ascii()); + pub fn from_base16(id: &str) -> Result { 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 { - assert!(id.is_ascii()); + pub fn from_base62(id: &str) -> Result { 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 { - assert_eq!(data.len(), 16); + pub fn from_raw(data: &[u8]) -> Result { + if data.len() != 16 { + return Err(SpotifyIdError) + }; let high = BigEndian::read_u64(&data[0..8]); let low = BigEndian::read_u64(&data[8..16]);