From 2b2b673581fcf49cdbe8bf0e0d493beba884be22 Mon Sep 17 00:00:00 2001 From: Deluan Date: Tue, 30 Dec 2025 16:53:06 -0500 Subject: [PATCH] refactor(plugins): reorganize Rust output structure to follow standard conventions Signed-off-by: Deluan --- plugins/cmd/ndpgen/integration_test.go | 10 ++-- .../cmd/ndpgen/internal/templates/lib.rs.tmpl | 8 ++- plugins/cmd/ndpgen/main.go | 22 ++++---- plugins/examples/webhook-rs/src/lib.rs | 10 ++-- plugins/pdk/rust/nd-pdk-host/Cargo.toml | 1 - plugins/pdk/rust/nd-pdk-host/{ => src}/lib.rs | 56 ++++++++++++++----- .../nd-pdk-host/{ => src}/nd_host_artwork.rs | 0 .../nd-pdk-host/{ => src}/nd_host_cache.rs | 0 .../nd-pdk-host/{ => src}/nd_host_kvstore.rs | 0 .../nd-pdk-host/{ => src}/nd_host_library.rs | 0 .../{ => src}/nd_host_scheduler.rs | 0 .../{ => src}/nd_host_subsonicapi.rs | 0 .../{ => src}/nd_host_websocket.rs | 0 13 files changed, 70 insertions(+), 37 deletions(-) rename plugins/pdk/rust/nd-pdk-host/{ => src}/lib.rs (57%) rename plugins/pdk/rust/nd-pdk-host/{ => src}/nd_host_artwork.rs (100%) rename plugins/pdk/rust/nd-pdk-host/{ => src}/nd_host_cache.rs (100%) rename plugins/pdk/rust/nd-pdk-host/{ => src}/nd_host_kvstore.rs (100%) rename plugins/pdk/rust/nd-pdk-host/{ => src}/nd_host_library.rs (100%) rename plugins/pdk/rust/nd-pdk-host/{ => src}/nd_host_scheduler.rs (100%) rename plugins/pdk/rust/nd-pdk-host/{ => src}/nd_host_subsonicapi.rs (100%) rename plugins/pdk/rust/nd-pdk-host/{ => src}/nd_host_websocket.rs (100%) diff --git a/plugins/cmd/ndpgen/integration_test.go b/plugins/cmd/ndpgen/integration_test.go index 20ff7c61f..e0a18d65f 100644 --- a/plugins/cmd/ndpgen/integration_test.go +++ b/plugins/cmd/ndpgen/integration_test.go @@ -228,11 +228,11 @@ type ServiceB interface { Expect(string(pyClientActual)).To(Equal(pyClientExpected), "Python client code mismatch") - // Verify Rust client code (now in $output/rust/nd-pdk-host/) - rustHostDir := filepath.Join(outputDir, "rust", "nd-pdk-host") - rsClientEntries, err := os.ReadDir(rustHostDir) + // Verify Rust client code (now in $output/rust/nd-pdk-host/src/) + rustSrcDir := filepath.Join(outputDir, "rust", "nd-pdk-host", "src") + rsClientEntries, err := os.ReadDir(rustSrcDir) Expect(err).ToNot(HaveOccurred()) - Expect(rsClientEntries).To(HaveLen(2), "Expected Rust client file and lib.rs") + Expect(rsClientEntries).To(HaveLen(2), "Expected Rust client file and lib.rs in src/") // Find the client file (not lib.rs) var rsClientName string @@ -244,7 +244,7 @@ type ServiceB interface { } Expect(rsClientName).ToNot(BeEmpty(), "Expected to find Rust client file") - rsClientActual, err := os.ReadFile(filepath.Join(rustHostDir, rsClientName)) + rsClientActual, err := os.ReadFile(filepath.Join(rustSrcDir, rsClientName)) Expect(err).ToNot(HaveOccurred()) Expect(string(rsClientActual)).To(Equal(rsClientExpected), "Rust client code mismatch") diff --git a/plugins/cmd/ndpgen/internal/templates/lib.rs.tmpl b/plugins/cmd/ndpgen/internal/templates/lib.rs.tmpl index 50f2603bf..3b0434ee2 100644 --- a/plugins/cmd/ndpgen/internal/templates/lib.rs.tmpl +++ b/plugins/cmd/ndpgen/internal/templates/lib.rs.tmpl @@ -36,8 +36,12 @@ //! - [`{{.Name | lower}}`] - {{if .Doc}}{{.Doc | firstLine}}{{else}}{{.Name}} service{{end}} {{- end}} {{range .Services}} -#[path = "nd_host_{{.Name | lower}}.rs"] -pub mod {{.Name | lower}}; +#[doc(hidden)] +mod nd_host_{{.Name | lower}}; +/// {{if .Doc}}{{.Doc | firstLine}}{{else}}{{.Name}} host service wrappers.{{end}} +pub mod {{.Name | lower}} { + pub use super::nd_host_{{.Name | lower}}::*; +} {{end}} // Re-export commonly used types from extism-pdk for convenience pub use extism_pdk::Error; diff --git a/plugins/cmd/ndpgen/main.go b/plugins/cmd/ndpgen/main.go index 5ff8a4a21..ddb0a5ea0 100644 --- a/plugins/cmd/ndpgen/main.go +++ b/plugins/cmd/ndpgen/main.go @@ -577,17 +577,18 @@ func generateRustClientCode(svc internal.Service, outputDir string, dryRun, verb return fmt.Errorf("generating code: %w", err) } - // Rust code goes directly in the output directory - clientFile := filepath.Join(outputDir, "nd_host_"+strings.ToLower(svc.Name)+".rs") + // Rust code goes in src/ subdirectory (standard Rust convention) + srcDir := filepath.Join(outputDir, "src") + clientFile := filepath.Join(srcDir, "nd_host_"+strings.ToLower(svc.Name)+".rs") if dryRun { fmt.Printf("=== %s ===\n%s\n", clientFile, code) return nil } - // Create output directory if needed - if err := os.MkdirAll(outputDir, 0755); err != nil { - return fmt.Errorf("creating rust client directory: %w", err) + // Create src directory if needed + if err := os.MkdirAll(srcDir, 0755); err != nil { + return fmt.Errorf("creating rust src directory: %w", err) } if err := os.WriteFile(clientFile, code, 0600); err != nil { @@ -607,17 +608,18 @@ func generateRustLibFile(services []internal.Service, outputDir string, dryRun, return fmt.Errorf("generating lib.rs: %w", err) } - // lib.rs goes directly in the output directory - libFile := filepath.Join(outputDir, "lib.rs") + // lib.rs goes in src/ subdirectory (standard Rust convention) + srcDir := filepath.Join(outputDir, "src") + libFile := filepath.Join(srcDir, "lib.rs") if dryRun { fmt.Printf("=== %s ===\n%s\n", libFile, code) return nil } - // Create output directory if needed - if err := os.MkdirAll(outputDir, 0755); err != nil { - return fmt.Errorf("creating rust client directory: %w", err) + // Create src directory if needed + if err := os.MkdirAll(srcDir, 0755); err != nil { + return fmt.Errorf("creating rust src directory: %w", err) } if err := os.WriteFile(libFile, code, 0600); err != nil { diff --git a/plugins/examples/webhook-rs/src/lib.rs b/plugins/examples/webhook-rs/src/lib.rs index 0850e61c8..30c394855 100644 --- a/plugins/examples/webhook-rs/src/lib.rs +++ b/plugins/examples/webhook-rs/src/lib.rs @@ -67,10 +67,10 @@ impl Scrobbler for WebhookPlugin { // Build query parameters let query = format!( "?title={}&artist={}&album={}&user={}×tamp={}", - urlencod(&req.track.title), - urlencod(&req.track.artist), - urlencod(&req.track.album), - urlencod(&req.username), + urlencode(&req.track.title), + urlencode(&req.track.artist), + urlencode(&req.track.album), + urlencode(&req.username), req.timestamp ); @@ -105,7 +105,7 @@ impl Scrobbler for WebhookPlugin { } /// Simple URL encoding for query parameters. -fn urlencod(s: &str) -> String { +fn urlencode(s: &str) -> String { let mut result = String::with_capacity(s.len() * 3); for c in s.chars() { match c { diff --git a/plugins/pdk/rust/nd-pdk-host/Cargo.toml b/plugins/pdk/rust/nd-pdk-host/Cargo.toml index 52fd0ab1c..4cb828697 100644 --- a/plugins/pdk/rust/nd-pdk-host/Cargo.toml +++ b/plugins/pdk/rust/nd-pdk-host/Cargo.toml @@ -8,7 +8,6 @@ license = "GPL-3.0" readme = "README.md" [lib] -path = "lib.rs" crate-type = ["rlib"] [dependencies] diff --git a/plugins/pdk/rust/nd-pdk-host/lib.rs b/plugins/pdk/rust/nd-pdk-host/src/lib.rs similarity index 57% rename from plugins/pdk/rust/nd-pdk-host/lib.rs rename to plugins/pdk/rust/nd-pdk-host/src/lib.rs index 7fabce3d5..ac4259fe8 100644 --- a/plugins/pdk/rust/nd-pdk-host/lib.rs +++ b/plugins/pdk/rust/nd-pdk-host/src/lib.rs @@ -40,26 +40,54 @@ //! - [`subsonicapi`] - provides access to Navidrome's Subsonic API from plugins. //! - [`websocket`] - provides WebSocket communication capabilities for plugins. -#[path = "nd_host_artwork.rs"] -pub mod artwork; +#[doc(hidden)] +mod nd_host_artwork; +/// provides artwork URL generation capabilities for plugins. +pub mod artwork { + pub use super::nd_host_artwork::*; +} -#[path = "nd_host_cache.rs"] -pub mod cache; +#[doc(hidden)] +mod nd_host_cache; +/// provides in-memory TTL-based caching capabilities for plugins. +pub mod cache { + pub use super::nd_host_cache::*; +} -#[path = "nd_host_kvstore.rs"] -pub mod kvstore; +#[doc(hidden)] +mod nd_host_kvstore; +/// provides persistent key-value storage for plugins. +pub mod kvstore { + pub use super::nd_host_kvstore::*; +} -#[path = "nd_host_library.rs"] -pub mod library; +#[doc(hidden)] +mod nd_host_library; +/// provides access to music library metadata for plugins. +pub mod library { + pub use super::nd_host_library::*; +} -#[path = "nd_host_scheduler.rs"] -pub mod scheduler; +#[doc(hidden)] +mod nd_host_scheduler; +/// provides task scheduling capabilities for plugins. +pub mod scheduler { + pub use super::nd_host_scheduler::*; +} -#[path = "nd_host_subsonicapi.rs"] -pub mod subsonicapi; +#[doc(hidden)] +mod nd_host_subsonicapi; +/// provides access to Navidrome's Subsonic API from plugins. +pub mod subsonicapi { + pub use super::nd_host_subsonicapi::*; +} -#[path = "nd_host_websocket.rs"] -pub mod websocket; +#[doc(hidden)] +mod nd_host_websocket; +/// provides WebSocket communication capabilities for plugins. +pub mod websocket { + pub use super::nd_host_websocket::*; +} // Re-export commonly used types from extism-pdk for convenience pub use extism_pdk::Error; diff --git a/plugins/pdk/rust/nd-pdk-host/nd_host_artwork.rs b/plugins/pdk/rust/nd-pdk-host/src/nd_host_artwork.rs similarity index 100% rename from plugins/pdk/rust/nd-pdk-host/nd_host_artwork.rs rename to plugins/pdk/rust/nd-pdk-host/src/nd_host_artwork.rs diff --git a/plugins/pdk/rust/nd-pdk-host/nd_host_cache.rs b/plugins/pdk/rust/nd-pdk-host/src/nd_host_cache.rs similarity index 100% rename from plugins/pdk/rust/nd-pdk-host/nd_host_cache.rs rename to plugins/pdk/rust/nd-pdk-host/src/nd_host_cache.rs diff --git a/plugins/pdk/rust/nd-pdk-host/nd_host_kvstore.rs b/plugins/pdk/rust/nd-pdk-host/src/nd_host_kvstore.rs similarity index 100% rename from plugins/pdk/rust/nd-pdk-host/nd_host_kvstore.rs rename to plugins/pdk/rust/nd-pdk-host/src/nd_host_kvstore.rs diff --git a/plugins/pdk/rust/nd-pdk-host/nd_host_library.rs b/plugins/pdk/rust/nd-pdk-host/src/nd_host_library.rs similarity index 100% rename from plugins/pdk/rust/nd-pdk-host/nd_host_library.rs rename to plugins/pdk/rust/nd-pdk-host/src/nd_host_library.rs diff --git a/plugins/pdk/rust/nd-pdk-host/nd_host_scheduler.rs b/plugins/pdk/rust/nd-pdk-host/src/nd_host_scheduler.rs similarity index 100% rename from plugins/pdk/rust/nd-pdk-host/nd_host_scheduler.rs rename to plugins/pdk/rust/nd-pdk-host/src/nd_host_scheduler.rs diff --git a/plugins/pdk/rust/nd-pdk-host/nd_host_subsonicapi.rs b/plugins/pdk/rust/nd-pdk-host/src/nd_host_subsonicapi.rs similarity index 100% rename from plugins/pdk/rust/nd-pdk-host/nd_host_subsonicapi.rs rename to plugins/pdk/rust/nd-pdk-host/src/nd_host_subsonicapi.rs diff --git a/plugins/pdk/rust/nd-pdk-host/nd_host_websocket.rs b/plugins/pdk/rust/nd-pdk-host/src/nd_host_websocket.rs similarity index 100% rename from plugins/pdk/rust/nd-pdk-host/nd_host_websocket.rs rename to plugins/pdk/rust/nd-pdk-host/src/nd_host_websocket.rs