Files
navidrome/plugins/pdk/rust/nd-pdk-host/src/nd_host_cache.rs
T
Deluan Quintão bd8032b327 fix(plugins): add base64 handling for []byte and remove raw=true (#5121)
* fix(plugins): add base64 handling for []byte and remove raw=true

Go's json.Marshal automatically base64-encodes []byte fields, but Rust's
serde_json serializes Vec<u8> as a JSON array and Python's json.dumps
raises TypeError on bytes. This fixes both directions of plugin
communication by adding proper base64 encoding/decoding in generated
client code.

For Rust templates (client and capability): adds a base64_bytes serde
helper module with #[serde(with = "base64_bytes")] on all Vec<u8> fields,
and adds base64 as a dependency. For Python templates: wraps bytes params
with base64.b64encode() and responses with base64.b64decode().

Also removes the raw=true binary framing protocol from all templates,
the parser, and the Method type. The raw mechanism added complexity that
is no longer needed once []byte works properly over JSON.

* fix(plugins): update production code and tests for base64 migration

Remove raw=true annotation from SubsonicAPI.CallRaw, delete all raw
test fixtures, remove raw-related test cases from parser, generator, and
integration tests, and add new test cases validating base64 handling
for Rust and Python templates.

* fix(plugins): update golden files and regenerate production code

Update golden test fixtures for codec and comprehensive services to
include base64 handling for []byte fields. Regenerate all production
PDK code (Go, Rust, Python) and host wrappers to use standard JSON
with base64-encoded byte fields instead of binary framing protocol.

* refactor: remove base64 helper duplication from rust template

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(plugins): add base64 dependency to capabilities' Cargo.toml

Signed-off-by: Deluan <deluan@navidrome.org>

---------

Signed-off-by: Deluan <deluan@navidrome.org>
2026-02-27 19:00:19 -05:00

522 lines
13 KiB
Rust

// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains client wrappers for the Cache host service.
// It is intended for use in Navidrome plugins built with extism-pdk.
use extism_pdk::*;
use serde::{Deserialize, Serialize};
use base64::Engine as _;
use base64::engine::general_purpose::STANDARD as BASE64;
mod base64_bytes {
use serde::{self, Deserialize, Deserializer, Serializer};
use base64::Engine as _;
use base64::engine::general_purpose::STANDARD as BASE64;
pub fn serialize<S>(bytes: &Vec<u8>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&BASE64.encode(bytes))
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
BASE64.decode(&s).map_err(serde::de::Error::custom)
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct CacheSetStringRequest {
key: String,
value: String,
ttl_seconds: i64,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CacheSetStringResponse {
#[serde(default)]
error: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct CacheGetStringRequest {
key: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CacheGetStringResponse {
#[serde(default)]
value: String,
#[serde(default)]
exists: bool,
#[serde(default)]
error: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct CacheSetIntRequest {
key: String,
value: i64,
ttl_seconds: i64,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CacheSetIntResponse {
#[serde(default)]
error: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct CacheGetIntRequest {
key: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CacheGetIntResponse {
#[serde(default)]
value: i64,
#[serde(default)]
exists: bool,
#[serde(default)]
error: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct CacheSetFloatRequest {
key: String,
value: f64,
ttl_seconds: i64,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CacheSetFloatResponse {
#[serde(default)]
error: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct CacheGetFloatRequest {
key: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CacheGetFloatResponse {
#[serde(default)]
value: f64,
#[serde(default)]
exists: bool,
#[serde(default)]
error: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct CacheSetBytesRequest {
key: String,
#[serde(with = "base64_bytes")]
value: Vec<u8>,
ttl_seconds: i64,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CacheSetBytesResponse {
#[serde(default)]
error: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct CacheGetBytesRequest {
key: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CacheGetBytesResponse {
#[serde(default)]
#[serde(with = "base64_bytes")]
value: Vec<u8>,
#[serde(default)]
exists: bool,
#[serde(default)]
error: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct CacheHasRequest {
key: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CacheHasResponse {
#[serde(default)]
exists: bool,
#[serde(default)]
error: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct CacheRemoveRequest {
key: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CacheRemoveResponse {
#[serde(default)]
error: Option<String>,
}
#[host_fn]
extern "ExtismHost" {
fn cache_setstring(input: Json<CacheSetStringRequest>) -> Json<CacheSetStringResponse>;
fn cache_getstring(input: Json<CacheGetStringRequest>) -> Json<CacheGetStringResponse>;
fn cache_setint(input: Json<CacheSetIntRequest>) -> Json<CacheSetIntResponse>;
fn cache_getint(input: Json<CacheGetIntRequest>) -> Json<CacheGetIntResponse>;
fn cache_setfloat(input: Json<CacheSetFloatRequest>) -> Json<CacheSetFloatResponse>;
fn cache_getfloat(input: Json<CacheGetFloatRequest>) -> Json<CacheGetFloatResponse>;
fn cache_setbytes(input: Json<CacheSetBytesRequest>) -> Json<CacheSetBytesResponse>;
fn cache_getbytes(input: Json<CacheGetBytesRequest>) -> Json<CacheGetBytesResponse>;
fn cache_has(input: Json<CacheHasRequest>) -> Json<CacheHasResponse>;
fn cache_remove(input: Json<CacheRemoveRequest>) -> Json<CacheRemoveResponse>;
}
/// SetString stores a string value in the cache.
///
/// Parameters:
/// - key: The cache key (will be namespaced with plugin ID)
/// - value: The string value to store
/// - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours)
///
/// Returns an error if the operation fails.
///
/// # Arguments
/// * `key` - String parameter.
/// * `value` - String parameter.
/// * `ttl_seconds` - i64 parameter.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn set_string(key: &str, value: &str, ttl_seconds: i64) -> Result<(), Error> {
let response = unsafe {
cache_setstring(Json(CacheSetStringRequest {
key: key.to_owned(),
value: value.to_owned(),
ttl_seconds: ttl_seconds,
}))?
};
if let Some(err) = response.0.error {
return Err(Error::msg(err));
}
Ok(())
}
/// GetString retrieves a string value from the cache.
///
/// Parameters:
/// - key: The cache key (will be namespaced with plugin ID)
///
/// Returns the value and whether the key exists. If the key doesn't exist
/// or the stored value is not a string, exists will be false.
///
/// # Arguments
/// * `key` - String parameter.
///
/// # Returns
/// `Some(value)` if found, `None` otherwise.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn get_string(key: &str) -> Result<Option<String>, Error> {
let response = unsafe {
cache_getstring(Json(CacheGetStringRequest {
key: key.to_owned(),
}))?
};
if let Some(err) = response.0.error {
return Err(Error::msg(err));
}
if response.0.exists {
Ok(Some(response.0.value))
} else {
Ok(None)
}
}
/// SetInt stores an integer value in the cache.
///
/// Parameters:
/// - key: The cache key (will be namespaced with plugin ID)
/// - value: The integer value to store
/// - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours)
///
/// Returns an error if the operation fails.
///
/// # Arguments
/// * `key` - String parameter.
/// * `value` - i64 parameter.
/// * `ttl_seconds` - i64 parameter.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn set_int(key: &str, value: i64, ttl_seconds: i64) -> Result<(), Error> {
let response = unsafe {
cache_setint(Json(CacheSetIntRequest {
key: key.to_owned(),
value: value,
ttl_seconds: ttl_seconds,
}))?
};
if let Some(err) = response.0.error {
return Err(Error::msg(err));
}
Ok(())
}
/// GetInt retrieves an integer value from the cache.
///
/// Parameters:
/// - key: The cache key (will be namespaced with plugin ID)
///
/// Returns the value and whether the key exists. If the key doesn't exist
/// or the stored value is not an integer, exists will be false.
///
/// # Arguments
/// * `key` - String parameter.
///
/// # Returns
/// `Some(value)` if found, `None` otherwise.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn get_int(key: &str) -> Result<Option<i64>, Error> {
let response = unsafe {
cache_getint(Json(CacheGetIntRequest {
key: key.to_owned(),
}))?
};
if let Some(err) = response.0.error {
return Err(Error::msg(err));
}
if response.0.exists {
Ok(Some(response.0.value))
} else {
Ok(None)
}
}
/// SetFloat stores a float value in the cache.
///
/// Parameters:
/// - key: The cache key (will be namespaced with plugin ID)
/// - value: The float value to store
/// - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours)
///
/// Returns an error if the operation fails.
///
/// # Arguments
/// * `key` - String parameter.
/// * `value` - f64 parameter.
/// * `ttl_seconds` - i64 parameter.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn set_float(key: &str, value: f64, ttl_seconds: i64) -> Result<(), Error> {
let response = unsafe {
cache_setfloat(Json(CacheSetFloatRequest {
key: key.to_owned(),
value: value,
ttl_seconds: ttl_seconds,
}))?
};
if let Some(err) = response.0.error {
return Err(Error::msg(err));
}
Ok(())
}
/// GetFloat retrieves a float value from the cache.
///
/// Parameters:
/// - key: The cache key (will be namespaced with plugin ID)
///
/// Returns the value and whether the key exists. If the key doesn't exist
/// or the stored value is not a float, exists will be false.
///
/// # Arguments
/// * `key` - String parameter.
///
/// # Returns
/// `Some(value)` if found, `None` otherwise.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn get_float(key: &str) -> Result<Option<f64>, Error> {
let response = unsafe {
cache_getfloat(Json(CacheGetFloatRequest {
key: key.to_owned(),
}))?
};
if let Some(err) = response.0.error {
return Err(Error::msg(err));
}
if response.0.exists {
Ok(Some(response.0.value))
} else {
Ok(None)
}
}
/// SetBytes stores a byte slice in the cache.
///
/// Parameters:
/// - key: The cache key (will be namespaced with plugin ID)
/// - value: The byte slice to store
/// - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours)
///
/// Returns an error if the operation fails.
///
/// # Arguments
/// * `key` - String parameter.
/// * `value` - Vec<u8> parameter.
/// * `ttl_seconds` - i64 parameter.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn set_bytes(key: &str, value: Vec<u8>, ttl_seconds: i64) -> Result<(), Error> {
let response = unsafe {
cache_setbytes(Json(CacheSetBytesRequest {
key: key.to_owned(),
value: value,
ttl_seconds: ttl_seconds,
}))?
};
if let Some(err) = response.0.error {
return Err(Error::msg(err));
}
Ok(())
}
/// GetBytes retrieves a byte slice from the cache.
///
/// Parameters:
/// - key: The cache key (will be namespaced with plugin ID)
///
/// Returns the value and whether the key exists. If the key doesn't exist
/// or the stored value is not a byte slice, exists will be false.
///
/// # Arguments
/// * `key` - String parameter.
///
/// # Returns
/// `Some(value)` if found, `None` otherwise.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn get_bytes(key: &str) -> Result<Option<Vec<u8>>, Error> {
let response = unsafe {
cache_getbytes(Json(CacheGetBytesRequest {
key: key.to_owned(),
}))?
};
if let Some(err) = response.0.error {
return Err(Error::msg(err));
}
if response.0.exists {
Ok(Some(response.0.value))
} else {
Ok(None)
}
}
/// Has checks if a key exists in the cache.
///
/// Parameters:
/// - key: The cache key (will be namespaced with plugin ID)
///
/// Returns true if the key exists and has not expired.
///
/// # Arguments
/// * `key` - String parameter.
///
/// # Returns
/// The exists value.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn has(key: &str) -> Result<bool, Error> {
let response = unsafe {
cache_has(Json(CacheHasRequest {
key: key.to_owned(),
}))?
};
if let Some(err) = response.0.error {
return Err(Error::msg(err));
}
Ok(response.0.exists)
}
/// Remove deletes a value from the cache.
///
/// Parameters:
/// - key: The cache key (will be namespaced with plugin ID)
///
/// Returns an error if the operation fails. Does not return an error if the key doesn't exist.
///
/// # Arguments
/// * `key` - String parameter.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn remove(key: &str) -> Result<(), Error> {
let response = unsafe {
cache_remove(Json(CacheRemoveRequest {
key: key.to_owned(),
}))?
};
if let Some(err) = response.0.error {
return Err(Error::msg(err));
}
Ok(())
}