Add method to manually add artist metadata #85

Merged
wojtek merged 16 commits from 55---add-method-to-manually-add-artist-metadata into main 2024-01-10 22:33:58 +01:00
2 changed files with 115 additions and 101 deletions
Showing only changes of commit bbab89d25f - Show all commits

View File

@ -1,16 +1,14 @@
use std::fs::OpenOptions;
use std::path::PathBuf; use std::path::PathBuf;
use structopt::StructOpt; use structopt::StructOpt;
use musichoard::{ use musichoard::{
database::{ database::json::{backend::JsonDatabaseFileBackend, JsonDatabase},
json::{backend::JsonDatabaseFileBackend, JsonDatabase},
IDatabase,
},
library::NoLibrary, library::NoLibrary,
Artist, ArtistId, ArtistProperties, MusicHoard, ArtistId, MusicHoard,
}; };
type MH = MusicHoard<NoLibrary, JsonDatabase<JsonDatabaseFileBackend>>;
#[derive(StructOpt, Debug)] #[derive(StructOpt, Debug)]
struct Opt { struct Opt {
#[structopt(subcommand)] #[structopt(subcommand)]
@ -29,6 +27,14 @@ enum Category {
Artist(ArtistCommand), 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)] #[derive(StructOpt, Debug)]
enum ArtistCommand { enum ArtistCommand {
New(ArtistValue), New(ArtistValue),
@ -66,6 +72,102 @@ struct MultiUrlValue {
urls: Vec<String>, urls: Vec<String>,
} }
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() { fn main() {
let opt = Opt::from_args(); let opt = Opt::from_args();
@ -79,101 +181,7 @@ fn main() {
.load_from_database() .load_from_database()
.expect("failed to load database"); .expect("failed to load database");
match opt.category { opt.category.handle(&mut music_hoard);
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.
}
}
}
}
}
}
music_hoard music_hoard
.save_to_database() .save_to_database()

View File

@ -279,6 +279,12 @@ pub struct ArtistId {
pub name: String, pub name: String,
} }
impl AsRef<ArtistId> for ArtistId {
fn as_ref(&self) -> &ArtistId {
self
}
}
impl ArtistId { impl ArtistId {
pub fn new<S: Into<String>>(name: S) -> ArtistId { pub fn new<S: Into<String>>(name: S) -> ArtistId {
ArtistId { name: name.into() } ArtistId { name: name.into() }