Эх сурвалжийг харах

Merge pull request #143 from SimonPersson/general_main_helper

Improve for library users
Paul Lietar 7 жил өмнө
parent
commit
ae7c177823

+ 32 - 1
src/authentication/mod.rs

@@ -6,13 +6,16 @@ use crypto::hmac::Hmac;
 use crypto::pbkdf2::pbkdf2;
 use crypto::sha1::Sha1;
 use protobuf::ProtobufEnum;
+use rpassword;
 use serde;
 use serde_json;
-use std::io::{self, Read, Write};
+use std::io::{self, stderr, Read, Write};
 use std::fs::File;
 use std::path::Path;
 use rustc_serialize::base64::{self, FromBase64, ToBase64};
 
+use session::Session;
+
 use protocol::authentication::AuthenticationType;
 
 #[derive(Debug, Clone)]
@@ -170,3 +173,31 @@ fn deserialize_base64<D>(de: &mut D) -> Result<Vec<u8>, D::Error>
 
 mod discovery;
 pub use self::discovery::discovery_login;
+
+pub fn get_credentials(session: &Session, username: Option<String>, password: Option<String>) -> Credentials {
+    let credentials = session.cache().get_credentials();
+
+    match (username, password, credentials) {
+
+        (Some(username), Some(password), _)
+            => Credentials::with_password(username, password),
+
+        (Some(ref username), _, Some(ref credentials)) if *username == credentials.username
+            => credentials.clone(),
+
+        (Some(username), None, _) => {
+            write!(stderr(), "Password for {}: ", username).unwrap();
+            stderr().flush().unwrap();
+            let password = rpassword::read_password().unwrap();
+            Credentials::with_password(username.clone(), password)
+        }
+
+        (None, _, Some(credentials))
+            => credentials,
+
+        (None, _, None) => {
+            info!("No username provided and no stored credentials, starting discovery ...");
+            discovery_login(&session.config().device_name, session.device_id()).unwrap()
+        }
+    }
+}

+ 3 - 1
src/main.rs

@@ -8,6 +8,7 @@ use std::thread;
 
 use librespot::spirc::SpircManager;
 use librespot::main_helper;
+use librespot::authentication::get_credentials;
 
 fn usage(program: &str, opts: &getopts::Options) -> String {
     let brief = format!("Usage: {} [options]", program);
@@ -33,7 +34,8 @@ fn main() {
     main_helper::setup_logging(&matches);
 
     let session = main_helper::create_session(&matches);
-    let credentials = main_helper::get_credentials(&session, &matches);
+    let credentials = get_credentials(&session, matches.opt_str("username"),
+                                      matches.opt_str("password"));
     session.login(credentials).unwrap();
 
     let player = main_helper::create_player(&session, &matches);

+ 0 - 33
src/main_helper.rs

@@ -1,13 +1,10 @@
 use env_logger::LogBuilder;
 use getopts;
-use rpassword;
 use std::env;
-use std::io::{stderr, Write};
 use std::path::PathBuf;
 use std::process::exit;
 
 use audio_backend::{BACKENDS, Sink};
-use authentication::{Credentials, discovery_login};
 use cache::{Cache, DefaultCache, NoCache};
 use player::Player;
 use session::{Bitrate, Config, Session};
@@ -92,36 +89,6 @@ pub fn create_session(matches: &getopts::Matches) -> Session {
     Session::new(config, cache)
 }
 
-pub fn get_credentials(session: &Session, matches: &getopts::Matches) -> Credentials {
-    let credentials = session.cache().get_credentials();
-
-    match (matches.opt_str("username"),
-           matches.opt_str("password"),
-           credentials) {
-
-        (Some(username), Some(password), _)
-            => Credentials::with_password(username, password),
-
-        (Some(ref username), _, Some(ref credentials)) if *username == credentials.username
-            => credentials.clone(),
-
-        (Some(username), None, _) => {
-            write!(stderr(), "Password for {}: ", username).unwrap();
-            stderr().flush().unwrap();
-            let password = rpassword::read_password().unwrap();
-            Credentials::with_password(username.clone(), password)
-        }
-
-        (None, _, Some(credentials))
-            => credentials,
-
-        (None, _, None) => {
-            info!("No username provided and no stored credentials, starting discovery ...");
-            discovery_login(&session.config().device_name, session.device_id()).unwrap()
-        }
-    }
-}
-
 pub fn create_player(session: &Session, matches: &getopts::Matches) -> Player {
     let backend_name = matches.opt_str("backend");
     let device_name = matches.opt_str("device");

+ 1 - 0
src/session.rs

@@ -28,6 +28,7 @@ use version;
 
 use stream;
 
+#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq)]
 pub enum Bitrate {
     Bitrate96,
     Bitrate160,