Fix with_xxx function naming in main.rs (#101)
All checks were successful
Cargo CI / Build and Test (push) Successful in 1m0s
Cargo CI / Lint (push) Successful in 44s
Cargo CI / Build and Test (pull_request) Successful in 1m0s
Cargo CI / Lint (pull_request) Successful in 42s

Closes #100

Reviewed-on: #101
This commit is contained in:
Wojciech Kozlowski 2024-01-12 21:15:59 +01:00
parent 845e9b09f4
commit a315bf4229

View File

@ -1,31 +1,37 @@
use std::fs::OpenOptions;
use std::path::PathBuf;
use std::{ffi::OsString, io};
use std::{ffi::OsString, fs::OpenOptions, io, path::PathBuf};
use musichoard::database::NullDatabase;
use musichoard::library::{ILibrary, NullLibrary};
use musichoard::{Collection, MusicHoardBuilder};
use ratatui::{backend::CrosstermBackend, Terminal};
use structopt::StructOpt;
use musichoard::{
database::{
json::{backend::JsonDatabaseFileBackend, JsonDatabase},
IDatabase,
IDatabase, NullDatabase,
},
library::beets::{
library::{
beets::{
executor::{ssh::BeetsLibrarySshExecutor, BeetsLibraryProcessExecutor},
BeetsLibrary,
},
NoLibrary,
ILibrary, NullLibrary,
},
Collection, MusicHoardBuilder, NoDatabase, NoLibrary,
};
mod tui;
use tui::ui::Ui;
use tui::{event::EventChannel, handler::EventHandler, listener::EventListener, Tui};
use tui::{event::EventChannel, handler::EventHandler, listener::EventListener, ui::Ui, Tui};
#[derive(StructOpt)]
struct Opt {
#[structopt(flatten)]
lib_opt: LibOpt,
#[structopt(flatten)]
db_opt: DbOpt,
}
#[derive(StructOpt)]
struct LibOpt {
#[structopt(long = "ssh", help = "Beets SSH URI")]
beets_ssh_uri: Option<OsString>,
@ -34,7 +40,10 @@ struct Opt {
#[structopt(long = "no-library", help = "Do not connect to the library")]
no_library: bool,
}
#[derive(StructOpt)]
struct DbOpt {
#[structopt(
long = "database",
help = "Database file path",
@ -63,42 +72,19 @@ fn with<LIB: ILibrary, DB: IDatabase>(builder: MusicHoardBuilder<LIB, DB>) {
Tui::run(terminal, ui, handler, listener).expect("failed to run tui");
}
fn with_database<DB: IDatabase>(opt: Opt, builder: MusicHoardBuilder<NoLibrary, DB>) {
if opt.no_library {
with(builder.set_library(NullLibrary));
} 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(builder.set_library(BeetsLibrary::new(lib_exec)));
} else {
let lib_exec = BeetsLibraryProcessExecutor::default().config(opt.beets_config_file_path);
with(builder.set_library(BeetsLibrary::new(lib_exec)));
}
}
fn main() {
let opt = Opt::from_args();
let builder = MusicHoardBuilder::default();
if opt.no_database {
with_database(opt, builder.set_database(NullDatabase));
fn with_database<LIB: ILibrary>(db_opt: DbOpt, builder: MusicHoardBuilder<LIB, NoDatabase>) {
if db_opt.no_database {
with(builder.set_database(NullDatabase));
} else {
// Create an empty database file if it does not exist.
match OpenOptions::new()
.write(true)
.create_new(true)
.open(&opt.database_file_path)
.open(&db_opt.database_file_path)
{
Ok(f) => {
drop(f);
JsonDatabase::new(JsonDatabaseFileBackend::new(&opt.database_file_path))
JsonDatabase::new(JsonDatabaseFileBackend::new(&db_opt.database_file_path))
.save::<Collection>(&vec![])
.expect("failed to create empty database");
}
@ -108,11 +94,38 @@ fn main() {
},
}
let db_exec = JsonDatabaseFileBackend::new(&opt.database_file_path);
with_database(opt, builder.set_database(JsonDatabase::new(db_exec)));
let db_exec = JsonDatabaseFileBackend::new(&db_opt.database_file_path);
with(builder.set_database(JsonDatabase::new(db_exec)));
};
}
fn with_library(lib_opt: LibOpt, db_opt: DbOpt, builder: MusicHoardBuilder<NoLibrary, NoDatabase>) {
if lib_opt.no_library {
with_database(db_opt, builder.set_library(NullLibrary));
} else if let Some(uri) = lib_opt.beets_ssh_uri {
let uri = uri.into_string().expect("invalid SSH URI");
let beets_config_file_path = lib_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_database(db_opt, builder.set_library(BeetsLibrary::new(lib_exec)));
} else {
let lib_exec =
BeetsLibraryProcessExecutor::default().config(lib_opt.beets_config_file_path);
with_database(db_opt, builder.set_library(BeetsLibrary::new(lib_exec)));
}
}
fn main() {
let opt = Opt::from_args();
let builder = MusicHoardBuilder::default();
with_library(opt.lib_opt, opt.db_opt, builder);
}
#[cfg(test)]
#[macro_use]
mod testlib;