Small refactor of librespot-core

* Remove default impl for `SessionConfig`
* Move util mod to single file
* Restore privacy of mods
* Move `fn get_credentials` to application
* Remove `extern crate` statements
This commit is contained in:
johannesd3 2021-02-10 22:40:33 +01:00 committed by Johannesd3
parent c0942f14e8
commit 9253be7bc9
8 changed files with 31 additions and 85 deletions

10
Cargo.lock generated
View file

@ -1475,7 +1475,6 @@ dependencies = [
"tokio-util",
"tower-service",
"url 1.7.2",
"uuid",
"vergen",
]
@ -2990,15 +2989,6 @@ dependencies = [
"percent-encoding 2.1.0",
]
[[package]]
name = "uuid"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
dependencies = [
"getrandom 0.2.2",
]
[[package]]
name = "vergen"
version = "3.2.0"

View file

@ -40,7 +40,6 @@ tokio = { version = "1.0", features = ["io-util", "rt-multi-thread"] }
tokio-util = { version = "0.6", features = ["codec"] }
tower-service = "0.3"
url = "1.7"
uuid = { version = "0.8", features = ["v4"] }
[build-dependencies]
rand = "0.7"

View file

@ -1,6 +1,3 @@
extern crate rand;
extern crate vergen;
use rand::distributions::Alphanumeric;
use rand::Rng;
use vergen::{generate_cargo_keys, ConstantsFlags};

View file

@ -142,30 +142,6 @@ where
base64::decode(&v).map_err(|e| serde::de::Error::custom(e.to_string()))
}
pub fn get_credentials<F: FnOnce(&String) -> String>(
username: Option<String>,
password: Option<String>,
cached_credentials: Option<Credentials>,
prompt: F,
) -> Option<Credentials> {
match (username, password, cached_credentials) {
(Some(username), Some(password), _) => Some(Credentials::with_password(username, password)),
(Some(ref username), _, Some(ref credentials)) if *username == credentials.username => {
Some(credentials.clone())
}
(Some(username), None, _) => Some(Credentials::with_password(
username.clone(),
prompt(&username),
)),
(None, _, Some(credentials)) => Some(credentials),
(None, _, None) => None,
}
}
error_chain! {
types {
AuthenticationError, AuthenticationErrorKind, AuthenticationResultExt, AuthenticationResult;

View file

@ -1,9 +1,6 @@
use std::fmt;
use std::str::FromStr;
use url::Url;
use uuid::Uuid;
use crate::version;
#[derive(Clone, Debug)]
pub struct SessionConfig {
@ -13,18 +10,6 @@ pub struct SessionConfig {
pub ap_port: Option<u16>,
}
impl Default for SessionConfig {
fn default() -> SessionConfig {
let device_id = Uuid::new_v4().to_hyphenated().to_string();
SessionConfig {
user_agent: version::version_string(),
device_id: device_id,
proxy: None,
ap_port: None,
}
}
}
#[derive(Clone, Copy, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub enum DeviceType {
Unknown = 0,

View file

@ -8,43 +8,19 @@ extern crate serde_derive;
extern crate pin_project_lite;
#[macro_use]
extern crate error_chain;
extern crate aes;
extern crate base64;
extern crate byteorder;
extern crate bytes;
extern crate futures;
extern crate hmac;
extern crate httparse;
extern crate hyper;
extern crate num_bigint;
extern crate num_integer;
extern crate num_traits;
extern crate once_cell;
extern crate pbkdf2;
extern crate protobuf;
extern crate rand;
extern crate serde;
extern crate serde_json;
extern crate sha1;
extern crate shannon;
pub extern crate tokio;
extern crate tokio_util;
extern crate tower_service;
extern crate url;
extern crate uuid;
extern crate librespot_protocol as protocol;
use librespot_protocol as protocol;
#[macro_use]
mod component;
pub mod apresolve;
mod apresolve;
pub mod audio_key;
pub mod authentication;
pub mod cache;
pub mod channel;
pub mod config;
pub mod connection;
mod connection;
pub mod diffie_hellman;
pub mod keymaster;
pub mod mercury;

View file

@ -12,7 +12,7 @@ use std::{
};
use url::Url;
use librespot::core::authentication::{get_credentials, Credentials};
use librespot::core::authentication::Credentials;
use librespot::core::cache::Cache;
use librespot::core::config::{ConnectConfig, DeviceType, SessionConfig, VolumeCtrl};
use librespot::core::session::Session;
@ -70,6 +70,29 @@ fn list_backends() {
}
}
pub fn get_credentials<F: FnOnce(&String) -> Option<String>>(
username: Option<String>,
password: Option<String>,
cached_credentials: Option<Credentials>,
prompt: F,
) -> Option<Credentials> {
if let Some(username) = username {
if let Some(password) = password {
return Some(Credentials::with_password(username, password));
}
match cached_credentials {
Some(credentials) if username == credentials.username => Some(credentials),
_ => {
let password = prompt(&username)?;
Some(Credentials::with_password(username, password))
}
}
} else {
cached_credentials
}
}
#[derive(Clone)]
struct Setup {
backend: fn(Option<String>) -> Box<dyn Sink + Send + 'static>,
@ -317,10 +340,10 @@ fn setup(args: &[String]) -> Setup {
let credentials = {
let cached_credentials = cache.as_ref().and_then(Cache::credentials);
let password = |username: &String| -> String {
write!(stderr(), "Password for {}: ", username).unwrap();
stderr().flush().unwrap();
rpassword::read_password().unwrap()
let password = |username: &String| -> Option<String> {
write!(stderr(), "Password for {}: ", username).ok()?;
stderr().flush().ok()?;
rpassword::read_password().ok()
};
get_credentials(