From 53aa1a67ee2d897c7d6b90f9e79bdcbaa5e7ea1e Mon Sep 17 00:00:00 2001 From: Deluan Date: Tue, 30 Dec 2025 17:14:16 -0500 Subject: [PATCH] refactor(plugins): update macro names for websocket and metadata registration to improve clarity and consistency Signed-off-by: Deluan --- .gitignore | 1 + plugins/cmd/ndpgen/internal/generator.go | 17 ++++++++++++----- .../discord-rich-presence-rs/src/lib.rs | 8 ++++---- .../examples/library-inspector-rs/src/lib.rs | 2 +- .../rust/nd-pdk-capabilities/src/lifecycle.rs | 2 +- .../rust/nd-pdk-capabilities/src/metadata.rs | 16 ++++++++-------- .../rust/nd-pdk-capabilities/src/websocket.rs | 8 ++++---- 7 files changed, 31 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index f8fd39bfb..dcd3eaf45 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ docker-compose.yml !contrib/docker-compose.yml binaries navidrome-* +ndpgen AGENTS.md .github/prompts .github/instructions diff --git a/plugins/cmd/ndpgen/internal/generator.go b/plugins/cmd/ndpgen/internal/generator.go index 680457559..42c2dc352 100644 --- a/plugins/cmd/ndpgen/internal/generator.go +++ b/plugins/cmd/ndpgen/internal/generator.go @@ -419,7 +419,7 @@ func rustCapabilityFuncMap(cap Capability) template.FuncMap { "hasHashMap": hasHashMap, "agentName": capabilityAgentName, "providerInterface": func(e Export) string { return e.ProviderInterfaceName() }, - "registerMacroName": registerMacroName, + "registerMacroName": func(name string) string { return registerMacroName(cap.Name, name) }, "snakeCase": ToSnakeCase, "indent": func(spaces int, s string) string { indent := strings.Repeat(" ", spaces) @@ -509,16 +509,23 @@ func hasHashMap(cap Capability) bool { } // registerMacroName returns the macro name for registering an optional method. -// For "GetArtistBiography", returns "register_artist_biography". -func registerMacroName(name string) string { - // Remove common prefixes +// For package "websocket" and method "OnClose", returns "register_websocket_close". +// Also handles deduplication when method name starts with package name (e.g., "scheduler" + "OnSchedulerCallback" → "register_scheduler_callback"). +func registerMacroName(pkg, name string) string { + // Remove common prefixes from method name for _, prefix := range []string{"Get", "On"} { if strings.HasPrefix(name, prefix) { name = name[len(prefix):] break } } - return "register_" + ToSnakeCase(name) + // Check if the method name starts with the package name to avoid duplication + // e.g., package="scheduler", method="SchedulerCallback" → just "register_scheduler_callback" + pkgTitle := strings.Title(pkg) //nolint:staticcheck + if strings.HasPrefix(name, pkgTitle) { + return "register_" + ToSnakeCase(name) + } + return "register_" + ToSnakeCase(pkg) + "_" + ToSnakeCase(name) } // GenerateCapabilityRust generates Rust export wrapper code for a capability. diff --git a/plugins/examples/discord-rich-presence-rs/src/lib.rs b/plugins/examples/discord-rich-presence-rs/src/lib.rs index ae1512b13..829602ba6 100644 --- a/plugins/examples/discord-rich-presence-rs/src/lib.rs +++ b/plugins/examples/discord-rich-presence-rs/src/lib.rs @@ -37,10 +37,10 @@ mod rpc; // Register capabilities using PDK macros nd_pdk::register_scrobbler!(DiscordPlugin); nd_pdk::register_scheduler_callback!(DiscordPlugin); -nd_pdk::register_text_message!(DiscordPlugin); -nd_pdk::register_binary_message!(DiscordPlugin); -nd_pdk::register_error!(DiscordPlugin); -nd_pdk::register_close!(DiscordPlugin); +nd_pdk::register_websocket_text_message!(DiscordPlugin); +nd_pdk::register_websocket_binary_message!(DiscordPlugin); +nd_pdk::register_websocket_error!(DiscordPlugin); +nd_pdk::register_websocket_close!(DiscordPlugin); // ============================================================================ // Constants diff --git a/plugins/examples/library-inspector-rs/src/lib.rs b/plugins/examples/library-inspector-rs/src/lib.rs index c9d20cbbd..1ecae4b9e 100644 --- a/plugins/examples/library-inspector-rs/src/lib.rs +++ b/plugins/examples/library-inspector-rs/src/lib.rs @@ -19,7 +19,7 @@ use nd_pdk::scheduler::{Error as SchedulerError, SchedulerCallbackProvider, Sche use std::fs; // Register capabilities using PDK macros -nd_pdk::register_init!(LibraryInspector); +nd_pdk::register_lifecycle_init!(LibraryInspector); nd_pdk::register_scheduler_callback!(LibraryInspector); // ============================================================================ diff --git a/plugins/pdk/rust/nd-pdk-capabilities/src/lifecycle.rs b/plugins/pdk/rust/nd-pdk-capabilities/src/lifecycle.rs index cd99386de..87b5485ba 100644 --- a/plugins/pdk/rust/nd-pdk-capabilities/src/lifecycle.rs +++ b/plugins/pdk/rust/nd-pdk-capabilities/src/lifecycle.rs @@ -32,7 +32,7 @@ pub trait InitProvider { /// Register the on_init export. /// This macro generates the WASM export function for this method. #[macro_export] -macro_rules! register_init { +macro_rules! register_lifecycle_init { ($plugin_type:ty) => { #[extism_pdk::plugin_fn] pub fn nd_on_init( diff --git a/plugins/pdk/rust/nd-pdk-capabilities/src/metadata.rs b/plugins/pdk/rust/nd-pdk-capabilities/src/metadata.rs index 823a62fc6..7db59cede 100644 --- a/plugins/pdk/rust/nd-pdk-capabilities/src/metadata.rs +++ b/plugins/pdk/rust/nd-pdk-capabilities/src/metadata.rs @@ -212,7 +212,7 @@ pub trait ArtistMBIDProvider { /// Register the get_artist_mbid export. /// This macro generates the WASM export function for this method. #[macro_export] -macro_rules! register_artist_mbid { +macro_rules! register_metadata_artist_mbid { ($plugin_type:ty) => { #[extism_pdk::plugin_fn] pub fn nd_get_artist_mbid( @@ -233,7 +233,7 @@ pub trait ArtistURLProvider { /// Register the get_artist_url export. /// This macro generates the WASM export function for this method. #[macro_export] -macro_rules! register_artist_url { +macro_rules! register_metadata_artist_url { ($plugin_type:ty) => { #[extism_pdk::plugin_fn] pub fn nd_get_artist_url( @@ -254,7 +254,7 @@ pub trait ArtistBiographyProvider { /// Register the get_artist_biography export. /// This macro generates the WASM export function for this method. #[macro_export] -macro_rules! register_artist_biography { +macro_rules! register_metadata_artist_biography { ($plugin_type:ty) => { #[extism_pdk::plugin_fn] pub fn nd_get_artist_biography( @@ -275,7 +275,7 @@ pub trait SimilarArtistsProvider { /// Register the get_similar_artists export. /// This macro generates the WASM export function for this method. #[macro_export] -macro_rules! register_similar_artists { +macro_rules! register_metadata_similar_artists { ($plugin_type:ty) => { #[extism_pdk::plugin_fn] pub fn nd_get_similar_artists( @@ -296,7 +296,7 @@ pub trait ArtistImagesProvider { /// Register the get_artist_images export. /// This macro generates the WASM export function for this method. #[macro_export] -macro_rules! register_artist_images { +macro_rules! register_metadata_artist_images { ($plugin_type:ty) => { #[extism_pdk::plugin_fn] pub fn nd_get_artist_images( @@ -317,7 +317,7 @@ pub trait ArtistTopSongsProvider { /// Register the get_artist_top_songs export. /// This macro generates the WASM export function for this method. #[macro_export] -macro_rules! register_artist_top_songs { +macro_rules! register_metadata_artist_top_songs { ($plugin_type:ty) => { #[extism_pdk::plugin_fn] pub fn nd_get_artist_top_songs( @@ -338,7 +338,7 @@ pub trait AlbumInfoProvider { /// Register the get_album_info export. /// This macro generates the WASM export function for this method. #[macro_export] -macro_rules! register_album_info { +macro_rules! register_metadata_album_info { ($plugin_type:ty) => { #[extism_pdk::plugin_fn] pub fn nd_get_album_info( @@ -359,7 +359,7 @@ pub trait AlbumImagesProvider { /// Register the get_album_images export. /// This macro generates the WASM export function for this method. #[macro_export] -macro_rules! register_album_images { +macro_rules! register_metadata_album_images { ($plugin_type:ty) => { #[extism_pdk::plugin_fn] pub fn nd_get_album_images( diff --git a/plugins/pdk/rust/nd-pdk-capabilities/src/websocket.rs b/plugins/pdk/rust/nd-pdk-capabilities/src/websocket.rs index 4cf25520c..81374ebe8 100644 --- a/plugins/pdk/rust/nd-pdk-capabilities/src/websocket.rs +++ b/plugins/pdk/rust/nd-pdk-capabilities/src/websocket.rs @@ -81,7 +81,7 @@ pub trait TextMessageProvider { /// Register the on_text_message export. /// This macro generates the WASM export function for this method. #[macro_export] -macro_rules! register_text_message { +macro_rules! register_websocket_text_message { ($plugin_type:ty) => { #[extism_pdk::plugin_fn] pub fn nd_websocket_on_text_message( @@ -102,7 +102,7 @@ pub trait BinaryMessageProvider { /// Register the on_binary_message export. /// This macro generates the WASM export function for this method. #[macro_export] -macro_rules! register_binary_message { +macro_rules! register_websocket_binary_message { ($plugin_type:ty) => { #[extism_pdk::plugin_fn] pub fn nd_websocket_on_binary_message( @@ -123,7 +123,7 @@ pub trait ErrorProvider { /// Register the on_error export. /// This macro generates the WASM export function for this method. #[macro_export] -macro_rules! register_error { +macro_rules! register_websocket_error { ($plugin_type:ty) => { #[extism_pdk::plugin_fn] pub fn nd_websocket_on_error( @@ -144,7 +144,7 @@ pub trait CloseProvider { /// Register the on_close export. /// This macro generates the WASM export function for this method. #[macro_export] -macro_rules! register_close { +macro_rules! register_websocket_close { ($plugin_type:ty) => { #[extism_pdk::plugin_fn] pub fn nd_websocket_on_close(