introduce track data class

This commit is contained in:
Henrik Friedrichsen
2019-03-06 00:15:28 +01:00
parent 0f3cc41115
commit 91e89c2970
10 changed files with 98 additions and 75 deletions

54
src/track.rs Normal file
View File

@@ -0,0 +1,54 @@
use std::fmt;
use librespot::core::spotify_id::SpotifyId;
use rspotify::spotify::model::track::FullTrack;
#[derive(Clone)]
pub struct Track {
pub id: SpotifyId,
pub duration: u32,
pub artists: String,
pub title: String,
}
impl Track {
pub fn new(track: &FullTrack) -> Track {
let artists_joined = track
.artists
.iter()
.map(|ref artist| artist.name.clone())
.collect::<Vec<String>>()
.join(", ");
Track {
id: SpotifyId::from_base62(&track.id).expect("could not load track"),
duration: track.duration_ms / 1000,
artists: artists_joined,
title: track.name.clone(),
}
}
pub fn duration_str(&self) -> String {
let minutes = self.duration / 60;
let seconds = self.duration % 60;
format!("{:02}:{:02}", minutes, seconds)
}
}
impl fmt::Display for Track {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} - {}", self.artists, self.title)
}
}
impl fmt::Debug for Track {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"({} - {} ({})",
self.artists,
self.title,
self.id.to_base62()
)
}
}