Auto-generate the method identifiers
All checks were successful
Cargo CI / Build and Test (pull_request) Successful in 1m3s
Cargo CI / Lint (pull_request) Successful in 43s

This commit is contained in:
Wojciech Kozlowski 2024-01-10 18:58:29 +01:00
parent d0e04d8a78
commit f36247f53b
4 changed files with 140 additions and 179 deletions

7
Cargo.lock generated
View File

@ -369,6 +369,7 @@ dependencies = [
"mockall",
"once_cell",
"openssh",
"paste",
"ratatui",
"serde",
"serde_json",
@ -456,6 +457,12 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "paste"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c"
[[package]]
name = "percent-encoding"
version = "2.2.0"

View File

@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
crossterm = { version = "0.26.1", optional = true}
openssh = { version = "0.9.9", features = ["native-mux"], default-features = false, optional = true}
paste = { version = "1.0.14" }
ratatui = { version = "0.20.1", optional = true}
serde = { version = "1.0.159", features = ["derive"] }
serde_json = { version = "1.0.95", optional = true}

View File

@ -1,3 +1,4 @@
use paste::paste;
use std::path::PathBuf;
use structopt::StructOpt;
@ -73,37 +74,39 @@ struct MultiUrlValue {
}
macro_rules! url_command_dispatch {
($cmd:ident, $mh:ident, $add:ident, $remove:ident, $set:ident, $clear:ident, $url:ident) => {
($cmd:ident, $mh:ident, $field:ident, $url:ident) => {
paste! {
match $cmd {
UrlCommand::Add(url_value) => {
$mh.$add(ArtistId::new(url_value.artist), url_value.$url)
$mh.[<add_ $field _ $url>](ArtistId::new(url_value.artist), url_value.$url)
.expect("failed to add URL(s)");
}
UrlCommand::Remove(url_value) => {
$mh.$remove(ArtistId::new(url_value.artist), url_value.$url)
$mh.[<remove_ $field _ $url>](ArtistId::new(url_value.artist), url_value.$url)
.expect("failed to remove URL(s)");
}
UrlCommand::Set(url_value) => {
$mh.$set(ArtistId::new(url_value.artist), url_value.$url)
$mh.[<set_ $field _ $url>](ArtistId::new(url_value.artist), url_value.$url)
.expect("failed to set URL(s)");
}
UrlCommand::Clear(artist_value) => {
$mh.$clear(ArtistId::new(artist_value.artist))
$mh.[<clear_ $field _ $url>](ArtistId::new(artist_value.artist))
.expect("failed to clear URL(s)");
}
}
}
};
}
macro_rules! single_url_command_dispatch {
($cmd:ident, $mh:ident, $add:ident, $remove:ident, $set:ident, $clear:ident) => {
url_command_dispatch!($cmd, $mh, $add, $remove, $set, $clear, url)
($cmd:ident, $mh:ident, $field:ident) => {
url_command_dispatch!($cmd, $mh, $field, url)
};
}
macro_rules! multi_url_command_dispatch {
($cmd:ident, $mh:ident, $add:ident, $remove:ident, $set:ident, $clear:ident) => {
url_command_dispatch!($cmd, $mh, $add, $remove, $set, $clear, urls)
($cmd:ident, $mh:ident, $field:ident) => {
url_command_dispatch!($cmd, $mh, $field, urls)
};
}
@ -120,38 +123,18 @@ impl ArtistCommand {
.delete_artist(ArtistId::new(artist_value.artist))
.expect("failed to delete artist");
}
ArtistCommand::MusicBrainz(url_command) => single_url_command_dispatch!(
url_command,
music_hoard,
add_musicbrainz_url,
remove_musicbrainz_url,
set_musicbrainz_url,
clear_musicbrainz_url
),
ArtistCommand::MusicButler(url_command) => multi_url_command_dispatch!(
url_command,
music_hoard,
add_musicbutler_urls,
remove_musicbutler_urls,
set_musicbutler_urls,
clear_musicbutler_urls
),
ArtistCommand::Bandcamp(url_command) => multi_url_command_dispatch!(
url_command,
music_hoard,
add_bandcamp_urls,
remove_bandcamp_urls,
set_bandcamp_urls,
clear_bandcamp_urls
),
ArtistCommand::Qobuz(url_command) => single_url_command_dispatch!(
url_command,
music_hoard,
add_qobuz_url,
remove_qobuz_url,
set_qobuz_url,
clear_qobuz_url
),
ArtistCommand::MusicBrainz(url_command) => {
single_url_command_dispatch!(url_command, music_hoard, musicbrainz)
}
ArtistCommand::MusicButler(url_command) => {
multi_url_command_dispatch!(url_command, music_hoard, musicbutler)
}
ArtistCommand::Bandcamp(url_command) => {
multi_url_command_dispatch!(url_command, music_hoard, bandcamp)
}
ArtistCommand::Qobuz(url_command) => {
single_url_command_dispatch!(url_command, music_hoard, qobuz)
}
}
}
}

View File

@ -13,6 +13,7 @@ use std::{
use database::IDatabase;
use library::{ILibrary, Item, Query};
use paste::paste;
use serde::{Deserialize, Serialize};
use url::Url;
use uuid::Uuid;
@ -378,42 +379,46 @@ pub struct Artist {
}
macro_rules! artist_unique_url_dispatch {
($add:ident, $remove:ident, $set:ident, $clear:ident, $field:ident) => {
fn $add<S: AsRef<str>>(&mut self, url: S) -> Result<(), Error> {
($field:ident) => {
paste! {
fn [<add_ $field _url>]<S: AsRef<str>>(&mut self, url: S) -> Result<(), Error> {
Self::add_unique_url(&mut self.properties.$field, url)
}
fn $remove<S: AsRef<str>>(&mut self, url: S) -> Result<(), Error> {
fn [<remove_ $field _url>]<S: AsRef<str>>(&mut self, url: S) -> Result<(), Error> {
Self::remove_unique_url(&mut self.properties.$field, url)
}
fn $set<S: AsRef<str>>(&mut self, url: S) -> Result<(), Error> {
fn [<set_ $field _url>]<S: AsRef<str>>(&mut self, url: S) -> Result<(), Error> {
Self::set_unique_url(&mut self.properties.$field, url)
}
fn $clear(&mut self) {
fn [<clear_ $field _url>](&mut self) {
Self::clear_unique_url(&mut self.properties.$field);
}
}
};
}
macro_rules! artist_multi_url_dispatch {
($add:ident, $remove:ident, $set:ident, $clear:ident, $field:ident) => {
fn $add<S: AsRef<str>>(&mut self, urls: Vec<S>) -> Result<(), Error> {
($field:ident) => {
paste! {
fn [<add_ $field _urls>]<S: AsRef<str>>(&mut self, urls: Vec<S>) -> Result<(), Error> {
Self::add_multi_urls(&mut self.properties.$field, urls)
}
fn $remove<S: AsRef<str>>(&mut self, urls: Vec<S>) -> Result<(), Error> {
fn [<remove_ $field _urls>]<S: AsRef<str>>(&mut self, urls: Vec<S>) -> Result<(), Error> {
Self::remove_multi_urls(&mut self.properties.$field, urls)
}
fn $set<S: AsRef<str>>(&mut self, urls: Vec<S>) -> Result<(), Error> {
fn [<set_ $field _urls>]<S: AsRef<str>>(&mut self, urls: Vec<S>) -> Result<(), Error> {
Self::set_multi_urls(&mut self.properties.$field, urls)
}
fn $clear(&mut self) {
fn [<clear_ $field _urls>](&mut self) {
Self::clear_multi_urls(&mut self.properties.$field);
}
}
};
}
@ -526,37 +531,13 @@ impl Artist {
container.clear();
}
artist_unique_url_dispatch!(
add_musicbrainz_url,
remove_musicbrainz_url,
set_musicbrainz_url,
clear_musicbrainz_url,
musicbrainz
);
artist_unique_url_dispatch!(musicbrainz);
artist_multi_url_dispatch!(
add_musicbutler_urls,
remove_musicbutler_urls,
set_musicbutler_urls,
clear_musicbutler_urls,
musicbutler
);
artist_multi_url_dispatch!(musicbutler);
artist_multi_url_dispatch!(
add_bandcamp_urls,
remove_bandcamp_urls,
set_bandcamp_urls,
clear_bandcamp_urls,
bandcamp
);
artist_multi_url_dispatch!(bandcamp);
artist_unique_url_dispatch!(
add_qobuz_url,
remove_qobuz_url,
set_qobuz_url,
clear_qobuz_url,
qobuz
);
artist_unique_url_dispatch!(qobuz);
}
impl PartialOrd for Artist {
@ -721,68 +702,77 @@ pub struct MusicHoard<LIB, DB> {
}
macro_rules! music_hoard_unique_url_dispatch {
($add:ident, $remove:ident, $set:ident, $clear:ident) => {
pub fn $add<ID: AsRef<ArtistId>, S: AsRef<str>>(
($field:ident) => {
paste! {
pub fn [<add_ $field _url>]<ID: AsRef<ArtistId>, S: AsRef<str>>(
&mut self,
artist_id: ID,
url: S,
) -> Result<(), Error> {
self.get_artist_or_err(artist_id.as_ref())?.$add(url)
self.get_artist_or_err(artist_id.as_ref())?.[<add_ $field _url>](url)
}
pub fn $remove<ID: AsRef<ArtistId>, S: AsRef<str>>(
pub fn [<remove_ $field _url>]<ID: AsRef<ArtistId>, S: AsRef<str>>(
&mut self,
artist_id: ID,
url: S,
) -> Result<(), Error> {
self.get_artist_or_err(artist_id.as_ref())?.$remove(url)
self.get_artist_or_err(artist_id.as_ref())?.[<remove_ $field _url>](url)
}
pub fn $set<ID: AsRef<ArtistId>, S: AsRef<str>>(
pub fn [<set_ $field _url>]<ID: AsRef<ArtistId>, S: AsRef<str>>(
&mut self,
artist_id: ID,
url: S,
) -> Result<(), Error> {
self.get_artist_or_err(artist_id.as_ref())?.$set(url)
self.get_artist_or_err(artist_id.as_ref())?.[<set_ $field _url>](url)
}
pub fn $clear<ID: AsRef<ArtistId>>(&mut self, artist_id: ID) -> Result<(), Error> {
self.get_artist_or_err(artist_id.as_ref())?.$clear();
pub fn [<clear_ $field _url>]<ID: AsRef<ArtistId>>(
&mut self,
artist_id: ID,
) -> Result<(), Error> {
self.get_artist_or_err(artist_id.as_ref())?.[<clear_ $field _url>]();
Ok(())
}
}
};
}
macro_rules! music_hoard_multi_url_dispatch {
($add:ident, $remove:ident, $set:ident, $clear:ident) => {
pub fn $add<ID: AsRef<ArtistId>, S: AsRef<str>>(
($field:ident) => {
paste! {
pub fn [<add_ $field _urls>]<ID: AsRef<ArtistId>, S: AsRef<str>>(
&mut self,
artist_id: ID,
urls: Vec<S>,
) -> Result<(), Error> {
self.get_artist_or_err(artist_id.as_ref())?.$add(urls)
self.get_artist_or_err(artist_id.as_ref())?.[<add_ $field _urls>](urls)
}
pub fn $remove<ID: AsRef<ArtistId>, S: AsRef<str>>(
pub fn [<remove_ $field _urls>]<ID: AsRef<ArtistId>, S: AsRef<str>>(
&mut self,
artist_id: ID,
urls: Vec<S>,
) -> Result<(), Error> {
self.get_artist_or_err(artist_id.as_ref())?.$remove(urls)
self.get_artist_or_err(artist_id.as_ref())?.[<remove_ $field _urls>](urls)
}
pub fn $set<ID: AsRef<ArtistId>, S: AsRef<str>>(
pub fn [<set_ $field _urls>]<ID: AsRef<ArtistId>, S: AsRef<str>>(
&mut self,
artist_id: ID,
urls: Vec<S>,
) -> Result<(), Error> {
self.get_artist_or_err(artist_id.as_ref())?.$set(urls)
self.get_artist_or_err(artist_id.as_ref())?.[<set_ $field _urls>](urls)
}
pub fn $clear<ID: AsRef<ArtistId>>(&mut self, artist_id: ID) -> Result<(), Error> {
self.get_artist_or_err(artist_id.as_ref())?.$clear();
pub fn [<clear_ $field _urls>]<ID: AsRef<ArtistId>>(
&mut self, artist_id: ID,
) -> Result<(), Error> {
self.get_artist_or_err(artist_id.as_ref())?.[<clear_ $field _urls>]();
Ok(())
}
}
};
}
@ -876,33 +866,13 @@ impl<LIB: ILibrary, DB: IDatabase> MusicHoard<LIB, DB> {
}
}
music_hoard_unique_url_dispatch!(
add_musicbrainz_url,
remove_musicbrainz_url,
set_musicbrainz_url,
clear_musicbrainz_url
);
music_hoard_unique_url_dispatch!(musicbrainz);
music_hoard_multi_url_dispatch!(
add_musicbutler_urls,
remove_musicbutler_urls,
set_musicbutler_urls,
clear_musicbutler_urls
);
music_hoard_multi_url_dispatch!(musicbutler);
music_hoard_multi_url_dispatch!(
add_bandcamp_urls,
remove_bandcamp_urls,
set_bandcamp_urls,
clear_bandcamp_urls
);
music_hoard_multi_url_dispatch!(bandcamp);
music_hoard_unique_url_dispatch!(
add_qobuz_url,
remove_qobuz_url,
set_qobuz_url,
clear_qobuz_url
);
music_hoard_unique_url_dispatch!(qobuz);
fn sort(collection: &mut [Artist]) {
collection.sort_unstable();