Set SpotifyId methods to return Result

This commit is contained in:
Sasha Hilton 2018-02-25 03:04:07 +01:00
parent 21d7b618cb
commit 4fb8c71b0a
4 changed files with 18 additions and 17 deletions

View file

@ -680,7 +680,7 @@ impl SpircTask {
let index = self.state.get_playing_track_index();
let track = {
let gid = self.state.get_track()[index as usize].get_gid();
SpotifyId::from_raw(gid)
SpotifyId::from_raw(gid).unwrap()
};
let position = self.state.get_position_ms();

View file

@ -1,6 +1,7 @@
use byteorder::{BigEndian, ByteOrder};
use std;
use std::fmt;
use std::io::Result;
use util::u128;
// Unneeded since 1.21
#[allow(unused_imports)]
@ -13,7 +14,7 @@ const BASE62_DIGITS: &'static [u8] = b"0123456789abcdefghijklmnopqrstuvwxyzABCDE
const BASE16_DIGITS: &'static [u8] = b"0123456789abcdef";
impl SpotifyId {
pub fn from_base16(id: &str) -> SpotifyId {
pub fn from_base16(id: &str) -> Result<SpotifyId> {
assert!(id.is_ascii());
let data = id.as_bytes();
@ -24,10 +25,10 @@ impl SpotifyId {
n = n + u128::from(d);
}
SpotifyId(n)
Ok(SpotifyId(n))
}
pub fn from_base62(id: &str) -> SpotifyId {
pub fn from_base62(id: &str) -> Result<SpotifyId> {
assert!(id.is_ascii());
let data = id.as_bytes();
@ -38,16 +39,16 @@ impl SpotifyId {
n = n + u128::from(d);
}
SpotifyId(n)
Ok(SpotifyId(n))
}
pub fn from_raw(data: &[u8]) -> SpotifyId {
pub fn from_raw(data: &[u8]) -> Result<SpotifyId> {
assert_eq!(data.len(), 16);
let high = BigEndian::read_u64(&data[0..8]);
let low = BigEndian::read_u64(&data[8..16]);
SpotifyId(u128::from_parts(high, low))
Ok(SpotifyId(u128::from_parts(high, low)))
}
pub fn to_base16(&self) -> String {

View file

@ -28,7 +28,7 @@ fn main() {
let password = args[2].to_owned();
let credentials = Credentials::with_password(username, password);
let track = SpotifyId::from_base62(&args[3]);
let track = SpotifyId::from_base62(&args[3]).unwrap();
let backend = audio_backend::find(None).unwrap();

View file

@ -113,7 +113,7 @@ impl Metadata for Track {
let artists = msg.get_artist()
.iter()
.filter(|artist| artist.has_gid())
.map(|artist| SpotifyId::from_raw(artist.get_gid()))
.map(|artist| SpotifyId::from_raw(artist.get_gid()).unwrap())
.collect::<Vec<_>>();
let files = msg.get_file()
@ -127,15 +127,15 @@ impl Metadata for Track {
.collect();
Track {
id: SpotifyId::from_raw(msg.get_gid()),
id: SpotifyId::from_raw(msg.get_gid()).unwrap(),
name: msg.get_name().to_owned(),
duration: msg.get_duration(),
album: SpotifyId::from_raw(msg.get_album().get_gid()),
album: SpotifyId::from_raw(msg.get_album().get_gid()).unwrap(),
artists: artists,
files: files,
alternatives: msg.get_alternative()
.iter()
.map(|alt| SpotifyId::from_raw(alt.get_gid()))
.map(|alt| SpotifyId::from_raw(alt.get_gid()).unwrap())
.collect(),
available: parse_restrictions(msg.get_restriction(), &country, "premium"),
}
@ -153,14 +153,14 @@ impl Metadata for Album {
let artists = msg.get_artist()
.iter()
.filter(|artist| artist.has_gid())
.map(|artist| SpotifyId::from_raw(artist.get_gid()))
.map(|artist| SpotifyId::from_raw(artist.get_gid()).unwrap())
.collect::<Vec<_>>();
let tracks = msg.get_disc()
.iter()
.flat_map(|disc| disc.get_track())
.filter(|track| track.has_gid())
.map(|track| SpotifyId::from_raw(track.get_gid()))
.map(|track| SpotifyId::from_raw(track.get_gid()).unwrap())
.collect::<Vec<_>>();
let covers = msg.get_cover_group()
@ -175,7 +175,7 @@ impl Metadata for Album {
.collect::<Vec<_>>();
Album {
id: SpotifyId::from_raw(msg.get_gid()),
id: SpotifyId::from_raw(msg.get_gid()).unwrap(),
name: msg.get_name().to_owned(),
artists: artists,
tracks: tracks,
@ -202,13 +202,13 @@ impl Metadata for Artist {
.get_track()
.iter()
.filter(|track| track.has_gid())
.map(|track| SpotifyId::from_raw(track.get_gid()))
.map(|track| SpotifyId::from_raw(track.get_gid()).unwrap())
.collect::<Vec<_>>(),
None => Vec::new(),
};
Artist {
id: SpotifyId::from_raw(msg.get_gid()),
id: SpotifyId::from_raw(msg.get_gid()).unwrap(),
name: msg.get_name().to_owned(),
top_tracks: top_tracks,
}