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

View File

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

View File

@ -1,3 +1,4 @@
use paste::paste;
use std::path::PathBuf; use std::path::PathBuf;
use structopt::StructOpt; use structopt::StructOpt;
@ -73,37 +74,39 @@ struct MultiUrlValue {
} }
macro_rules! url_command_dispatch { 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 { match $cmd {
UrlCommand::Add(url_value) => { 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)"); .expect("failed to add URL(s)");
} }
UrlCommand::Remove(url_value) => { 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)"); .expect("failed to remove URL(s)");
} }
UrlCommand::Set(url_value) => { 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)"); .expect("failed to set URL(s)");
} }
UrlCommand::Clear(artist_value) => { 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)"); .expect("failed to clear URL(s)");
} }
} }
}
}; };
} }
macro_rules! single_url_command_dispatch { macro_rules! single_url_command_dispatch {
($cmd:ident, $mh:ident, $add:ident, $remove:ident, $set:ident, $clear:ident) => { ($cmd:ident, $mh:ident, $field:ident) => {
url_command_dispatch!($cmd, $mh, $add, $remove, $set, $clear, url) url_command_dispatch!($cmd, $mh, $field, url)
}; };
} }
macro_rules! multi_url_command_dispatch { macro_rules! multi_url_command_dispatch {
($cmd:ident, $mh:ident, $add:ident, $remove:ident, $set:ident, $clear:ident) => { ($cmd:ident, $mh:ident, $field:ident) => {
url_command_dispatch!($cmd, $mh, $add, $remove, $set, $clear, urls) url_command_dispatch!($cmd, $mh, $field, urls)
}; };
} }
@ -120,38 +123,18 @@ impl ArtistCommand {
.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) => single_url_command_dispatch!( ArtistCommand::MusicBrainz(url_command) => {
url_command, single_url_command_dispatch!(url_command, music_hoard, musicbrainz)
music_hoard, }
add_musicbrainz_url, ArtistCommand::MusicButler(url_command) => {
remove_musicbrainz_url, multi_url_command_dispatch!(url_command, music_hoard, musicbutler)
set_musicbrainz_url, }
clear_musicbrainz_url ArtistCommand::Bandcamp(url_command) => {
), multi_url_command_dispatch!(url_command, music_hoard, bandcamp)
ArtistCommand::MusicButler(url_command) => multi_url_command_dispatch!( }
url_command, ArtistCommand::Qobuz(url_command) => {
music_hoard, single_url_command_dispatch!(url_command, music_hoard, qobuz)
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
),
} }
} }
} }

View File

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