From f36247f53b05380b75dd05c54df12bb548b64452 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Wed, 10 Jan 2024 18:58:29 +0100 Subject: [PATCH] Auto-generate the method identifiers --- Cargo.lock | 7 ++ Cargo.toml | 1 + src/bin/mh-edit.rs | 89 ++++++++---------- src/lib.rs | 222 ++++++++++++++++++++------------------------- 4 files changed, 140 insertions(+), 179 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 491d365..4ad4190 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 395c326..9fe2d21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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} diff --git a/src/bin/mh-edit.rs b/src/bin/mh-edit.rs index 205fb13..5b717f3 100644 --- a/src/bin/mh-edit.rs +++ b/src/bin/mh-edit.rs @@ -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) => { - match $cmd { - UrlCommand::Add(url_value) => { - $mh.$add(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) - .expect("failed to remove URL(s)"); - } - UrlCommand::Set(url_value) => { - $mh.$set(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)) - .expect("failed to clear URL(s)"); + ($cmd:ident, $mh:ident, $field:ident, $url:ident) => { + paste! { + match $cmd { + UrlCommand::Add(url_value) => { + $mh.[](ArtistId::new(url_value.artist), url_value.$url) + .expect("failed to add URL(s)"); + } + UrlCommand::Remove(url_value) => { + $mh.[](ArtistId::new(url_value.artist), url_value.$url) + .expect("failed to remove URL(s)"); + } + UrlCommand::Set(url_value) => { + $mh.[](ArtistId::new(url_value.artist), url_value.$url) + .expect("failed to set URL(s)"); + } + UrlCommand::Clear(artist_value) => { + $mh.[](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) + } } } } diff --git a/src/lib.rs b/src/lib.rs index 523993c..7131ff5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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,41 +379,45 @@ pub struct Artist { } macro_rules! artist_unique_url_dispatch { - ($add:ident, $remove:ident, $set:ident, $clear:ident, $field:ident) => { - fn $add>(&mut self, url: S) -> Result<(), Error> { - Self::add_unique_url(&mut self.properties.$field, url) - } + ($field:ident) => { + paste! { + fn []>(&mut self, url: S) -> Result<(), Error> { + Self::add_unique_url(&mut self.properties.$field, url) + } - fn $remove>(&mut self, url: S) -> Result<(), Error> { - Self::remove_unique_url(&mut self.properties.$field, url) - } + fn []>(&mut self, url: S) -> Result<(), Error> { + Self::remove_unique_url(&mut self.properties.$field, url) + } - fn $set>(&mut self, url: S) -> Result<(), Error> { - Self::set_unique_url(&mut self.properties.$field, url) - } + fn []>(&mut self, url: S) -> Result<(), Error> { + Self::set_unique_url(&mut self.properties.$field, url) + } - fn $clear(&mut self) { - Self::clear_unique_url(&mut self.properties.$field); + fn [](&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>(&mut self, urls: Vec) -> Result<(), Error> { - Self::add_multi_urls(&mut self.properties.$field, urls) - } + ($field:ident) => { + paste! { + fn []>(&mut self, urls: Vec) -> Result<(), Error> { + Self::add_multi_urls(&mut self.properties.$field, urls) + } - fn $remove>(&mut self, urls: Vec) -> Result<(), Error> { - Self::remove_multi_urls(&mut self.properties.$field, urls) - } + fn []>(&mut self, urls: Vec) -> Result<(), Error> { + Self::remove_multi_urls(&mut self.properties.$field, urls) + } - fn $set>(&mut self, urls: Vec) -> Result<(), Error> { - Self::set_multi_urls(&mut self.properties.$field, urls) - } + fn []>(&mut self, urls: Vec) -> Result<(), Error> { + Self::set_multi_urls(&mut self.properties.$field, urls) + } - fn $clear(&mut self) { - Self::clear_multi_urls(&mut self.properties.$field); + fn [](&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,67 +702,76 @@ pub struct MusicHoard { } macro_rules! music_hoard_unique_url_dispatch { - ($add:ident, $remove:ident, $set:ident, $clear:ident) => { - pub fn $add, S: AsRef>( - &mut self, - artist_id: ID, - url: S, - ) -> Result<(), Error> { - self.get_artist_or_err(artist_id.as_ref())?.$add(url) - } + ($field:ident) => { + paste! { + pub fn [], S: AsRef>( + &mut self, + artist_id: ID, + url: S, + ) -> Result<(), Error> { + self.get_artist_or_err(artist_id.as_ref())?.[](url) + } - pub fn $remove, S: AsRef>( - &mut self, - artist_id: ID, - url: S, - ) -> Result<(), Error> { - self.get_artist_or_err(artist_id.as_ref())?.$remove(url) - } + pub fn [], S: AsRef>( + &mut self, + artist_id: ID, + url: S, + ) -> Result<(), Error> { + self.get_artist_or_err(artist_id.as_ref())?.[](url) + } - pub fn $set, S: AsRef>( - &mut self, - artist_id: ID, - url: S, - ) -> Result<(), Error> { - self.get_artist_or_err(artist_id.as_ref())?.$set(url) - } + pub fn [], S: AsRef>( + &mut self, + artist_id: ID, + url: S, + ) -> Result<(), Error> { + self.get_artist_or_err(artist_id.as_ref())?.[](url) + } - pub fn $clear>(&mut self, artist_id: ID) -> Result<(), Error> { - self.get_artist_or_err(artist_id.as_ref())?.$clear(); - Ok(()) + pub fn []>( + &mut self, + artist_id: ID, + ) -> Result<(), Error> { + self.get_artist_or_err(artist_id.as_ref())?.[](); + Ok(()) + } } }; } macro_rules! music_hoard_multi_url_dispatch { - ($add:ident, $remove:ident, $set:ident, $clear:ident) => { - pub fn $add, S: AsRef>( - &mut self, - artist_id: ID, - urls: Vec, - ) -> Result<(), Error> { - self.get_artist_or_err(artist_id.as_ref())?.$add(urls) - } + ($field:ident) => { + paste! { + pub fn [], S: AsRef>( + &mut self, + artist_id: ID, + urls: Vec, + ) -> Result<(), Error> { + self.get_artist_or_err(artist_id.as_ref())?.[](urls) + } - pub fn $remove, S: AsRef>( - &mut self, - artist_id: ID, - urls: Vec, - ) -> Result<(), Error> { - self.get_artist_or_err(artist_id.as_ref())?.$remove(urls) - } + pub fn [], S: AsRef>( + &mut self, + artist_id: ID, + urls: Vec, + ) -> Result<(), Error> { + self.get_artist_or_err(artist_id.as_ref())?.[](urls) + } - pub fn $set, S: AsRef>( - &mut self, - artist_id: ID, - urls: Vec, - ) -> Result<(), Error> { - self.get_artist_or_err(artist_id.as_ref())?.$set(urls) - } + pub fn [], S: AsRef>( + &mut self, + artist_id: ID, + urls: Vec, + ) -> Result<(), Error> { + self.get_artist_or_err(artist_id.as_ref())?.[](urls) + } - pub fn $clear>(&mut self, artist_id: ID) -> Result<(), Error> { - self.get_artist_or_err(artist_id.as_ref())?.$clear(); - Ok(()) + pub fn []>( + &mut self, artist_id: ID, + ) -> Result<(), Error> { + self.get_artist_or_err(artist_id.as_ref())?.[](); + Ok(()) + } } }; } @@ -876,33 +866,13 @@ impl MusicHoard { } } - 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();