From be2ad9059a47135768259454b7659984c6c28894 Mon Sep 17 00:00:00 2001 From: marcelbuesing Date: Fri, 17 Jan 2020 15:35:46 +0100 Subject: [PATCH 1/6] Migrate to rust 2018 edition --- Cargo.toml | 1 + examples/play.rs | 3 --- examples/playlist_tracks.rs | 8 +------- src/lib.rs | 9 --------- src/main.rs | 28 +++++++--------------------- src/player_event_handler.rs | 1 + 6 files changed, 10 insertions(+), 40 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a42e2b2d..6605b8a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ description = "An open source client library for Spotify, with support for Spoti keywords = ["spotify"] repository = "https://github.com/librespot-org/librespot" readme = "README.md" +edition = "2018" [workspace] diff --git a/examples/play.rs b/examples/play.rs index 87f68825..c619b591 100644 --- a/examples/play.rs +++ b/examples/play.rs @@ -1,6 +1,3 @@ -extern crate librespot; -extern crate tokio_core; - use std::env; use tokio_core::reactor::Core; diff --git a/examples/playlist_tracks.rs b/examples/playlist_tracks.rs index 5d6aec59..3e4202d5 100644 --- a/examples/playlist_tracks.rs +++ b/examples/playlist_tracks.rs @@ -1,11 +1,5 @@ -extern crate log; -extern crate env_logger; - -extern crate librespot; -extern crate tokio_core; -extern crate tokio_io; -extern crate futures; +use env_logger; use std::env; use tokio_core::reactor::Core; diff --git a/src/lib.rs b/src/lib.rs index f73db1ab..610062e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,15 +1,6 @@ #![crate_name = "librespot"] #![cfg_attr(feature = "cargo-clippy", allow(unused_io_amount))] -extern crate base64; -extern crate futures; -extern crate hyper; -extern crate num_bigint; -extern crate protobuf; -extern crate rand; -extern crate tokio_core; -extern crate url; - pub extern crate librespot_audio as audio; pub extern crate librespot_connect as connect; pub extern crate librespot_core as core; diff --git a/src/main.rs b/src/main.rs index e193257b..db197269 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,7 @@ -extern crate env_logger; -extern crate futures; -extern crate getopts; -extern crate librespot; -#[macro_use] -extern crate log; -extern crate hex; -extern crate rpassword; -extern crate sha1; -extern crate tokio_core; -extern crate tokio_io; -extern crate tokio_process; -extern crate tokio_signal; -extern crate url; - use futures::sync::mpsc::UnboundedReceiver; use futures::{Async, Future, Poll, Stream}; use sha1::{Digest, Sha1}; +use log::{error, info, trace, warn}; use std::env; use std::io::{self, stderr, Write}; use std::mem; @@ -40,7 +26,7 @@ use librespot::playback::mixer::{self, Mixer, MixerConfig}; use librespot::playback::player::{Player, PlayerEvent}; mod player_event_handler; -use player_event_handler::run_program_on_events; +use crate::player_event_handler::run_program_on_events; fn device_id(name: &str) -> String { hex::encode(Sha1::digest(name.as_bytes())) @@ -86,10 +72,10 @@ fn list_backends() { #[derive(Clone)] struct Setup { - backend: fn(Option) -> Box, + backend: fn(Option) -> Box, device: Option, - mixer: fn(Option) -> Box, + mixer: fn(Option) -> Box, cache: Option, player_config: PlayerConfig, @@ -367,9 +353,9 @@ struct Main { player_config: PlayerConfig, session_config: SessionConfig, connect_config: ConnectConfig, - backend: fn(Option) -> Box, + backend: fn(Option) -> Box, device: Option, - mixer: fn(Option) -> Box, + mixer: fn(Option) -> Box, mixer_config: MixerConfig, handle: Handle, @@ -378,7 +364,7 @@ struct Main { spirc: Option, spirc_task: Option, - connect: Box>, + connect: Box>, shutdown: bool, diff --git a/src/player_event_handler.rs b/src/player_event_handler.rs index 1e682b98..6da600b8 100644 --- a/src/player_event_handler.rs +++ b/src/player_event_handler.rs @@ -1,3 +1,4 @@ +use log::info; use librespot::playback::player::PlayerEvent; use tokio_process::{Child, CommandExt}; use std::collections::HashMap; From d26590afc536128d22ef560a842aef87964a2d20 Mon Sep 17 00:00:00 2001 From: ashthespy Date: Tue, 8 Oct 2019 11:31:18 +0200 Subject: [PATCH 2/6] Update to Rust 2018 - Fix deprecated Error::cause warnings and missing dyn - Reset max_width - Add rustfmt to Travis - Run rustfmt on full codebase with `cargo fmt --all` - Add rustfmt to Travis - Complete migration to edition 2018 - Replace try! shorthand - Use explicit `dyn Trait` --- .travis.yml | 2 + audio/src/decrypt.rs | 13 +++--- audio/src/fetch.rs | 4 +- audio/src/lib.rs | 2 +- audio/src/libvorbis_decoder.rs | 4 +- connect/src/context.rs | 4 +- connect/src/discovery.rs | 55 ++++++++++++------------ connect/src/lib.rs | 4 +- connect/src/spirc.rs | 51 +++++++++++++--------- core/src/apresolve.rs | 19 ++++---- core/src/audio_key.rs | 10 +++-- core/src/authentication.rs | 31 ++++++------- core/src/cache.rs | 8 ++-- core/src/channel.rs | 2 +- core/src/config.rs | 2 +- core/src/connection/codec.rs | 3 +- core/src/connection/handshake.rs | 26 +++++------ core/src/connection/mod.rs | 46 ++++++++++++-------- core/src/diffie_hellman.rs | 21 +++++---- core/src/keymaster.rs | 6 +-- core/src/lib.rs | 8 ++-- core/src/mercury/mod.rs | 7 +-- core/src/mercury/types.rs | 5 ++- core/src/proxytunnel.rs | 7 ++- core/src/session.rs | 45 +++++++++++-------- core/src/spotify_id.rs | 3 +- core/src/util/mod.rs | 5 ++- core/src/volume.rs | 4 +- examples/play.rs | 4 +- metadata/src/lib.rs | 2 +- playback/src/audio_backend/alsa.rs | 3 +- playback/src/audio_backend/jackaudio.rs | 15 ++++--- playback/src/audio_backend/mod.rs | 8 ++-- playback/src/audio_backend/pipe.rs | 7 ++- playback/src/audio_backend/portaudio.rs | 10 +++-- playback/src/audio_backend/pulseaudio.rs | 6 ++- playback/src/audio_backend/rodio.rs | 17 ++++---- playback/src/mixer/alsamixer.rs | 14 +++--- playback/src/mixer/mod.rs | 17 ++++---- playback/src/mixer/softmixer.rs | 2 +- playback/src/player.rs | 29 ++++++++----- protocol/build.rs | 14 +++--- rustfmt.toml | 3 +- src/main.rs | 19 ++++++-- src/player_event_handler.rs | 2 +- 45 files changed, 331 insertions(+), 238 deletions(-) diff --git a/.travis.yml b/.travis.yml index 182adfdb..0d6d19f6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,12 +28,14 @@ addons: - libsdl2-dev before_script: + - rustup component add rustfmt - mkdir -p ~/.cargo - echo '[target.armv7-unknown-linux-gnueabihf]' > ~/.cargo/config - echo 'linker = "arm-linux-gnueabihf-gcc"' >> ~/.cargo/config - rustup target add armv7-unknown-linux-gnueabihf script: + - cargo fmt --all -- --check - cargo build --locked --no-default-features - cargo build --locked --examples - cargo build --locked --no-default-features --features "with-tremor" diff --git a/audio/src/decrypt.rs b/audio/src/decrypt.rs index d2697e72..818eb34e 100644 --- a/audio/src/decrypt.rs +++ b/audio/src/decrypt.rs @@ -1,16 +1,13 @@ use std::io; -use aes_ctr::Aes128Ctr; -use aes_ctr::stream_cipher::{ - NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek -}; use aes_ctr::stream_cipher::generic_array::GenericArray; +use aes_ctr::stream_cipher::{NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek}; +use aes_ctr::Aes128Ctr; use librespot_core::audio_key::AudioKey; const AUDIO_AESIV: [u8; 16] = [ - 0x72, 0xe0, 0x67, 0xfb, 0xdd, 0xcb, 0xcf, 0x77, - 0xeb, 0xe8, 0xbc, 0x64, 0x3f, 0x63, 0x0d, 0x93, + 0x72, 0xe0, 0x67, 0xfb, 0xdd, 0xcb, 0xcf, 0x77, 0xeb, 0xe8, 0xbc, 0x64, 0x3f, 0x63, 0x0d, 0x93, ]; pub struct AudioDecrypt { @@ -30,7 +27,7 @@ impl AudioDecrypt { impl io::Read for AudioDecrypt { fn read(&mut self, output: &mut [u8]) -> io::Result { - let len = try!(self.reader.read(output)); + let len = self.reader.read(output)?; self.cipher.apply_keystream(&mut output[..len]); @@ -40,7 +37,7 @@ impl io::Read for AudioDecrypt { impl io::Seek for AudioDecrypt { fn seek(&mut self, pos: io::SeekFrom) -> io::Result { - let newpos = try!(self.reader.seek(pos)); + let newpos = self.reader.seek(pos)?; self.cipher.seek(newpos); diff --git a/audio/src/fetch.rs b/audio/src/fetch.rs index 53c95456..a7f80902 100644 --- a/audio/src/fetch.rs +++ b/audio/src/fetch.rs @@ -1009,7 +1009,7 @@ impl Read for AudioFileStreaming { self.position = self.read_file.seek(SeekFrom::Start(offset as u64)).unwrap(); let read_len = min(length, available_length); - let read_len = try!(self.read_file.read(&mut output[..read_len])); + let read_len = self.read_file.read(&mut output[..read_len])?; if download_message_printed { debug!( @@ -1031,7 +1031,7 @@ impl Read for AudioFileStreaming { impl Seek for AudioFileStreaming { fn seek(&mut self, pos: SeekFrom) -> io::Result { - self.position = try!(self.read_file.seek(pos)); + self.position = self.read_file.seek(pos)?; // Do not seek past EOF self.shared .read_position diff --git a/audio/src/lib.rs b/audio/src/lib.rs index 9a82f90e..3e13c079 100644 --- a/audio/src/lib.rs +++ b/audio/src/lib.rs @@ -31,6 +31,6 @@ pub use fetch::{ }; #[cfg(not(any(feature = "with-tremor", feature = "with-vorbis")))] -pub use lewton_decoder::{VorbisDecoder, VorbisError, VorbisPacket}; +pub use crate::lewton_decoder::{VorbisDecoder, VorbisError, VorbisPacket}; #[cfg(any(feature = "with-tremor", feature = "with-vorbis"))] pub use libvorbis_decoder::{VorbisDecoder, VorbisError, VorbisPacket}; diff --git a/audio/src/libvorbis_decoder.rs b/audio/src/libvorbis_decoder.rs index eef1708d..c2198251 100644 --- a/audio/src/libvorbis_decoder.rs +++ b/audio/src/libvorbis_decoder.rs @@ -77,7 +77,7 @@ impl error::Error for VorbisError { error::Error::description(&self.0) } - fn cause(&self) -> Option<&error::Error> { - error::Error::cause(&self.0) + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + error::Error::source(&self.0) } } diff --git a/connect/src/context.rs b/connect/src/context.rs index c59e389e..5a94f6cb 100644 --- a/connect/src/context.rs +++ b/connect/src/context.rs @@ -1,5 +1,5 @@ +use crate::protocol::spirc::TrackRef; use librespot_core::spotify_id::SpotifyId; -use protocol::spirc::TrackRef; use serde; @@ -69,7 +69,7 @@ fn deserialize_protobuf_TrackRef<'d, D>(de: D) -> Result, D::Error where D: serde::Deserializer<'d>, { - let v: Vec = try!(serde::Deserialize::deserialize(de)); + let v: Vec = serde::Deserialize::deserialize(de)?; let track_vec = v .iter() .map(|v| { diff --git a/connect/src/discovery.rs b/connect/src/discovery.rs index de575b15..9779e6f8 100644 --- a/connect/src/discovery.rs +++ b/connect/src/discovery.rs @@ -1,13 +1,13 @@ -use base64; -use sha1::{Sha1, Digest}; -use hmac::{Hmac, Mac}; -use aes_ctr::Aes128Ctr; -use aes_ctr::stream_cipher::{NewStreamCipher, SyncStreamCipher}; use aes_ctr::stream_cipher::generic_array::GenericArray; +use aes_ctr::stream_cipher::{NewStreamCipher, SyncStreamCipher}; +use aes_ctr::Aes128Ctr; +use base64; use futures::sync::mpsc; use futures::{Future, Poll, Stream}; +use hmac::{Hmac, Mac}; use hyper::server::{Http, Request, Response, Service}; use hyper::{self, Get, Post, StatusCode}; +use sha1::{Digest, Sha1}; #[cfg(feature = "with-dns-sd")] use dns_sd::DNSService; @@ -114,21 +114,18 @@ impl Discovery { let base_key = &base_key[..16]; let checksum_key = { - let mut h = HmacSha1::new_varkey(base_key) - .expect("HMAC can take key of any size"); + let mut h = HmacSha1::new_varkey(base_key).expect("HMAC can take key of any size"); h.input(b"checksum"); h.result().code() }; let encryption_key = { - let mut h = HmacSha1::new_varkey(&base_key) - .expect("HMAC can take key of any size"); + let mut h = HmacSha1::new_varkey(&base_key).expect("HMAC can take key of any size"); h.input(b"encryption"); h.result().code() }; - let mut h = HmacSha1::new_varkey(&checksum_key) - .expect("HMAC can take key of any size"); + let mut h = HmacSha1::new_varkey(&checksum_key).expect("HMAC can take key of any size"); h.input(encrypted); if let Err(_) = h.verify(cksum) { warn!("Login error for user {:?}: MAC mismatch", username); @@ -139,7 +136,7 @@ impl Discovery { }); let body = result.to_string(); - return ::futures::finished(Response::new().with_body(body)) + return ::futures::finished(Response::new().with_body(body)); } let decrypted = { @@ -152,7 +149,8 @@ impl Discovery { String::from_utf8(data).unwrap() }; - let credentials = Credentials::with_blob(username.to_owned(), &decrypted, &self.0.device_id); + let credentials = + Credentials::with_blob(username.to_owned(), &decrypted, &self.0.device_id); self.0.tx.unbounded_send(credentials).unwrap(); @@ -175,7 +173,7 @@ impl Service for Discovery { type Request = Request; type Response = Response; type Error = hyper::Error; - type Future = Box>; + type Future = Box>; fn call(&self, request: Request) -> Self::Future { let mut params = BTreeMap::new(); @@ -194,17 +192,18 @@ impl Service for Discovery { body.fold(Vec::new(), |mut acc, chunk| { acc.extend_from_slice(chunk.as_ref()); Ok::<_, hyper::Error>(acc) - }).map(move |body| { - params.extend(url::form_urlencoded::parse(&body).into_owned()); - params - }) - .and_then( - move |params| match (method, params.get("action").map(AsRef::as_ref)) { - (Get, Some("getInfo")) => this.handle_get_info(¶ms), - (Post, Some("addUser")) => this.handle_add_user(¶ms), - _ => this.not_found(), - }, - ), + }) + .map(move |body| { + params.extend(url::form_urlencoded::parse(&body).into_owned()); + params + }) + .and_then(move |params| { + match (method, params.get("action").map(AsRef::as_ref)) { + (Get, Some("getInfo")) => this.handle_get_info(¶ms), + (Post, Some("addUser")) => this.handle_add_user(¶ms), + _ => this.not_found(), + } + }), ) } } @@ -235,7 +234,8 @@ pub fn discovery( &format!("0.0.0.0:{}", port).parse().unwrap(), &handle, move || Ok(discovery.clone()), - ).unwrap() + ) + .unwrap() }; let s_port = serve.incoming_ref().local_addr().port(); @@ -260,7 +260,8 @@ pub fn discovery( None, s_port, &["VERSION=1.0", "CPath=/"], - ).unwrap(); + ) + .unwrap(); #[cfg(not(feature = "with-dns-sd"))] let responder = libmdns::Responder::spawn(&handle)?; diff --git a/connect/src/lib.rs b/connect/src/lib.rs index d0290585..118c85df 100644 --- a/connect/src/lib.rs +++ b/connect/src/lib.rs @@ -15,10 +15,10 @@ extern crate rand; extern crate tokio_core; extern crate url; -extern crate sha1; -extern crate hmac; extern crate aes_ctr; extern crate block_modes; +extern crate hmac; +extern crate sha1; #[cfg(feature = "with-dns-sd")] extern crate dns_sd; diff --git a/connect/src/spirc.rs b/connect/src/spirc.rs index 9594082e..29e6b1d8 100644 --- a/connect/src/spirc.rs +++ b/connect/src/spirc.rs @@ -9,7 +9,11 @@ use rand; use rand::seq::SliceRandom; use serde_json; -use context::StationContext; +use crate::context::StationContext; +use crate::playback::mixer::Mixer; +use crate::playback::player::Player; +use crate::protocol; +use crate::protocol::spirc::{DeviceState, Frame, MessageType, PlayStatus, State, TrackRef}; use librespot_core::config::ConnectConfig; use librespot_core::mercury::MercuryError; use librespot_core::session::Session; @@ -17,14 +21,10 @@ use librespot_core::spotify_id::{SpotifyAudioType, SpotifyId, SpotifyIdError}; use librespot_core::util::SeqGenerator; use librespot_core::version; use librespot_core::volume::Volume; -use playback::mixer::Mixer; -use playback::player::Player; -use protocol; -use protocol::spirc::{DeviceState, Frame, MessageType, PlayStatus, State, TrackRef}; pub struct SpircTask { player: Player, - mixer: Box, + mixer: Box, config: SpircTaskConfig, sequence: SeqGenerator, @@ -33,15 +33,15 @@ pub struct SpircTask { device: DeviceState, state: State, - subscription: Box>, - sender: Box>, + subscription: Box>, + sender: Box>, commands: mpsc::UnboundedReceiver, - end_of_track: Box>, + end_of_track: Box>, shutdown: bool, session: Session, - context_fut: Box>, - autoplay_fut: Box>, + context_fut: Box>, + autoplay_fut: Box>, context: Option, } @@ -221,7 +221,7 @@ impl Spirc { config: ConnectConfig, session: Session, player: Player, - mixer: Box, + mixer: Box, ) -> (Spirc, SpircTask) { debug!("new Spirc[{}]", session.session_id()); @@ -526,7 +526,8 @@ impl SpircTask { if self.state.get_track().len() > 0 { let now = self.now_ms(); - self.state.set_position_ms(frame.get_state().get_position_ms()); + self.state + .set_position_ms(frame.get_state().get_position_ms()); self.state.set_position_measured_at(now as u64); let play = frame.get_state().get_status() == PlayStatus::kPlayStatusPlay; @@ -689,7 +690,8 @@ impl SpircTask { tracks_len - new_index < CONTEXT_FETCH_THRESHOLD ); let context_uri = self.state.get_context_uri().to_owned(); - if (context_uri.starts_with("spotify:station:") || context_uri.starts_with("spotify:dailymix:")) + if (context_uri.starts_with("spotify:station:") + || context_uri.starts_with("spotify:dailymix:")) && ((self.state.get_track().len() as u32) - new_index) < CONTEXT_FETCH_THRESHOLD { self.context_fut = self.resolve_station(&context_uri); @@ -785,13 +787,16 @@ impl SpircTask { self.state.get_position_ms() + diff as u32 } - fn resolve_station(&self, uri: &str) -> Box> { + fn resolve_station( + &self, + uri: &str, + ) -> Box> { let radio_uri = format!("hm://radio-apollo/v3/stations/{}", uri); self.resolve_uri(&radio_uri) } - fn resolve_autoplay_uri(&self, uri: &str) -> Box> { + fn resolve_autoplay_uri(&self, uri: &str) -> Box> { let query_uri = format!("hm://autoplay-enabled/query?uri={}", uri); let request = self.session.mercury().get(query_uri); Box::new(request.and_then(move |response| { @@ -806,11 +811,14 @@ impl SpircTask { })) } - fn resolve_uri(&self, uri: &str) -> Box> { + fn resolve_uri(&self, uri: &str) -> Box> { let request = self.session.mercury().get(uri); Box::new(request.and_then(move |response| { - let data = response.payload.first().expect("Empty payload on context uri"); + let data = response + .payload + .first() + .expect("Empty payload on context uri"); let response: serde_json::Value = serde_json::from_slice(&data).unwrap(); Ok(response) @@ -828,7 +836,8 @@ impl SpircTask { track_vec.drain(0..head); } track_vec.extend_from_slice(&new_tracks); - self.state.set_track(protobuf::RepeatedField::from_vec(track_vec)); + self.state + .set_track(protobuf::RepeatedField::from_vec(track_vec)); // Update playing index if let Some(new_index) = self @@ -849,7 +858,9 @@ impl SpircTask { let context_uri = frame.get_state().get_context_uri().to_owned(); let tracks = frame.get_state().get_track(); debug!("Frame has {:?} tracks", tracks.len()); - if context_uri.starts_with("spotify:station:") || context_uri.starts_with("spotify:dailymix:") { + if context_uri.starts_with("spotify:station:") + || context_uri.starts_with("spotify:dailymix:") + { self.context_fut = self.resolve_station(&context_uri); } else if self.config.autoplay { info!("Fetching autoplay context uri"); diff --git a/core/src/apresolve.rs b/core/src/apresolve.rs index bcb7bfe1..94d94244 100644 --- a/core/src/apresolve.rs +++ b/core/src/apresolve.rs @@ -10,7 +10,7 @@ use std::str::FromStr; use tokio_core::reactor::Handle; use url::Url; -error_chain!{} +error_chain! {} #[derive(Clone, Debug, Serialize, Deserialize)] pub struct APResolveData { @@ -21,7 +21,7 @@ fn apresolve( handle: &Handle, proxy: &Option, ap_port: &Option, -) -> Box> { +) -> Box> { let url = Uri::from_str(APRESOLVE_ENDPOINT).expect("invalid AP resolve URL"); let use_proxy = proxy.is_some(); @@ -52,19 +52,20 @@ fn apresolve( }) }); let body = body.then(|result| result.chain_err(|| "HTTP error")); - let body = body.and_then(|body| String::from_utf8(body).chain_err(|| "invalid UTF8 in response")); + let body = + body.and_then(|body| String::from_utf8(body).chain_err(|| "invalid UTF8 in response")); - let data = - body.and_then(|body| serde_json::from_str::(&body).chain_err(|| "invalid JSON")); + let data = body + .and_then(|body| serde_json::from_str::(&body).chain_err(|| "invalid JSON")); let p = ap_port.clone(); let ap = data.and_then(move |data| { let mut aps = data.ap_list.iter().filter(|ap| { if p.is_some() { - Uri::from_str(ap) - .ok() - .map_or(false, |uri| uri.port().map_or(false, |port| port == p.unwrap())) + Uri::from_str(ap).ok().map_or(false, |uri| { + uri.port().map_or(false, |port| port == p.unwrap()) + }) } else if use_proxy { // It is unlikely that the proxy will accept CONNECT on anything other than 443. Uri::from_str(ap) @@ -86,7 +87,7 @@ pub(crate) fn apresolve_or_fallback( handle: &Handle, proxy: &Option, ap_port: &Option, -) -> Box> +) -> Box> where E: 'static, { diff --git a/core/src/audio_key.rs b/core/src/audio_key.rs index 410cc2c9..1e5310c2 100644 --- a/core/src/audio_key.rs +++ b/core/src/audio_key.rs @@ -5,8 +5,8 @@ use futures::{Async, Future, Poll}; use std::collections::HashMap; use std::io::Write; -use spotify_id::{FileId, SpotifyId}; -use util::SeqGenerator; +use crate::spotify_id::{FileId, SpotifyId}; +use crate::util::SeqGenerator; #[derive(Debug, Hash, PartialEq, Eq, Copy, Clone)] pub struct AudioKey(pub [u8; 16]); @@ -35,7 +35,11 @@ impl AudioKeyManager { let _ = sender.send(Ok(AudioKey(key))); } 0xe => { - warn!("error audio key {:x} {:x}", data.as_ref()[0], data.as_ref()[1]); + warn!( + "error audio key {:x} {:x}", + data.as_ref()[0], + data.as_ref()[1] + ); let _ = sender.send(Err(AudioKeyError)); } _ => (), diff --git a/core/src/authentication.rs b/core/src/authentication.rs index 07c014f9..36cbd439 100644 --- a/core/src/authentication.rs +++ b/core/src/authentication.rs @@ -1,18 +1,18 @@ +use aes::Aes192; use base64; use byteorder::{BigEndian, ByteOrder}; -use aes::Aes192; use hmac::Hmac; -use sha1::{Sha1, Digest}; use pbkdf2::pbkdf2; use protobuf::ProtobufEnum; use serde; use serde_json; +use sha1::{Digest, Sha1}; use std::fs::File; use std::io::{self, Read, Write}; use std::ops::FnOnce; use std::path::Path; -use protocol::authentication::AuthenticationType; +use crate::protocol::authentication::AuthenticationType; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Credentials { @@ -40,24 +40,24 @@ impl Credentials { pub fn with_blob(username: String, encrypted_blob: &str, device_id: &str) -> Credentials { fn read_u8(stream: &mut R) -> io::Result { let mut data = [0u8]; - try!(stream.read_exact(&mut data)); + stream.read_exact(&mut data)?; Ok(data[0]) } fn read_int(stream: &mut R) -> io::Result { - let lo = try!(read_u8(stream)) as u32; + let lo = read_u8(stream)? as u32; if lo & 0x80 == 0 { return Ok(lo); } - let hi = try!(read_u8(stream)) as u32; + let hi = read_u8(stream)? as u32; Ok(lo & 0x7f | hi << 7) } fn read_bytes(stream: &mut R) -> io::Result> { - let length = try!(read_int(stream)); + let length = read_int(stream)?; let mut data = vec![0u8; length as usize]; - try!(stream.read_exact(&mut data)); + stream.read_exact(&mut data)?; Ok(data) } @@ -76,9 +76,9 @@ impl Credentials { // decrypt data using ECB mode without padding let blob = { - use aes::block_cipher_trait::BlockCipher; - use aes::block_cipher_trait::generic_array::GenericArray; use aes::block_cipher_trait::generic_array::typenum::Unsigned; + use aes::block_cipher_trait::generic_array::GenericArray; + use aes::block_cipher_trait::BlockCipher; let mut data = base64::decode(encrypted_blob).unwrap(); let cipher = Aes192::new(GenericArray::from_slice(&key)); @@ -148,7 +148,7 @@ where T: ProtobufEnum, D: serde::Deserializer<'de>, { - let v: i32 = try!(serde::Deserialize::deserialize(de)); + let v: i32 = serde::Deserialize::deserialize(de)?; T::from_i32(v).ok_or_else(|| serde::de::Error::custom("Invalid enum value")) } @@ -164,7 +164,7 @@ fn deserialize_base64<'de, D>(de: D) -> Result, D::Error> where D: serde::Deserializer<'de>, { - let v: String = try!(serde::Deserialize::deserialize(de)); + let v: String = serde::Deserialize::deserialize(de)?; base64::decode(&v).map_err(|e| serde::de::Error::custom(e.to_string())) } @@ -181,9 +181,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/cache.rs b/core/src/cache.rs index 908ba294..9ee0ae18 100644 --- a/core/src/cache.rs +++ b/core/src/cache.rs @@ -5,9 +5,9 @@ use std::io::Read; use std::path::Path; use std::path::PathBuf; -use authentication::Credentials; -use spotify_id::FileId; -use volume::Volume; +use crate::authentication::Credentials; +use crate::spotify_id::FileId; +use crate::volume::Volume; #[derive(Clone)] pub struct Cache { @@ -80,7 +80,7 @@ impl Cache { File::open(self.file_path(file)).ok() } - pub fn save_file(&self, file: FileId, contents: &mut Read) { + pub fn save_file(&self, file: FileId, contents: &mut dyn Read) { if self.use_audio_cache { let path = self.file_path(file); diff --git a/core/src/channel.rs b/core/src/channel.rs index a4785eb8..06488b90 100644 --- a/core/src/channel.rs +++ b/core/src/channel.rs @@ -5,7 +5,7 @@ use futures::{Async, Poll, Stream}; use std::collections::HashMap; use std::time::Instant; -use util::SeqGenerator; +use crate::util::SeqGenerator; component! { ChannelManager : ChannelManagerInner { diff --git a/core/src/config.rs b/core/src/config.rs index 7c053217..38be4597 100644 --- a/core/src/config.rs +++ b/core/src/config.rs @@ -3,7 +3,7 @@ use std::str::FromStr; use url::Url; use uuid::Uuid; -use version; +use crate::version; #[derive(Clone, Debug)] pub struct SessionConfig { diff --git a/core/src/connection/codec.rs b/core/src/connection/codec.rs index 60634fc2..fa4cd9d9 100644 --- a/core/src/connection/codec.rs +++ b/core/src/connection/codec.rs @@ -88,7 +88,8 @@ impl Decoder for APCodec { let mut payload = buf.split_to(size + MAC_SIZE); - self.decode_cipher.decrypt(&mut payload.get_mut(..size).unwrap()); + self.decode_cipher + .decrypt(&mut payload.get_mut(..size).unwrap()); let mac = payload.split_off(size); self.decode_cipher.check_mac(mac.as_ref())?; diff --git a/core/src/connection/handshake.rs b/core/src/connection/handshake.rs index f291f13f..220ab6e8 100644 --- a/core/src/connection/handshake.rs +++ b/core/src/connection/handshake.rs @@ -1,9 +1,9 @@ use byteorder::{BigEndian, ByteOrder, WriteBytesExt}; -use hmac::{Hmac, Mac}; -use sha1::Sha1; use futures::{Async, Future, Poll}; +use hmac::{Hmac, Mac}; use protobuf::{self, Message}; use rand::thread_rng; +use sha1::Sha1; use std::io::{self, Read}; use std::marker::PhantomData; use tokio_codec::{Decoder, Framed}; @@ -11,10 +11,10 @@ use tokio_io::io::{read_exact, write_all, ReadExact, Window, WriteAll}; use tokio_io::{AsyncRead, AsyncWrite}; use super::codec::APCodec; -use diffie_hellman::DHLocalKeys; -use protocol; -use protocol::keyexchange::{APResponseMessage, ClientHello, ClientResponsePlaintext}; -use util; +use crate::diffie_hellman::DHLocalKeys; +use crate::protocol; +use crate::protocol::keyexchange::{APResponseMessage, ClientHello, ClientResponsePlaintext}; +use crate::util; pub struct Handshake { keys: DHLocalKeys, @@ -62,7 +62,8 @@ impl Future for Handshake { .to_owned(); let shared_secret = self.keys.shared_secret(&remote_key); - let (challenge, send_key, recv_key) = compute_keys(&shared_secret, &accumulator); + let (challenge, send_key, recv_key) = + compute_keys(&shared_secret, &accumulator); let codec = APCodec::new(&send_key, &recv_key); let write = client_response(connection, challenge); @@ -92,7 +93,10 @@ fn client_hello(connection: T, gc: Vec) -> WriteAll (Vec, Vec, Vec< let mut data = Vec::with_capacity(0x64); for i in 1..6 { - let mut mac = HmacSha1::new_varkey(&shared_secret) - .expect("HMAC can take key of any size"); + let mut mac = HmacSha1::new_varkey(&shared_secret).expect("HMAC can take key of any size"); mac.input(packets); mac.input(&[i]); data.extend_from_slice(&mac.result().code()); } - let mut mac = HmacSha1::new_varkey(&data[..0x14]) - .expect("HMAC can take key of any size");; + let mut mac = HmacSha1::new_varkey(&data[..0x14]).expect("HMAC can take key of any size"); mac.input(packets); ( diff --git a/core/src/connection/mod.rs b/core/src/connection/mod.rs index 91b46c80..5f9b3dc3 100644 --- a/core/src/connection/mod.rs +++ b/core/src/connection/mod.rs @@ -8,15 +8,15 @@ use futures::{Future, Sink, Stream}; use protobuf::{self, Message}; use std::io; use std::net::ToSocketAddrs; +use tokio_codec::Framed; use tokio_core::net::TcpStream; use tokio_core::reactor::Handle; -use tokio_codec::Framed; use url::Url; -use authentication::Credentials; -use version; +use crate::authentication::Credentials; +use crate::version; -use proxytunnel; +use crate::proxytunnel; pub type Transport = Framed; @@ -24,7 +24,7 @@ pub fn connect( addr: String, handle: &Handle, proxy: &Option, -) -> Box> { +) -> Box> { let (addr, connect_url) = match *proxy { Some(ref url) => { info!("Using proxy \"{}\"", url); @@ -48,23 +48,31 @@ pub fn authenticate( transport: Transport, credentials: Credentials, device_id: String, -) -> Box> { - use protocol::authentication::{APWelcome, ClientResponseEncrypted, CpuFamily, Os}; - use protocol::keyexchange::APLoginFailed; +) -> Box> { + use crate::protocol::authentication::{APWelcome, ClientResponseEncrypted, CpuFamily, Os}; + use crate::protocol::keyexchange::APLoginFailed; let mut packet = ClientResponseEncrypted::new(); - packet.mut_login_credentials().set_username(credentials.username); - packet.mut_login_credentials().set_typ(credentials.auth_type); + packet + .mut_login_credentials() + .set_username(credentials.username); + packet + .mut_login_credentials() + .set_typ(credentials.auth_type); packet .mut_login_credentials() .set_auth_data(credentials.auth_data); - packet.mut_system_info().set_cpu_family(CpuFamily::CPU_UNKNOWN); + packet + .mut_system_info() + .set_cpu_family(CpuFamily::CPU_UNKNOWN); packet.mut_system_info().set_os(Os::OS_UNKNOWN); - packet.mut_system_info().set_system_information_string(format!( - "librespot_{}_{}", - version::short_sha(), - version::build_id() - )); + packet + .mut_system_info() + .set_system_information_string(format!( + "librespot_{}_{}", + version::short_sha(), + version::build_id() + )); packet.mut_system_info().set_device_id(device_id); packet.set_version_string(version::version_string()); @@ -77,7 +85,8 @@ pub fn authenticate( .and_then(|transport| transport.into_future().map_err(|(err, _stream)| err)) .and_then(|(packet, transport)| match packet { Some((0xac, data)) => { - let welcome_data: APWelcome = protobuf::parse_from_bytes(data.as_ref()).unwrap(); + let welcome_data: APWelcome = + protobuf::parse_from_bytes(data.as_ref()).unwrap(); let reusable_credentials = Credentials { username: welcome_data.get_canonical_username().to_owned(), @@ -89,7 +98,8 @@ pub fn authenticate( } Some((0xad, data)) => { - let error_data: APLoginFailed = protobuf::parse_from_bytes(data.as_ref()).unwrap(); + let error_data: APLoginFailed = + protobuf::parse_from_bytes(data.as_ref()).unwrap(); panic!( "Authentication failed with reason: {:?}", error_data.get_error_code() diff --git a/core/src/diffie_hellman.rs b/core/src/diffie_hellman.rs index b8e41426..dec34a3b 100644 --- a/core/src/diffie_hellman.rs +++ b/core/src/diffie_hellman.rs @@ -2,17 +2,18 @@ use num_bigint::BigUint; use num_traits::FromPrimitive; use rand::Rng; -use util; +use crate::util; lazy_static! { pub static ref DH_GENERATOR: BigUint = BigUint::from_u64(0x2).unwrap(); pub static ref DH_PRIME: BigUint = BigUint::from_bytes_be(&[ - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, - 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, 0xcc, 0x74, - 0x02, 0x0b, 0xbe, 0xa6, 0x3b, 0x13, 0x9b, 0x22, 0x51, 0x4a, 0x08, 0x79, 0x8e, 0x34, 0x04, 0xdd, - 0xef, 0x95, 0x19, 0xb3, 0xcd, 0x3a, 0x43, 0x1b, 0x30, 0x2b, 0x0a, 0x6d, 0xf2, 0x5f, 0x14, 0x37, - 0x4f, 0xe1, 0x35, 0x6d, 0x6d, 0x51, 0xc2, 0x45, 0xe4, 0x85, 0xb5, 0x76, 0x62, 0x5e, 0x7e, 0xc6, - 0xf4, 0x4c, 0x42, 0xe9, 0xa6, 0x3a, 0x36, 0x20, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, + 0x34, 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, + 0xcc, 0x74, 0x02, 0x0b, 0xbe, 0xa6, 0x3b, 0x13, 0x9b, 0x22, 0x51, 0x4a, 0x08, 0x79, 0x8e, + 0x34, 0x04, 0xdd, 0xef, 0x95, 0x19, 0xb3, 0xcd, 0x3a, 0x43, 0x1b, 0x30, 0x2b, 0x0a, 0x6d, + 0xf2, 0x5f, 0x14, 0x37, 0x4f, 0xe1, 0x35, 0x6d, 0x6d, 0x51, 0xc2, 0x45, 0xe4, 0x85, 0xb5, + 0x76, 0x62, 0x5e, 0x7e, 0xc6, 0xf4, 0x4c, 0x42, 0xe9, 0xa6, 0x3a, 0x36, 0x20, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, ]); } @@ -39,7 +40,11 @@ impl DHLocalKeys { } pub fn shared_secret(&self, remote_key: &[u8]) -> Vec { - let shared_key = util::powm(&BigUint::from_bytes_be(remote_key), &self.private_key, &DH_PRIME); + let shared_key = util::powm( + &BigUint::from_bytes_be(remote_key), + &self.private_key, + &DH_PRIME, + ); shared_key.to_bytes_be() } } diff --git a/core/src/keymaster.rs b/core/src/keymaster.rs index 4d82ae07..f2d7b772 100644 --- a/core/src/keymaster.rs +++ b/core/src/keymaster.rs @@ -1,8 +1,8 @@ use futures::Future; use serde_json; -use mercury::MercuryError; -use session::Session; +use crate::mercury::MercuryError; +use crate::session::Session; #[derive(Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] @@ -17,7 +17,7 @@ pub fn get_token( session: &Session, client_id: &str, scopes: &str, -) -> Box> { +) -> Box> { let url = format!( "hm://keymaster/token/authenticated?client_id={}&scope={}", client_id, scopes diff --git a/core/src/lib.rs b/core/src/lib.rs index e384d14b..c65878c2 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -11,29 +11,29 @@ extern crate log; #[macro_use] extern crate serde_derive; +extern crate aes; extern crate base64; extern crate byteorder; extern crate bytes; +extern crate hmac; extern crate httparse; extern crate hyper; extern crate hyper_proxy; extern crate num_bigint; extern crate num_integer; extern crate num_traits; +extern crate pbkdf2; extern crate protobuf; extern crate rand; extern crate serde; extern crate serde_json; +extern crate sha1; extern crate shannon; extern crate tokio_codec; extern crate tokio_core; extern crate tokio_io; extern crate url; extern crate uuid; -extern crate sha1; -extern crate hmac; -extern crate pbkdf2; -extern crate aes; extern crate librespot_protocol as protocol; diff --git a/core/src/mercury/mod.rs b/core/src/mercury/mod.rs index 0b69d8ee..89a1ff1f 100644 --- a/core/src/mercury/mod.rs +++ b/core/src/mercury/mod.rs @@ -1,13 +1,13 @@ +use crate::protocol; use byteorder::{BigEndian, ByteOrder}; use bytes::Bytes; use futures::sync::{mpsc, oneshot}; use futures::{Async, Future, Poll}; use protobuf; -use protocol; use std::collections::HashMap; use std::mem; -use util::SeqGenerator; +use crate::util::SeqGenerator; mod types; pub use self::types::*; @@ -95,7 +95,8 @@ impl MercuryManager { pub fn subscribe>( &self, uri: T, - ) -> Box, Error = MercuryError>> { + ) -> Box, Error = MercuryError>> + { let uri = uri.into(); let request = self.request(MercuryRequest { method: MercuryMethod::SUB, diff --git a/core/src/mercury/types.rs b/core/src/mercury/types.rs index 23f64c45..57cedce5 100644 --- a/core/src/mercury/types.rs +++ b/core/src/mercury/types.rs @@ -2,7 +2,7 @@ use byteorder::{BigEndian, WriteBytesExt}; use protobuf::Message; use std::io::Write; -use protocol; +use crate::protocol; #[derive(Debug, PartialEq, Eq)] pub enum MercuryMethod { @@ -37,7 +37,8 @@ impl ToString for MercuryMethod { MercuryMethod::SUB => "SUB", MercuryMethod::UNSUB => "UNSUB", MercuryMethod::SEND => "SEND", - }.to_owned() + } + .to_owned() } } diff --git a/core/src/proxytunnel.rs b/core/src/proxytunnel.rs index 5e07db99..2b9df5a8 100644 --- a/core/src/proxytunnel.rs +++ b/core/src/proxytunnel.rs @@ -57,7 +57,9 @@ impl Future for ProxyTunnel { let mut response = httparse::Response::new(&mut headers); let status = match response.parse(&buf) { Ok(status) => status, - Err(err) => return Err(io::Error::new(io::ErrorKind::Other, err.description())), + Err(err) => { + return Err(io::Error::new(io::ErrorKind::Other, err.description())) + } }; if status.is_complete() { @@ -102,7 +104,8 @@ fn proxy_connect(connection: T, connect_url: &str) -> WriteAll, handle: Handle, - ) -> Box> { - let access_point = apresolve_or_fallback::(&handle, &config.proxy, &config.ap_port); + ) -> Box> { + let access_point = + apresolve_or_fallback::(&handle, &config.proxy, &config.ap_port); let handle_ = handle.clone(); let proxy = config.proxy.clone(); @@ -64,8 +65,9 @@ impl Session { }); let device_id = config.device_id.clone(); - let authentication = connection - .and_then(move |connection| connection::authenticate(connection, credentials, device_id)); + let authentication = connection.and_then(move |connection| { + connection::authenticate(connection, credentials, device_id) + }); let result = authentication.map(move |(transport, reusable_credentials)| { info!("Authenticated as \"{}\" !", reusable_credentials.username); @@ -97,7 +99,7 @@ impl Session { config: SessionConfig, cache: Option, username: String, - ) -> (Session, Box>) { + ) -> (Session, Box>) { let (sink, stream) = transport.split(); let (sender_tx, sender_rx) = mpsc::unbounded(); @@ -133,7 +135,11 @@ impl Session { .map(|_| ()); let receiver_task = DispatchTask(stream, session.weak()); - let task = Box::new((receiver_task, sender_task).into_future().map(|((), ())| ())); + let task = Box::new( + (receiver_task, sender_task) + .into_future() + .map(|((), ())| ()), + ); (session, task) } @@ -197,7 +203,7 @@ impl Session { 0x9 | 0xa => self.channel().dispatch(cmd, data), 0xd | 0xe => self.audio_key().dispatch(cmd, data), - 0xb2...0xb6 => self.mercury().dispatch(cmd, data), + 0xb2..=0xb6 => self.mercury().dispatch(cmd, data), _ => (), } } @@ -289,7 +295,8 @@ where session.shutdown(); return Err(From::from(e)); } - }.expect("connection closed"); + } + .expect("connection closed"); session.dispatch(cmd, data); } diff --git a/core/src/spotify_id.rs b/core/src/spotify_id.rs index e6f0cdd8..1a5fcd2e 100644 --- a/core/src/spotify_id.rs +++ b/core/src/spotify_id.rs @@ -17,7 +17,8 @@ pub struct SpotifyId { #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub struct SpotifyIdError; -const BASE62_DIGITS: &'static [u8] = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; +const BASE62_DIGITS: &'static [u8] = + b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; const BASE16_DIGITS: &'static [u8] = b"0123456789abcdef"; impl SpotifyId { diff --git a/core/src/util/mod.rs b/core/src/util/mod.rs index c91cac97..5c1e50f5 100644 --- a/core/src/util/mod.rs +++ b/core/src/util/mod.rs @@ -6,7 +6,10 @@ use std::mem; use std::ops::{Mul, Rem, Shr}; pub fn rand_vec(rng: &mut G, size: usize) -> Vec { - ::std::iter::repeat(()).map(|()| rng.gen()).take(size).collect() + ::std::iter::repeat(()) + .map(|()| rng.gen()) + .take(size) + .collect() } pub fn powm(base: &BigUint, exp: &BigUint, modulus: &BigUint) -> BigUint { diff --git a/core/src/volume.rs b/core/src/volume.rs index 24a3d3f9..6b456d1f 100644 --- a/core/src/volume.rs +++ b/core/src/volume.rs @@ -21,7 +21,9 @@ impl Volume { // write volume to file fn save_to_writer(&self, writer: &mut W) { - writer.write_all(self.volume.to_string().as_bytes()).unwrap(); + writer + .write_all(self.volume.to_string().as_bytes()) + .unwrap(); } pub(crate) fn save_to_file>(&self, path: P) { diff --git a/examples/play.rs b/examples/play.rs index c619b591..6888ebb4 100644 --- a/examples/play.rs +++ b/examples/play.rs @@ -34,7 +34,9 @@ fn main() { .run(Session::connect(session_config, credentials, None, handle)) .unwrap(); - let (player, _) = Player::new(player_config, session.clone(), None, move || (backend)(None)); + let (player, _) = Player::new(player_config, session.clone(), None, move || { + (backend)(None) + }); println!("Playing..."); core.run(player.load(track, true, 0)).unwrap(); diff --git a/metadata/src/lib.rs b/metadata/src/lib.rs index 677e4669..6ba32f4d 100644 --- a/metadata/src/lib.rs +++ b/metadata/src/lib.rs @@ -19,7 +19,7 @@ use librespot_core::mercury::MercuryError; use librespot_core::session::Session; use librespot_core::spotify_id::{FileId, SpotifyAudioType, SpotifyId}; -pub use protocol::metadata::AudioFile_Format as FileFormat; +pub use crate::protocol::metadata::AudioFile_Format as FileFormat; fn countrylist_contains(list: &str, country: &str) -> bool { list.chunks(2).any(|cc| cc == country) diff --git a/playback/src/audio_backend/alsa.rs b/playback/src/audio_backend/alsa.rs index 98e7c8f7..8bdcb9d1 100644 --- a/playback/src/audio_backend/alsa.rs +++ b/playback/src/audio_backend/alsa.rs @@ -64,7 +64,8 @@ impl Open for AlsaSink { } Some(device) => device, None => "default", - }.to_string(); + } + .to_string(); AlsaSink(None, name) } diff --git a/playback/src/audio_backend/jackaudio.rs b/playback/src/audio_backend/jackaudio.rs index 3b118f38..54cf8b4a 100644 --- a/playback/src/audio_backend/jackaudio.rs +++ b/playback/src/audio_backend/jackaudio.rs @@ -1,7 +1,7 @@ use super::{Open, Sink}; use jack::prelude::{ - client_options, AsyncClient, AudioOutPort, AudioOutSpec, Client, JackControl, Port, ProcessHandler, - ProcessScope, + client_options, AsyncClient, AudioOutPort, AudioOutSpec, Client, JackControl, Port, + ProcessHandler, ProcessScope, }; use std::io; use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; @@ -45,9 +45,14 @@ impl Open for JackSink { info!("Using jack sink!"); let client_name = client_name.unwrap_or("librespot".to_string()); - let (client, _status) = Client::new(&client_name[..], client_options::NO_START_SERVER).unwrap(); - let ch_r = client.register_port("out_0", AudioOutSpec::default()).unwrap(); - let ch_l = client.register_port("out_1", AudioOutSpec::default()).unwrap(); + let (client, _status) = + Client::new(&client_name[..], client_options::NO_START_SERVER).unwrap(); + let ch_r = client + .register_port("out_0", AudioOutSpec::default()) + .unwrap(); + let ch_l = client + .register_port("out_1", AudioOutSpec::default()) + .unwrap(); // buffer for samples from librespot (~10ms) let (tx, rx) = sync_channel(2 * 1024 * 4); let jack_data = JackData { diff --git a/playback/src/audio_backend/mod.rs b/playback/src/audio_backend/mod.rs index bdf309a8..441252c6 100644 --- a/playback/src/audio_backend/mod.rs +++ b/playback/src/audio_backend/mod.rs @@ -1,7 +1,7 @@ use std::io; pub trait Open { - fn open(Option) -> Self; + fn open(_: Option) -> Self; } pub trait Sink { @@ -10,7 +10,7 @@ pub trait Sink { fn write(&mut self, data: &[i16]) -> io::Result<()>; } -fn mk_sink(device: Option) -> Box { +fn mk_sink(device: Option) -> Box { Box::new(S::open(device)) } @@ -46,7 +46,7 @@ use self::sdl::SdlSink; mod pipe; use self::pipe::StdoutSink; -pub const BACKENDS: &'static [(&'static str, fn(Option) -> Box)] = &[ +pub const BACKENDS: &'static [(&'static str, fn(Option) -> Box)] = &[ #[cfg(feature = "alsa-backend")] ("alsa", mk_sink::), #[cfg(feature = "portaudio-backend")] @@ -62,7 +62,7 @@ pub const BACKENDS: &'static [(&'static str, fn(Option) -> Box)] = ("pipe", mk_sink::), ]; -pub fn find(name: Option) -> Option) -> Box> { +pub fn find(name: Option) -> Option) -> Box> { if let Some(name) = name { BACKENDS .iter() diff --git a/playback/src/audio_backend/pipe.rs b/playback/src/audio_backend/pipe.rs index 414e1929..2adafe11 100644 --- a/playback/src/audio_backend/pipe.rs +++ b/playback/src/audio_backend/pipe.rs @@ -4,7 +4,7 @@ use std::io::{self, Write}; use std::mem; use std::slice; -pub struct StdoutSink(Box); +pub struct StdoutSink(Box); impl Open for StdoutSink { fn open(path: Option) -> StdoutSink { @@ -28,7 +28,10 @@ impl Sink for StdoutSink { fn write(&mut self, data: &[i16]) -> io::Result<()> { let data: &[u8] = unsafe { - slice::from_raw_parts(data.as_ptr() as *const u8, data.len() * mem::size_of::()) + slice::from_raw_parts( + data.as_ptr() as *const u8, + data.len() * mem::size_of::(), + ) }; self.0.write_all(data)?; diff --git a/playback/src/audio_backend/portaudio.rs b/playback/src/audio_backend/portaudio.rs index 19a0bf09..31397bfb 100644 --- a/playback/src/audio_backend/portaudio.rs +++ b/playback/src/audio_backend/portaudio.rs @@ -11,7 +11,7 @@ pub struct PortAudioSink<'a>( StreamParameters, ); -fn output_devices() -> Box> { +fn output_devices() -> Box> { let count = portaudio_rs::device::get_count().unwrap(); let devices = (0..count) .filter_map(|idx| portaudio_rs::device::get_info(idx).map(|info| (idx, info))) @@ -51,7 +51,8 @@ impl<'a> Open for PortAudioSink<'a> { } Some(device) => find_output(device), None => get_default_output_index(), - }.expect("Could not find device"); + } + .expect("Could not find device"); let info = portaudio_rs::device::get_info(device_idx); let latency = match info { @@ -81,8 +82,9 @@ impl<'a> Sink for PortAudioSink<'a> { FRAMES_PER_BUFFER_UNSPECIFIED, StreamFlags::empty(), None, - ).unwrap(), - );; + ) + .unwrap(), + ); } self.0.as_mut().unwrap().start().unwrap(); diff --git a/playback/src/audio_backend/pulseaudio.rs b/playback/src/audio_backend/pulseaudio.rs index 88f62806..e844b0d6 100644 --- a/playback/src/audio_backend/pulseaudio.rs +++ b/playback/src/audio_backend/pulseaudio.rs @@ -14,7 +14,11 @@ pub struct PulseAudioSink { desc: CString, } -fn call_pulseaudio(f: F, fail_check: FailCheck, kind: io::ErrorKind) -> io::Result +fn call_pulseaudio( + f: F, + fail_check: FailCheck, + kind: io::ErrorKind, +) -> io::Result where T: Copy, F: Fn(*mut libc::c_int) -> T, diff --git a/playback/src/audio_backend/rodio.rs b/playback/src/audio_backend/rodio.rs index c4b9c927..4ce09f6f 100644 --- a/playback/src/audio_backend/rodio.rs +++ b/playback/src/audio_backend/rodio.rs @@ -1,8 +1,8 @@ use super::{Open, Sink}; -extern crate rodio; extern crate cpal; -use std::{io, thread, time}; +extern crate rodio; use std::process::exit; +use std::{io, thread, time}; pub struct RodioSink { rodio_sink: rodio::Sink, @@ -14,7 +14,7 @@ fn list_formats(ref device: &rodio::Device) { Err(e) => { warn!("Error getting default rodio::Sink format: {:?}", e); return; - }, + } }; let mut output_formats = match device.supported_output_formats() { @@ -22,13 +22,16 @@ fn list_formats(ref device: &rodio::Device) { Err(e) => { warn!("Error getting supported rodio::Sink formats: {:?}", e); return; - }, + } }; if output_formats.peek().is_some() { debug!(" Available formats:"); for format in output_formats { - let s = format!("{}ch, {:?}, min {:?}, max {:?}", format.channels, format.data_type, format.min_sample_rate, format.max_sample_rate); + let s = format!( + "{}ch, {:?}, min {:?}, max {:?}", + format.channels, format.data_type, format.min_sample_rate, format.max_sample_rate + ); if format == default_fmt { debug!(" (default) {}", s); } else { @@ -79,9 +82,7 @@ impl Open for RodioSink { } let sink = rodio::Sink::new(&rodio_device); - RodioSink { - rodio_sink: sink, - } + RodioSink { rodio_sink: sink } } } diff --git a/playback/src/mixer/alsamixer.rs b/playback/src/mixer/alsamixer.rs index 5c77d470..a906c2e0 100644 --- a/playback/src/mixer/alsamixer.rs +++ b/playback/src/mixer/alsamixer.rs @@ -10,13 +10,17 @@ pub struct AlsaMixer { } impl AlsaMixer { - fn map_volume(&self, set_volume: Option) -> Result<(u16), Box> { + fn map_volume(&self, set_volume: Option) -> Result<(u16), Box> { let mixer = alsa::mixer::Mixer::new(&self.config.card, false)?; let sid = alsa::mixer::SelemId::new(&*self.config.mixer, self.config.index); - let selem = mixer - .find_selem(&sid) - .expect(format!("Couldn't find simple mixer control for {}", self.config.mixer).as_str()); + let selem = mixer.find_selem(&sid).expect( + format!( + "Couldn't find simple mixer control for {}", + self.config.mixer + ) + .as_str(), + ); let (min, max) = selem.get_playback_volume_range(); let range = (max - min) as f64; @@ -72,7 +76,7 @@ impl Mixer for AlsaMixer { } } - fn get_audio_filter(&self) -> Option> { + fn get_audio_filter(&self) -> Option> { None } } diff --git a/playback/src/mixer/mod.rs b/playback/src/mixer/mod.rs index f19a8661..4fc01b55 100644 --- a/playback/src/mixer/mod.rs +++ b/playback/src/mixer/mod.rs @@ -1,12 +1,12 @@ pub trait Mixer: Send { - fn open(Option) -> Self + fn open(_: Option) -> Self where Self: Sized; fn start(&self); fn stop(&self); fn set_volume(&self, volume: u16); fn volume(&self) -> u16; - fn get_audio_filter(&self) -> Option> { + fn get_audio_filter(&self) -> Option> { None } } @@ -28,10 +28,11 @@ pub struct MixerConfig { } impl Default for MixerConfig { - fn default() -> MixerConfig { MixerConfig { - card: String::from("default"), - mixer: String::from("PCM"), - index: 0, + fn default() -> MixerConfig { + MixerConfig { + card: String::from("default"), + mixer: String::from("PCM"), + index: 0, } } } @@ -39,11 +40,11 @@ impl Default for MixerConfig { pub mod softmixer; use self::softmixer::SoftMixer; -fn mk_sink(device: Option) -> Box { +fn mk_sink(device: Option) -> Box { Box::new(M::open(device)) } -pub fn find>(name: Option) -> Option) -> Box> { +pub fn find>(name: Option) -> Option) -> Box> { match name.as_ref().map(AsRef::as_ref) { None | Some("softvol") => Some(mk_sink::), #[cfg(feature = "alsa-backend")] diff --git a/playback/src/mixer/softmixer.rs b/playback/src/mixer/softmixer.rs index 4b969785..28e1cf57 100644 --- a/playback/src/mixer/softmixer.rs +++ b/playback/src/mixer/softmixer.rs @@ -23,7 +23,7 @@ impl Mixer for SoftMixer { fn set_volume(&self, volume: u16) { self.volume.store(volume as usize, Ordering::Relaxed); } - fn get_audio_filter(&self) -> Option> { + fn get_audio_filter(&self) -> Option> { Some(Box::new(SoftVolumeApplier { volume: self.volume.clone(), })) diff --git a/playback/src/player.rs b/playback/src/player.rs index a54a577f..eab8f43e 100644 --- a/playback/src/player.rs +++ b/playback/src/player.rs @@ -11,7 +11,7 @@ use std::sync::mpsc::{RecvError, RecvTimeoutError, TryRecvError}; use std::thread; use std::time::Duration; -use config::{Bitrate, PlayerConfig}; +use crate::config::{Bitrate, PlayerConfig}; use librespot_core::session::Session; use librespot_core::spotify_id::SpotifyId; @@ -36,9 +36,9 @@ struct PlayerInternal { commands: std::sync::mpsc::Receiver, state: PlayerState, - sink: Box, + sink: Box, sink_running: bool, - audio_filter: Option>, + audio_filter: Option>, event_sender: futures::sync::mpsc::UnboundedSender, } @@ -98,8 +98,10 @@ impl NormalisationData { } fn get_factor(config: &PlayerConfig, data: NormalisationData) -> f32 { - let mut normalisation_factor = - f32::powf(10.0, (data.track_gain_db + config.normalisation_pregain) / 20.0); + let mut normalisation_factor = f32::powf( + 10.0, + (data.track_gain_db + config.normalisation_pregain) / 20.0, + ); if normalisation_factor * data.track_peak > 1.0 { warn!("Reducing normalisation factor to prevent clipping. Please add negative pregain to avoid."); @@ -117,11 +119,11 @@ impl Player { pub fn new( config: PlayerConfig, session: Session, - audio_filter: Option>, + audio_filter: Option>, sink_builder: F, ) -> (Player, PlayerEventChannel) where - F: FnOnce() -> Box + Send + 'static, + F: FnOnce() -> Box + Send + 'static, { let (cmd_tx, cmd_rx) = std::sync::mpsc::channel(); let (event_sender, event_receiver) = futures::sync::mpsc::unbounded(); @@ -238,7 +240,12 @@ impl PlayerState { use self::PlayerState::*; match *self { Stopped | EndOfTrack { .. } => None, - Paused { ref mut decoder, .. } | Playing { ref mut decoder, .. } => Some(decoder), + Paused { + ref mut decoder, .. + } + | Playing { + ref mut decoder, .. + } => Some(decoder), Invalid => panic!("invalid state"), } } @@ -689,7 +696,9 @@ impl PlayerInternal { let mut decrypted_file = AudioDecrypt::new(key, encrypted_file); let normalisation_factor = match NormalisationData::parse_from_file(&mut decrypted_file) { - Ok(normalisation_data) => NormalisationData::get_factor(&self.config, normalisation_data), + Ok(normalisation_data) => { + NormalisationData::get_factor(&self.config, normalisation_data) + } Err(_) => { warn!("Unable to extract normalisation data, using default value."); 1.0 as f32 @@ -768,7 +777,7 @@ impl Seek for Subfile { x => x, }; - let newpos = try!(self.stream.seek(pos)); + let newpos = self.stream.seek(pos)?; if newpos > self.offset { Ok(newpos - self.offset) } else { diff --git a/protocol/build.rs b/protocol/build.rs index c7f19acb..3b632f65 100644 --- a/protocol/build.rs +++ b/protocol/build.rs @@ -1,14 +1,16 @@ extern crate protobuf_codegen; // Does the business extern crate protobuf_codegen_pure; // Helper function -use std::path::Path; use std::fs::{read_to_string, write}; +use std::path::Path; -use protobuf_codegen_pure::Customize; use protobuf_codegen_pure::parse_and_typecheck; +use protobuf_codegen_pure::Customize; fn main() { - let customizations = Customize { ..Default::default() }; + let customizations = Customize { + ..Default::default() + }; let lib_str = read_to_string("src/lib.rs").unwrap(); @@ -44,11 +46,7 @@ fn main() { let p = parse_and_typecheck(&["proto"], &[src]).expect("protoc"); // But generate them with the protobuf-codegen crate directly. // Then we can keep the result in-memory. - let result = protobuf_codegen::gen( - &p.file_descriptors, - &p.relative_paths, - &customizations, - ); + let result = protobuf_codegen::gen(&p.file_descriptors, &p.relative_paths, &customizations); // Protoc result as a byte array. let new = &result.first().unwrap().content; // Convert to utf8 to compare with existing. diff --git a/rustfmt.toml b/rustfmt.toml index 627f7c40..25c1fc1e 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,4 +1,3 @@ -max_width = 105 +# max_width = 105 reorder_imports = true -reorder_imports_in_group = true reorder_modules = true diff --git a/src/main.rs b/src/main.rs index db197269..52452d44 100644 --- a/src/main.rs +++ b/src/main.rs @@ -184,7 +184,13 @@ fn setup(args: &[String]) -> Setup { let matches = match opts.parse(&args[1..]) { Ok(m) => m, Err(f) => { - writeln!(stderr(), "error: {}\n{}", f.to_string(), usage(&args[0], &opts)).unwrap(); + writeln!( + stderr(), + "error: {}\n{}", + f.to_string(), + usage(&args[0], &opts) + ) + .unwrap(); exit(1); } }; @@ -218,7 +224,9 @@ fn setup(args: &[String]) -> Setup { let mixer = mixer::find(mixer_name.as_ref()).expect("Invalid mixer"); let mixer_config = MixerConfig { - card: matches.opt_str("mixer-card").unwrap_or(String::from("default")), + card: matches + .opt_str("mixer-card") + .unwrap_or(String::from("default")), mixer: matches.opt_str("mixer-name").unwrap_or(String::from("PCM")), index: matches .opt_str("mixer-index") @@ -400,7 +408,8 @@ impl Main { let config = task.connect_config.clone(); let device_id = task.session_config.device_id.clone(); - task.discovery = Some(discovery(&handle, config, device_id, setup.zeroconf_port).unwrap()); + task.discovery = + Some(discovery(&handle, config, device_id, setup.zeroconf_port).unwrap()); } if let Some(credentials) = setup.credentials { @@ -433,7 +442,9 @@ impl Future for Main { loop { let mut progress = false; - if let Some(Async::Ready(Some(creds))) = self.discovery.as_mut().map(|d| d.poll().unwrap()) { + if let Some(Async::Ready(Some(creds))) = + self.discovery.as_mut().map(|d| d.poll().unwrap()) + { if let Some(ref spirc) = self.spirc { spirc.shutdown(); } diff --git a/src/player_event_handler.rs b/src/player_event_handler.rs index 6da600b8..a4ef4daf 100644 --- a/src/player_event_handler.rs +++ b/src/player_event_handler.rs @@ -1,9 +1,9 @@ use log::info; use librespot::playback::player::PlayerEvent; -use tokio_process::{Child, CommandExt}; use std::collections::HashMap; use std::io; use std::process::Command; +use tokio_process::{Child, CommandExt}; fn run_program(program: &str, env_vars: HashMap<&str, String>) -> io::Result { let mut v: Vec<&str> = program.split_whitespace().collect(); From 38d82f2dc26d15c17f97585f68ce3575bac5c503 Mon Sep 17 00:00:00 2001 From: marcelbuesing Date: Fri, 17 Jan 2020 18:11:07 +0100 Subject: [PATCH 3/6] Rerun rustfmt on full codebase --- audio/src/fetch.rs | 56 ++++++++++++++++++++++++++----------- audio/src/range_set.rs | 17 ++++++++--- connect/src/spirc.rs | 19 ++++++++++--- core/src/proxytunnel.rs | 2 +- examples/playlist_tracks.rs | 11 ++++---- metadata/src/lib.rs | 9 ++++-- playback/src/player.rs | 18 +++++++++--- protocol/build.rs | 7 ++--- src/main.rs | 2 +- src/player_event_handler.rs | 2 +- 10 files changed, 99 insertions(+), 44 deletions(-) diff --git a/audio/src/fetch.rs b/audio/src/fetch.rs index a7f80902..ef68ed87 100644 --- a/audio/src/fetch.rs +++ b/audio/src/fetch.rs @@ -446,7 +446,7 @@ impl AudioFile { channel_tx: None, stream_shared: None, file_size: file.metadata().unwrap().len() as usize, - } + }; } } } @@ -514,7 +514,10 @@ impl AudioFileFetchDataReceiver { request_length: usize, request_sent_time: Instant, ) -> AudioFileFetchDataReceiver { - let measure_ping_time = shared.number_of_open_requests.load(atomic::Ordering::SeqCst) == 0; + let measure_ping_time = shared + .number_of_open_requests + .load(atomic::Ordering::SeqCst) + == 0; shared .number_of_open_requests @@ -562,7 +565,8 @@ impl Future for AudioFileFetchDataReceiver { if let Some(request_sent_time) = self.request_sent_time { let duration = Instant::now() - request_sent_time; let duration_ms: u64; - if 0.001 * (duration.as_millis() as f64) > MAXIMUM_ASSUMED_PING_TIME_SECONDS + if 0.001 * (duration.as_millis() as f64) + > MAXIMUM_ASSUMED_PING_TIME_SECONDS { duration_ms = (MAXIMUM_ASSUMED_PING_TIME_SECONDS * 1000.0) as u64; } else { @@ -714,8 +718,13 @@ impl AudioFileFetch { ranges_to_request.subtract_range_set(&download_status.requested); for range in ranges_to_request.iter() { - let (_headers, data) = - request_range(&self.session, self.shared.file_id, range.start, range.length).split(); + let (_headers, data) = request_range( + &self.session, + self.shared.file_id, + range.start, + range.length, + ) + .split(); download_status.requested.add_range(range); @@ -749,7 +758,10 @@ impl AudioFileFetch { // download data from after the current read position first let mut tail_end = RangeSet::new(); let read_position = self.shared.read_position.load(atomic::Ordering::Relaxed); - tail_end.add_range(&Range::new(read_position, self.shared.file_size - read_position)); + tail_end.add_range(&Range::new( + read_position, + self.shared.file_size - read_position, + )); let tail_end = tail_end.intersection(&missing_data); if !tail_end.is_empty() { @@ -794,8 +806,9 @@ impl AudioFileFetch { let ping_time_ms: usize = match self.network_response_times_ms.len() { 1 => self.network_response_times_ms[0] as usize, 2 => { - ((self.network_response_times_ms[0] + self.network_response_times_ms[1]) / 2) - as usize + ((self.network_response_times_ms[0] + + self.network_response_times_ms[1]) + / 2) as usize } 3 => { let mut times = self.network_response_times_ms.clone(); @@ -863,10 +876,12 @@ impl AudioFileFetch { self.download_range(request.start, request.length); } Ok(Async::Ready(Some(StreamLoaderCommand::RandomAccessMode()))) => { - *(self.shared.download_strategy.lock().unwrap()) = DownloadStrategy::RandomAccess(); + *(self.shared.download_strategy.lock().unwrap()) = + DownloadStrategy::RandomAccess(); } Ok(Async::Ready(Some(StreamLoaderCommand::StreamMode()))) => { - *(self.shared.download_strategy.lock().unwrap()) = DownloadStrategy::Streaming(); + *(self.shared.download_strategy.lock().unwrap()) = + DownloadStrategy::Streaming(); } Ok(Async::Ready(Some(StreamLoaderCommand::Close()))) => { return Ok(Async::Ready(())); @@ -908,15 +923,20 @@ impl Future for AudioFileFetch { } if let DownloadStrategy::Streaming() = self.get_download_strategy() { - let number_of_open_requests = - self.shared.number_of_open_requests.load(atomic::Ordering::SeqCst); + let number_of_open_requests = self + .shared + .number_of_open_requests + .load(atomic::Ordering::SeqCst); let max_requests_to_send = MAX_PREFETCH_REQUESTS - min(MAX_PREFETCH_REQUESTS, number_of_open_requests); if max_requests_to_send > 0 { let bytes_pending: usize = { let download_status = self.shared.download_status.lock().unwrap(); - download_status.requested.minus(&download_status.downloaded).len() + download_status + .requested + .minus(&download_status.downloaded) + .len() }; let ping_time_seconds = @@ -924,9 +944,11 @@ impl Future for AudioFileFetch { let download_rate = self.session.channel().get_download_rate_estimate(); let desired_pending_bytes = max( - (PREFETCH_THRESHOLD_FACTOR * ping_time_seconds * self.shared.stream_data_rate as f64) + (PREFETCH_THRESHOLD_FACTOR + * ping_time_seconds + * self.shared.stream_data_rate as f64) as usize, + (FAST_PREFETCH_THRESHOLD_FACTOR * ping_time_seconds * download_rate as f64) as usize, - (FAST_PREFETCH_THRESHOLD_FACTOR * ping_time_seconds * download_rate as f64) as usize, ); if bytes_pending < desired_pending_bytes { @@ -1003,7 +1025,9 @@ impl Read for AudioFileStreaming { .unwrap() .0; } - let available_length = download_status.downloaded.contained_length_from_value(offset); + let available_length = download_status + .downloaded + .contained_length_from_value(offset); assert!(available_length > 0); drop(download_status); diff --git a/audio/src/range_set.rs b/audio/src/range_set.rs index 448c0971..44955388 100644 --- a/audio/src/range_set.rs +++ b/audio/src/range_set.rs @@ -113,7 +113,8 @@ impl RangeSet { // the new range starts after anything we already passed and ends before the next range starts (they don't touch) -> insert it. self.ranges.insert(index, range.clone()); return; - } else if range.start <= self.ranges[index].end() && self.ranges[index].start <= range.end() + } else if range.start <= self.ranges[index].end() + && self.ranges[index].start <= range.end() { // the new range overlaps (or touches) the first range. They are to be merged. // In addition we might have to merge further ranges in as well. @@ -161,7 +162,9 @@ impl RangeSet { if range.end() <= self.ranges[index].start { // the remaining ranges are past the one to subtract. -> we're done. return; - } else if range.start <= self.ranges[index].start && self.ranges[index].start < range.end() { + } else if range.start <= self.ranges[index].start + && self.ranges[index].start < range.end() + { // the range to subtract started before the current range and reaches into the current range // -> we have to remove the beginning of the range or the entire range and do the same for following ranges. @@ -223,8 +226,14 @@ impl RangeSet { other_index += 1; } else { // the two intervals overlap. Add the union and advance the index of the one that ends first. - let new_start = max(self.ranges[self_index].start, other.ranges[other_index].start); - let new_end = min(self.ranges[self_index].end(), other.ranges[other_index].end()); + let new_start = max( + self.ranges[self_index].start, + other.ranges[other_index].start, + ); + let new_end = min( + self.ranges[self_index].end(), + other.ranges[other_index].end(), + ); assert!(new_start <= new_end); result.add_range(&Range::new(new_start, new_end - new_start)); if self.ranges[self_index].end() <= other.ranges[other_index].end() { diff --git a/connect/src/spirc.rs b/connect/src/spirc.rs index 29e6b1d8..ea24f6b8 100644 --- a/connect/src/spirc.rs +++ b/connect/src/spirc.rs @@ -796,12 +796,19 @@ impl SpircTask { self.resolve_uri(&radio_uri) } - fn resolve_autoplay_uri(&self, uri: &str) -> Box> { + fn resolve_autoplay_uri( + &self, + uri: &str, + ) -> Box> { let query_uri = format!("hm://autoplay-enabled/query?uri={}", uri); let request = self.session.mercury().get(query_uri); Box::new(request.and_then(move |response| { if response.status_code == 200 { - let data = response.payload.first().expect("Empty autoplay uri").to_vec(); + let data = response + .payload + .first() + .expect("Empty autoplay uri") + .to_vec(); let autoplay_uri = String::from_utf8(data).unwrap(); Ok(autoplay_uri) } else { @@ -811,7 +818,10 @@ impl SpircTask { })) } - fn resolve_uri(&self, uri: &str) -> Box> { + fn resolve_uri( + &self, + uri: &str, + ) -> Box> { let request = self.session.mercury().get(uri); Box::new(request.and_then(move |response| { @@ -900,7 +910,8 @@ impl SpircTask { let track = { let mut track_ref = self.state.get_track()[index as usize].clone(); let mut track_id = self.get_spotify_id_for_track(&track_ref); - while track_id.is_err() || track_id.unwrap().audio_type == SpotifyAudioType::NonPlayable { + while track_id.is_err() || track_id.unwrap().audio_type == SpotifyAudioType::NonPlayable + { warn!( "Skipping track <{:?}> at position [{}] of {}", track_ref.get_uri(), diff --git a/core/src/proxytunnel.rs b/core/src/proxytunnel.rs index 2b9df5a8..ea84bc88 100644 --- a/core/src/proxytunnel.rs +++ b/core/src/proxytunnel.rs @@ -58,7 +58,7 @@ impl Future for ProxyTunnel { let status = match response.parse(&buf) { Ok(status) => status, Err(err) => { - return Err(io::Error::new(io::ErrorKind::Other, err.description())) + return Err(io::Error::new(io::ErrorKind::Other, err.description())); } }; diff --git a/examples/playlist_tracks.rs b/examples/playlist_tracks.rs index 3e4202d5..fc288d18 100644 --- a/examples/playlist_tracks.rs +++ b/examples/playlist_tracks.rs @@ -1,4 +1,3 @@ - use env_logger; use std::env; use tokio_core::reactor::Core; @@ -7,7 +6,7 @@ use librespot::core::authentication::Credentials; use librespot::core::config::SessionConfig; use librespot::core::session::Session; use librespot::core::spotify_id::SpotifyId; -use librespot::metadata::{Metadata, Track, Playlist}; +use librespot::metadata::{Metadata, Playlist, Track}; fn main() { env_logger::init(); @@ -26,16 +25,16 @@ fn main() { let uri_split = args[3].split(":"); let uri_parts: Vec<&str> = uri_split.collect(); - println!("{}, {}, {}",uri_parts[0], uri_parts[1], uri_parts[2]); - + println!("{}, {}, {}", uri_parts[0], uri_parts[1], uri_parts[2]); + let plist_uri = SpotifyId::from_base62(uri_parts[2]).unwrap(); - + let session = core .run(Session::connect(session_config, credentials, None, handle)) .unwrap(); let plist = core.run(Playlist::get(&session, plist_uri)).unwrap(); - println!("{:?}",plist); + println!("{:?}", plist); for track_id in plist.tracks { let plist_track = core.run(Track::get(&session, track_id)).unwrap(); println!("track: {} ", plist_track.name); diff --git a/metadata/src/lib.rs b/metadata/src/lib.rs index 6ba32f4d..d4bd797c 100644 --- a/metadata/src/lib.rs +++ b/metadata/src/lib.rs @@ -301,7 +301,6 @@ impl Metadata for Playlist { } fn parse(msg: &Self::Message, _: &Session) -> Self { - let tracks = msg .get_contents() .get_items() @@ -312,9 +311,13 @@ impl Metadata for Playlist { SpotifyId::from_base62(uri_parts[2]).unwrap() }) .collect::>(); - + if tracks.len() != msg.get_length() as usize { - warn!("Got {} tracks, but the playlist should contain {} tracks.", tracks.len(), msg.get_length()); + warn!( + "Got {} tracks, but the playlist should contain {} tracks.", + tracks.len(), + msg.get_length() + ); } Playlist { diff --git a/playback/src/player.rs b/playback/src/player.rs index eab8f43e..ba61471f 100644 --- a/playback/src/player.rs +++ b/playback/src/player.rs @@ -525,7 +525,10 @@ impl PlayerInternal { if let Some(stream_loader_controller) = self.state.stream_loader_controller() { stream_loader_controller.set_stream_mode(); } - if let PlayerState::Playing { bytes_per_second, .. } = self.state { + if let PlayerState::Playing { + bytes_per_second, .. + } = self.state + { if let Some(stream_loader_controller) = self.state.stream_loader_controller() { // Request our read ahead range let request_data_length = max( @@ -599,7 +602,10 @@ impl PlayerInternal { .iter() .map(|alt_id| AudioItem::get_audio_item(&self.session, *alt_id)); let alternatives = future::join_all(alternatives).wait().unwrap(); - alternatives.into_iter().find(|alt| alt.available).map(Cow::Owned) + alternatives + .into_iter() + .find(|alt| alt.available) + .map(Cow::Owned) } else { None } @@ -677,8 +683,12 @@ impl PlayerInternal { let play_from_beginning = position == 0; let key = self.session.audio_key().request(spotify_id, file_id); - let encrypted_file = - AudioFile::open(&self.session, file_id, bytes_per_second, play_from_beginning); + let encrypted_file = AudioFile::open( + &self.session, + file_id, + bytes_per_second, + play_from_beginning, + ); let encrypted_file = encrypted_file.wait().unwrap(); diff --git a/protocol/build.rs b/protocol/build.rs index 3b632f65..3ea25e75 100644 --- a/protocol/build.rs +++ b/protocol/build.rs @@ -23,10 +23,9 @@ fn main() { let name; if line.starts_with("pub mod ") { - name = &line[8..len-1]; // Remove keywords and semi-colon - } - else { - name = &line[4..len-1]; // Remove keywords and semi-colon + name = &line[8..len - 1]; // Remove keywords and semi-colon + } else { + name = &line[4..len - 1]; // Remove keywords and semi-colon } // Build the paths to relevant files. diff --git a/src/main.rs b/src/main.rs index 52452d44..f94a2ef8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use futures::sync::mpsc::UnboundedReceiver; use futures::{Async, Future, Poll, Stream}; -use sha1::{Digest, Sha1}; use log::{error, info, trace, warn}; +use sha1::{Digest, Sha1}; use std::env; use std::io::{self, stderr, Write}; use std::mem; diff --git a/src/player_event_handler.rs b/src/player_event_handler.rs index a4ef4daf..03bae147 100644 --- a/src/player_event_handler.rs +++ b/src/player_event_handler.rs @@ -1,5 +1,5 @@ -use log::info; use librespot::playback::player::PlayerEvent; +use log::info; use std::collections::HashMap; use std::io; use std::process::Command; From c9147d31ab47d8daf989d2450465d7f8ee4ef965 Mon Sep 17 00:00:00 2001 From: marcelbuesing Date: Fri, 17 Jan 2020 19:09:10 +0100 Subject: [PATCH 4/6] Turn child crates into rust 2018 edition --- audio/Cargo.toml | 1 + audio/src/fetch.rs | 2 +- connect/Cargo.toml | 1 + core/Cargo.toml | 1 + metadata/Cargo.toml | 1 + playback/Cargo.toml | 1 + playback/src/player.rs | 12 ++++++------ protocol/Cargo.toml | 1 + 8 files changed, 13 insertions(+), 7 deletions(-) diff --git a/audio/Cargo.toml b/audio/Cargo.toml index 6c485e25..a0f95310 100644 --- a/audio/Cargo.toml +++ b/audio/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Paul Lietar "] description="The audio fetching and processing logic for librespot" license="MIT" +edition = "2018" [dependencies.librespot-core] path = "../core" diff --git a/audio/src/fetch.rs b/audio/src/fetch.rs index ef68ed87..1ab6ce78 100644 --- a/audio/src/fetch.rs +++ b/audio/src/fetch.rs @@ -1,9 +1,9 @@ +use crate::range_set::{Range, RangeSet}; use byteorder::{BigEndian, ByteOrder, WriteBytesExt}; use bytes::Bytes; use futures::sync::{mpsc, oneshot}; use futures::Stream; use futures::{Async, Future, Poll}; -use range_set::{Range, RangeSet}; use std::cmp::{max, min}; use std::fs; use std::io::{self, Read, Seek, SeekFrom, Write}; diff --git a/connect/Cargo.toml b/connect/Cargo.toml index 270f605e..c468d2d3 100644 --- a/connect/Cargo.toml +++ b/connect/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Paul Lietar "] description="The discovery and Spotify Connect logic for librespot" license="MIT" +edition = "2018" [dependencies.librespot-core] path = "../core" diff --git a/core/Cargo.toml b/core/Cargo.toml index 908b9073..9e9ccce4 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Paul Lietar "] build = "build.rs" description="The core functionality provided by librespot" license="MIT" +edition = "2018" [dependencies.librespot-protocol] path = "../protocol" diff --git a/metadata/Cargo.toml b/metadata/Cargo.toml index 75028195..02c0f2b5 100644 --- a/metadata/Cargo.toml +++ b/metadata/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Paul Lietar "] description="The metadata logic for librespot" license="MIT" +edition = "2018" [dependencies] byteorder = "1.3" diff --git a/playback/Cargo.toml b/playback/Cargo.toml index d7d1c5b5..f414d5ec 100644 --- a/playback/Cargo.toml +++ b/playback/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Sasha Hilton "] description="The audio playback logic for librespot" license="MIT" +edition = "2018" [dependencies.librespot-audio] path = "../audio" diff --git a/playback/src/player.rs b/playback/src/player.rs index ba61471f..4d1ae4fb 100644 --- a/playback/src/player.rs +++ b/playback/src/player.rs @@ -15,15 +15,15 @@ use crate::config::{Bitrate, PlayerConfig}; use librespot_core::session::Session; use librespot_core::spotify_id::SpotifyId; -use audio::{AudioDecrypt, AudioFile, StreamLoaderController}; -use audio::{VorbisDecoder, VorbisPacket}; -use audio::{ +use crate::audio::{AudioDecrypt, AudioFile, StreamLoaderController}; +use crate::audio::{VorbisDecoder, VorbisPacket}; +use crate::audio::{ READ_AHEAD_BEFORE_PLAYBACK_ROUNDTRIPS, READ_AHEAD_BEFORE_PLAYBACK_SECONDS, READ_AHEAD_DURING_PLAYBACK_ROUNDTRIPS, READ_AHEAD_DURING_PLAYBACK_SECONDS, }; -use audio_backend::Sink; -use metadata::{AudioItem, FileFormat}; -use mixer::AudioFilter; +use crate::audio_backend::Sink; +use crate::metadata::{AudioItem, FileFormat}; +use crate::mixer::AudioFilter; pub struct Player { commands: Option>, diff --git a/protocol/Cargo.toml b/protocol/Cargo.toml index 4c9a332b..792f5b5b 100644 --- a/protocol/Cargo.toml +++ b/protocol/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Paul LiĆ©tar "] build = "build.rs" description="The protobuf logic for communicating with Spotify servers" license="MIT" +edition = "2018" [dependencies] protobuf = "2.8.1" From d599c5cc8514a8845cb2656d9567485b09cffb24 Mon Sep 17 00:00:00 2001 From: Sasha Hilton Date: Fri, 24 Jan 2020 00:27:57 +0100 Subject: [PATCH 5/6] Further reduce cache bloat --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 182adfdb..ee2bd7f2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,8 @@ before_cache: - rm -rfv target/debug/deps/liblibrespot-* - rm -rfv target/debug/deps/librespot-* - rm -rfv target/debug/{librespot,liblibrespot}.d - - cargo clean -p librespot librespot-core librespot-connect librespot-audio librespot-metadata librespot-playback + - rm -rfv target/debug/incremental/{build_script_build,librespot,librespot_core,librespot_connect,librespot_audio,librespot_metadata,librespot_playback,librespot_player,librespot_protocol}-* + - cargo clean -p librespot librespot-core librespot-connect librespot-audio librespot-metadata librespot-playback addons: apt: From 94bb4623d209aaf92221f5a6ef0497a93db1cbc5 Mon Sep 17 00:00:00 2001 From: Sasha Hilton Date: Fri, 24 Jan 2020 00:59:28 +0100 Subject: [PATCH 6/6] Correct cargo clean package flags --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ee2bd7f2..ffd2eb75 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ before_cache: - rm -rfv target/debug/deps/librespot-* - rm -rfv target/debug/{librespot,liblibrespot}.d - rm -rfv target/debug/incremental/{build_script_build,librespot,librespot_core,librespot_connect,librespot_audio,librespot_metadata,librespot_playback,librespot_player,librespot_protocol}-* - - cargo clean -p librespot librespot-core librespot-connect librespot-audio librespot-metadata librespot-playback + - cargo clean -p librespot -p librespot-core -p librespot-connect -p librespot-audio -p librespot-metadata -p librespot-playback addons: apt: