WIP: 93---musichoard-with-dyn-trait-or-generics #94

Closed
wojtek wants to merge 2 commits from 93---musichoard-with-dyn-trait-or-generics into main
Showing only changes of commit 73d0144696 - Show all commits

View File

@ -2,9 +2,6 @@ use std::fs::OpenOptions;
use std::path::PathBuf; use std::path::PathBuf;
use std::{ffi::OsString, io}; use std::{ffi::OsString, io};
use musichoard::database::NoDatabase;
use musichoard::library::NoLibrary;
use musichoard::Collection;
use ratatui::{backend::CrosstermBackend, Terminal}; use ratatui::{backend::CrosstermBackend, Terminal};
use structopt::StructOpt; use structopt::StructOpt;
@ -66,31 +63,11 @@ fn with(lib: Option<Box<dyn ILibrary>>, db: Option<Box<dyn IDatabase>>) {
Tui::run(terminal, ui, handler, listener).expect("failed to run tui"); Tui::run(terminal, ui, handler, listener).expect("failed to run tui");
} }
fn with_database(opt: Opt, db: Option<Box<dyn IDatabase>>) {
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() { fn main() {
let opt = Opt::from_args(); let opt = Opt::from_args();
if opt.no_database { let database: Option<Box<dyn IDatabase>> = if opt.no_database {
with_database(opt, None); None
} else { } else {
// Create an empty database file if it does not exist. // Create an empty database file if it does not exist.
match OpenOptions::new() match OpenOptions::new()
@ -111,8 +88,28 @@ fn main() {
} }
let db_exec = JsonDatabaseFileBackend::new(&opt.database_file_path); 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<Box<dyn ILibrary>> = 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)] #[cfg(test)]