Tidier main
All checks were successful
Cargo CI / Build and Test (pull_request) Successful in 1m0s
Cargo CI / Lint (pull_request) Successful in 45s

This commit is contained in:
Wojciech Kozlowski 2024-01-11 22:50:04 +01:00
parent 45844e448f
commit a192e2631e

View File

@ -2,6 +2,7 @@ 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};
@ -65,11 +66,31 @@ fn with<LIB: ILibrary, DB: IDatabase>(lib: Option<LIB>, db: Option<DB>) {
Tui::run(terminal, ui, handler, listener).expect("failed to run tui");
}
fn with_database<DB: IDatabase>(opt: Opt, db: Option<DB>) {
if opt.no_library {
with(None::<NoLibrary>, 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(BeetsLibrary::new(lib_exec)), db);
} else {
let lib_exec = BeetsLibraryProcessExecutor::default().config(opt.beets_config_file_path);
with(Some(BeetsLibrary::new(lib_exec)), db);
}
}
fn main() {
let opt = Opt::from_args();
let db = if opt.no_database {
None
if opt.no_database {
with_database(opt, None::<NoDatabase>);
} else {
// Create an empty database file if it does not exist.
match OpenOptions::new()
@ -90,26 +111,8 @@ fn main() {
}
let db_exec = JsonDatabaseFileBackend::new(&opt.database_file_path);
Some(JsonDatabase::new(db_exec))
with_database(opt, Some(JsonDatabase::new(db_exec)));
};
if opt.no_library {
with(None::<NoLibrary>, 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(BeetsLibrary::new(lib_exec)), db);
} else {
let lib_exec = BeetsLibraryProcessExecutor::default().config(opt.beets_config_file_path);
with(Some(BeetsLibrary::new(lib_exec)), db);
}
}
#[cfg(test)]