diff --git a/src/bin/mh-edit.rs b/src/bin/mh-edit.rs index 58493c5..b79346c 100644 --- a/src/bin/mh-edit.rs +++ b/src/bin/mh-edit.rs @@ -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>; + #[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,6 +72,102 @@ struct MultiUrlValue { urls: Vec, } +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)"); + } + } + }; +} + +impl ArtistCommand { + fn handle(self, music_hoard: &mut MH) { + match self { + ArtistCommand::New(artist_value) => { + music_hoard + .new_artist(ArtistId::new(artist_value.artist)) + .expect("failed to add new artist"); + } + ArtistCommand::Delete(artist_value) => { + music_hoard + .delete_artist(ArtistId::new(artist_value.artist)) + .expect("failed to delete artist"); + } + 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(_) => { + // Add URL. + } + UrlCommand::Remove(_) => { + // Remove URL if it exists. + } + UrlCommand::Set(_) => { + // Set the URLs regardless of previous (if any) value. + } + UrlCommand::Clear(_) => { + // Remove the URLs. + } + } + } + ArtistCommand::Bandcamp(url_command) => { + match url_command { + UrlCommand::Add(_) => { + // Add URL. + } + UrlCommand::Remove(_) => { + // Remove URL if it exists. + } + UrlCommand::Set(_) => { + // Set the URLs regardless of previous (if any) value. + } + UrlCommand::Clear(_) => { + // Remove the URLs. + } + } + } + ArtistCommand::Qobuz(url_command) => { + match url_command { + UrlCommand::Add(_) => { + // Add URL or return error if one already existss. + } + UrlCommand::Remove(_) => { + // Remove URL if it exists. + } + UrlCommand::Set(_) => { + // Set the URL regardless of previous (if any) value. + } + UrlCommand::Clear(_) => { + // Remove the URL. + } + } + } + } + } +} + fn main() { let opt = Opt::from_args(); @@ -79,101 +181,7 @@ fn main() { .load_from_database() .expect("failed to load database"); - match opt.category { - Category::Artist(artist_command) => { - match artist_command { - ArtistCommand::New(artist_value) => { - music_hoard - .new_artist(ArtistId::new(artist_value.artist)) - .expect("failed to add new artist"); - } - ArtistCommand::Delete(artist_value) => { - music_hoard - .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::MusicButler(url_command) => { - match url_command { - UrlCommand::Add(_) => { - // Add URL. - } - UrlCommand::Remove(_) => { - // Remove URL if it exists. - } - UrlCommand::Set(_) => { - // Set the URLs regardless of previous (if any) value. - } - UrlCommand::Clear(_) => { - // Remove the URLs. - } - } - } - ArtistCommand::Bandcamp(url_command) => { - match url_command { - UrlCommand::Add(_) => { - // Add URL. - } - UrlCommand::Remove(_) => { - // Remove URL if it exists. - } - UrlCommand::Set(_) => { - // Set the URLs regardless of previous (if any) value. - } - UrlCommand::Clear(_) => { - // Remove the URLs. - } - } - } - ArtistCommand::Qobuz(url_command) => { - match url_command { - UrlCommand::Add(_) => { - // Add URL or return error if one already existss. - } - UrlCommand::Remove(_) => { - // Remove URL if it exists. - } - UrlCommand::Set(_) => { - // Set the URL regardless of previous (if any) value. - } - UrlCommand::Clear(_) => { - // Remove the URL. - } - } - } - } - } - } + opt.category.handle(&mut music_hoard); music_hoard .save_to_database() diff --git a/src/lib.rs b/src/lib.rs index b7cb440..97527a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -279,6 +279,12 @@ pub struct ArtistId { pub name: String, } +impl AsRef for ArtistId { + fn as_ref(&self) -> &ArtistId { + self + } +} + impl ArtistId { pub fn new>(name: S) -> ArtistId { ArtistId { name: name.into() }