Prepare to reduce code repetition in mh-edit
This commit is contained in:
parent
d8a1ec1645
commit
bbab89d25f
@ -1,16 +1,14 @@
|
||||
use std::fs::OpenOptions;
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
|
||||
use musichoard::{
|
||||
database::{
|
||||
json::{backend::JsonDatabaseFileBackend, JsonDatabase},
|
||||
IDatabase,
|
||||
},
|
||||
database::json::{backend::JsonDatabaseFileBackend, JsonDatabase},
|
||||
library::NoLibrary,
|
||||
Artist, ArtistId, ArtistProperties, MusicHoard,
|
||||
ArtistId, MusicHoard,
|
||||
};
|
||||
|
||||
type MH = MusicHoard<NoLibrary, JsonDatabase<JsonDatabaseFileBackend>>;
|
||||
|
||||
#[derive(StructOpt, Debug)]
|
||||
struct Opt {
|
||||
#[structopt(subcommand)]
|
||||
@ -29,6 +27,14 @@ enum Category {
|
||||
Artist(ArtistCommand),
|
||||
}
|
||||
|
||||
impl Category {
|
||||
fn handle(self, music_hoard: &mut MH) {
|
||||
match self {
|
||||
Category::Artist(artist_command) => artist_command.handle(music_hoard),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(StructOpt, Debug)]
|
||||
enum ArtistCommand {
|
||||
New(ArtistValue),
|
||||
@ -66,22 +72,32 @@ struct MultiUrlValue {
|
||||
urls: Vec<String>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let opt = Opt::from_args();
|
||||
macro_rules! url_command_dispatch {
|
||||
($command:ident, $mh:ident, $add:ident, $remove:ident, $set:ident, $clear:ident) => {
|
||||
match $command {
|
||||
UrlCommand::Add(single_url_value) => {
|
||||
$mh.$add(ArtistId::new(single_url_value.artist), single_url_value.url)
|
||||
.expect("failed to add URL(s)");
|
||||
}
|
||||
UrlCommand::Remove(single_url_value) => {
|
||||
$mh.$remove(ArtistId::new(single_url_value.artist), single_url_value.url)
|
||||
.expect("failed to remove URL(s)");
|
||||
}
|
||||
UrlCommand::Set(single_url_value) => {
|
||||
$mh.$set(ArtistId::new(single_url_value.artist), single_url_value.url)
|
||||
.expect("failed to set URL(s)");
|
||||
}
|
||||
UrlCommand::Clear(artist_value) => {
|
||||
$mh.$clear(ArtistId::new(artist_value.artist))
|
||||
.expect("failed to clear URL(s)");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let lib: Option<NoLibrary> = None;
|
||||
let db = Some(JsonDatabase::new(JsonDatabaseFileBackend::new(
|
||||
&opt.database_file_path,
|
||||
)));
|
||||
|
||||
let mut music_hoard = MusicHoard::new(lib, db);
|
||||
music_hoard
|
||||
.load_from_database()
|
||||
.expect("failed to load database");
|
||||
|
||||
match opt.category {
|
||||
Category::Artist(artist_command) => {
|
||||
match artist_command {
|
||||
impl ArtistCommand {
|
||||
fn handle(self, music_hoard: &mut MH) {
|
||||
match self {
|
||||
ArtistCommand::New(artist_value) => {
|
||||
music_hoard
|
||||
.new_artist(ArtistId::new(artist_value.artist))
|
||||
@ -92,37 +108,14 @@ fn main() {
|
||||
.delete_artist(ArtistId::new(artist_value.artist))
|
||||
.expect("failed to delete artist");
|
||||
}
|
||||
ArtistCommand::MusicBrainz(url_command) => match url_command {
|
||||
UrlCommand::Add(single_url_value) => {
|
||||
music_hoard
|
||||
.add_musicbrainz_url(
|
||||
ArtistId::new(single_url_value.artist),
|
||||
single_url_value.url,
|
||||
)
|
||||
.expect("failed to add MusicBrainz URL");
|
||||
}
|
||||
UrlCommand::Remove(single_url_value) => {
|
||||
music_hoard
|
||||
.remove_musicbrainz_url(
|
||||
ArtistId::new(single_url_value.artist),
|
||||
single_url_value.url,
|
||||
)
|
||||
.expect("failed to remove MusicBrainz URL");
|
||||
}
|
||||
UrlCommand::Set(single_url_value) => {
|
||||
music_hoard
|
||||
.set_musicbrainz_url(
|
||||
ArtistId::new(single_url_value.artist),
|
||||
single_url_value.url,
|
||||
)
|
||||
.expect("failed to set MusicBrainz URL");
|
||||
}
|
||||
UrlCommand::Clear(artist_value) => {
|
||||
music_hoard
|
||||
.clear_musicbrainz_url(ArtistId::new(artist_value.artist))
|
||||
.expect("failed to clear MusicBrainz URL");
|
||||
}
|
||||
},
|
||||
ArtistCommand::MusicBrainz(url_command) => url_command_dispatch!(
|
||||
url_command,
|
||||
music_hoard,
|
||||
add_musicbrainz_url,
|
||||
remove_musicbrainz_url,
|
||||
set_musicbrainz_url,
|
||||
clear_musicbrainz_url
|
||||
),
|
||||
ArtistCommand::MusicButler(url_command) => {
|
||||
match url_command {
|
||||
UrlCommand::Add(_) => {
|
||||
@ -175,6 +168,21 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let opt = Opt::from_args();
|
||||
|
||||
let lib: Option<NoLibrary> = None;
|
||||
let db = Some(JsonDatabase::new(JsonDatabaseFileBackend::new(
|
||||
&opt.database_file_path,
|
||||
)));
|
||||
|
||||
let mut music_hoard = MusicHoard::new(lib, db);
|
||||
music_hoard
|
||||
.load_from_database()
|
||||
.expect("failed to load database");
|
||||
|
||||
opt.category.handle(&mut music_hoard);
|
||||
|
||||
music_hoard
|
||||
.save_to_database()
|
||||
.expect("failed to save database");
|
||||
|
@ -279,6 +279,12 @@ pub struct ArtistId {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl AsRef<ArtistId> for ArtistId {
|
||||
fn as_ref(&self) -> &ArtistId {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl ArtistId {
|
||||
pub fn new<S: Into<String>>(name: S) -> ArtistId {
|
||||
ArtistId { name: name.into() }
|
||||
|
Loading…
Reference in New Issue
Block a user