diff --git a/src/main.rs b/src/main.rs index 6eda338c..69fbace4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,11 +32,11 @@ fn main() { main_helper::setup_logging(&matches); - let session = main_helper::session_from_matches(&matches); - let credentials = main_helper::credentials_from_matches(&session, &matches); + let session = main_helper::create_session(&matches); + let credentials = main_helper::get_credentials(&session, &matches); session.login(credentials).unwrap(); - let player = main_helper::player_from_matches(&session, &matches); + let player = main_helper::create_player(&session, &matches); let spirc = SpircManager::new(session.clone(), player); let spirc_signal = spirc.clone(); diff --git a/src/main_helper.rs b/src/main_helper.rs index 9cdc959e..67e8b2a2 100644 --- a/src/main_helper.rs +++ b/src/main_helper.rs @@ -55,26 +55,14 @@ pub fn add_player_arguments(opts: &mut getopts::Options) { .optopt("", "device", "Audio device to use. Use '?' to list options", "DEVICE"); } -pub fn session_from_matches(matches: &getopts::Matches) -> Session { - create_session(matches.opt_str("n").unwrap(), - matches.opt_str("b").as_ref().map(String::as_ref), - matches.opt_str("c").as_ref().map(String::as_ref), - matches.opt_str("onstart"), - matches.opt_str("onstop")) -} - -pub fn create_session(name: String, - bitrate: Option<&str>, - cache: Option<&str>, - onstart: Option, - onstop: Option) - -> Session { +pub fn create_session(matches: &getopts::Matches) -> Session { info!("librespot {} ({}). Built on {}.", version::short_sha(), version::commit_date(), version::short_now()); - let bitrate = match bitrate { + let name = matches.opt_str("n").unwrap(); + let bitrate = match matches.opt_str("b").as_ref().map(String::as_ref) { None => Bitrate::Bitrate160, // default value Some("96") => Bitrate::Bitrate96, @@ -86,10 +74,13 @@ pub fn create_session(name: String, } }; - let cache = cache.map(|cache_location| { + let cache = matches.opt_str("c").map(|cache_location| { Box::new(DefaultCache::new(PathBuf::from(cache_location)).unwrap()) as Box }).unwrap_or_else(|| Box::new(NoCache) as Box); + let onstart = matches.opt_str("onstart"); + let onstop = matches.opt_str("onstop"); + let config = Config { user_agent: version::version_string(), device_name: name, @@ -101,16 +92,13 @@ pub fn create_session(name: String, Session::new(config, cache) } -pub fn credentials_from_matches(session: &Session, - matches: &getopts::Matches) -> Credentials { - get_credentials(session, matches.opt_str("username"), matches.opt_str("password")) -} - -pub fn get_credentials(session: &Session, username: Option, - password: Option) -> Credentials { +pub fn get_credentials(session: &Session, matches: &getopts::Matches) -> Credentials { let credentials = session.cache().get_credentials(); - match (username, password, credentials) { + match (matches.opt_str("username"), + matches.opt_str("password"), + credentials) { + (Some(username), Some(password), _) => Credentials::with_password(username, password), @@ -134,14 +122,11 @@ pub fn get_credentials(session: &Session, username: Option, } } -pub fn player_from_matches(session: &Session, matches: &getopts::Matches) -> Player { - create_player(session, matches.opt_str("backend").as_ref().map(String::as_ref) - , matches.opt_str("device")) -} +pub fn create_player(session: &Session, matches: &getopts::Matches) -> Player { + let backend_name = matches.opt_str("backend"); + let device_name = matches.opt_str("device"); -pub fn create_player(session: &Session, backend_name: Option<&str> - , device_name: Option) -> Player { - let make_backend = find_backend(backend_name); + let make_backend = find_backend(backend_name.as_ref().map(AsRef::as_ref)); Player::new(session.clone(), move || { make_backend(device_name.as_ref().map(AsRef::as_ref))