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,22 +72,32 @@ struct MultiUrlValue {
urls: Vec<String>, urls: Vec<String>,
} }
fn main() { macro_rules! url_command_dispatch {
let opt = Opt::from_args(); ($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; impl ArtistCommand {
let db = Some(JsonDatabase::new(JsonDatabaseFileBackend::new( fn handle(self, music_hoard: &mut MH) {
&opt.database_file_path, match self {
)));
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 {
ArtistCommand::New(artist_value) => { ArtistCommand::New(artist_value) => {
music_hoard music_hoard
.new_artist(ArtistId::new(artist_value.artist)) .new_artist(ArtistId::new(artist_value.artist))
@ -92,37 +108,14 @@ fn main() {
.delete_artist(ArtistId::new(artist_value.artist)) .delete_artist(ArtistId::new(artist_value.artist))
.expect("failed to delete artist"); .expect("failed to delete artist");
} }
ArtistCommand::MusicBrainz(url_command) => match url_command { ArtistCommand::MusicBrainz(url_command) => url_command_dispatch!(
UrlCommand::Add(single_url_value) => { url_command,
music_hoard music_hoard,
.add_musicbrainz_url( add_musicbrainz_url,
ArtistId::new(single_url_value.artist), remove_musicbrainz_url,
single_url_value.url, set_musicbrainz_url,
) clear_musicbrainz_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) => { ArtistCommand::MusicButler(url_command) => {
match url_command { match url_command {
UrlCommand::Add(_) => { 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 music_hoard
.save_to_database() .save_to_database()
.expect("failed to save database"); .expect("failed to save 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() }