Fix with_xxx function naming in main.rs #101

Merged
wojtek merged 2 commits from 100---fix-with_xxx-function-naming-in-main.rs into main 2024-01-12 21:15:59 +01:00
Showing only changes of commit e8e493a0a0 - Show all commits

View File

@ -23,6 +23,15 @@ use tui::{event::EventChannel, handler::EventHandler, listener::EventListener, u
#[derive(StructOpt)] #[derive(StructOpt)]
struct Opt { struct Opt {
#[structopt(flatten)]
lib_opt: LibOpt,
#[structopt(flatten)]
db_opt: DbOpt,
}
#[derive(StructOpt)]
struct LibOpt {
#[structopt(long = "ssh", help = "Beets SSH URI")] #[structopt(long = "ssh", help = "Beets SSH URI")]
beets_ssh_uri: Option<OsString>, beets_ssh_uri: Option<OsString>,
@ -31,7 +40,10 @@ struct Opt {
#[structopt(long = "no-library", help = "Do not connect to the library")] #[structopt(long = "no-library", help = "Do not connect to the library")]
no_library: bool, no_library: bool,
}
#[derive(StructOpt)]
struct DbOpt {
#[structopt( #[structopt(
long = "database", long = "database",
help = "Database file path", help = "Database file path",
@ -60,19 +72,19 @@ fn with<LIB: ILibrary, DB: IDatabase>(builder: MusicHoardBuilder<LIB, DB>) {
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<LIB: ILibrary>(opt: Opt, builder: MusicHoardBuilder<LIB, NoDatabase>) { fn with_database<LIB: ILibrary>(db_opt: DbOpt, builder: MusicHoardBuilder<LIB, NoDatabase>) {
if opt.no_database { if db_opt.no_database {
with(builder.set_database(NullDatabase)); with(builder.set_database(NullDatabase));
} 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()
.write(true) .write(true)
.create_new(true) .create_new(true)
.open(&opt.database_file_path) .open(&db_opt.database_file_path)
{ {
Ok(f) => { Ok(f) => {
drop(f); drop(f);
JsonDatabase::new(JsonDatabaseFileBackend::new(&opt.database_file_path)) JsonDatabase::new(JsonDatabaseFileBackend::new(&db_opt.database_file_path))
.save::<Collection>(&vec![]) .save::<Collection>(&vec![])
.expect("failed to create empty database"); .expect("failed to create empty database");
} }
@ -82,17 +94,17 @@ fn with_database<LIB: ILibrary>(opt: Opt, builder: MusicHoardBuilder<LIB, NoData
}, },
} }
let db_exec = JsonDatabaseFileBackend::new(&opt.database_file_path); let db_exec = JsonDatabaseFileBackend::new(&db_opt.database_file_path);
with(builder.set_database(JsonDatabase::new(db_exec))); with(builder.set_database(JsonDatabase::new(db_exec)));
}; };
} }
fn with_library(opt: Opt, builder: MusicHoardBuilder<NoLibrary, NoDatabase>) { fn with_library(lib_opt: LibOpt, db_opt: DbOpt, builder: MusicHoardBuilder<NoLibrary, NoDatabase>) {
if opt.no_library { if lib_opt.no_library {
with_database(opt, builder.set_library(NullLibrary)); with_database(db_opt, builder.set_library(NullLibrary));
} else if let Some(uri) = opt.beets_ssh_uri { } else if let Some(uri) = lib_opt.beets_ssh_uri {
let uri = uri.into_string().expect("invalid SSH URI"); let uri = uri.into_string().expect("invalid SSH URI");
let beets_config_file_path = opt let beets_config_file_path = lib_opt
.beets_config_file_path .beets_config_file_path
.map(|s| s.into_string()) .map(|s| s.into_string())
.transpose() .transpose()
@ -100,17 +112,18 @@ fn with_library(opt: Opt, builder: MusicHoardBuilder<NoLibrary, NoDatabase>) {
let lib_exec = BeetsLibrarySshExecutor::new(uri) let lib_exec = BeetsLibrarySshExecutor::new(uri)
.expect("failed to initialise beets") .expect("failed to initialise beets")
.config(beets_config_file_path); .config(beets_config_file_path);
with_database(opt, builder.set_library(BeetsLibrary::new(lib_exec))); with_database(db_opt, builder.set_library(BeetsLibrary::new(lib_exec)));
} else { } else {
let lib_exec = BeetsLibraryProcessExecutor::default().config(opt.beets_config_file_path); let lib_exec =
with_database(opt, builder.set_library(BeetsLibrary::new(lib_exec))); BeetsLibraryProcessExecutor::default().config(lib_opt.beets_config_file_path);
with_database(db_opt, builder.set_library(BeetsLibrary::new(lib_exec)));
} }
} }
fn main() { fn main() {
let opt = Opt::from_args(); let opt = Opt::from_args();
let builder = MusicHoardBuilder::default(); let builder = MusicHoardBuilder::default();
with_library(opt, builder); with_library(opt.lib_opt, opt.db_opt, builder);
} }
#[cfg(test)] #[cfg(test)]