From 73d01446962be66769c01fb143c248bce08e738a Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Thu, 11 Jan 2024 23:29:03 +0100 Subject: [PATCH] Tidy up main --- src/main.rs | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index ec90759..28793b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,6 @@ use std::fs::OpenOptions; use std::path::PathBuf; use std::{ffi::OsString, io}; -use musichoard::database::NoDatabase; -use musichoard::library::NoLibrary; -use musichoard::Collection; use ratatui::{backend::CrosstermBackend, Terminal}; use structopt::StructOpt; @@ -66,31 +63,11 @@ fn with(lib: Option>, db: Option>) { Tui::run(terminal, ui, handler, listener).expect("failed to run tui"); } -fn with_database(opt: Opt, db: Option>) { - if opt.no_library { - with(None, db); - } else if let Some(uri) = opt.beets_ssh_uri { - let uri = uri.into_string().expect("invalid SSH URI"); - let beets_config_file_path = opt - .beets_config_file_path - .map(|s| s.into_string()) - .transpose() - .expect("failed to extract beets config file path"); - let lib_exec = BeetsLibrarySshExecutor::new(uri) - .expect("failed to initialise beets") - .config(beets_config_file_path); - with(Some(Box::new(BeetsLibrary::new(lib_exec))), db); - } else { - let lib_exec = BeetsLibraryProcessExecutor::default().config(opt.beets_config_file_path); - with(Some(Box::new(BeetsLibrary::new(lib_exec))), db); - } -} - fn main() { let opt = Opt::from_args(); - if opt.no_database { - with_database(opt, None); + let database: Option> = if opt.no_database { + None } else { // Create an empty database file if it does not exist. match OpenOptions::new() @@ -111,8 +88,28 @@ fn main() { } let db_exec = JsonDatabaseFileBackend::new(&opt.database_file_path); - with_database(opt, Some(Box::new(JsonDatabase::new(db_exec)))); + Some(Box::new(JsonDatabase::new(db_exec))) }; + + let library: Option> = if opt.no_library { + None + } else if let Some(uri) = opt.beets_ssh_uri { + let uri = uri.into_string().expect("invalid SSH URI"); + let beets_config_file_path = opt + .beets_config_file_path + .map(|s| s.into_string()) + .transpose() + .expect("failed to extract beets config file path"); + let lib_exec = BeetsLibrarySshExecutor::new(uri) + .expect("failed to initialise beets") + .config(beets_config_file_path); + Some(Box::new(BeetsLibrary::new(lib_exec))) + } else { + let lib_exec = BeetsLibraryProcessExecutor::default().config(opt.beets_config_file_path); + Some(Box::new(BeetsLibrary::new(lib_exec))) + }; + + with(library, database); } #[cfg(test)]