Skip to content

Commit

Permalink
Small refactor of librespot-core
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Johannesd3 committed Feb 23, 2021
1 parent c0942f1 commit 9253be7
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 85 deletions.
10 changes: 0 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 0 additions & 3 deletions core/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
extern crate rand;
extern crate vergen;

use rand::distributions::Alphanumeric;
use rand::Rng;
use vergen::{generate_cargo_keys, ConstantsFlags};
Expand Down
24 changes: 0 additions & 24 deletions core/src/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 0 additions & 15 deletions core/src/config.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand Down
30 changes: 3 additions & 27 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
File renamed without changes.
33 changes: 28 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 9253be7

Please sign in to comment.