use extism_pdk::*;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Library {
pub id: i32,
pub name: String,
#[serde(default)]
pub path: String,
#[serde(default)]
pub mount_point: String,
pub last_scan_at: i64,
pub total_songs: i32,
pub total_albums: i32,
pub total_artists: i32,
pub total_size: i64,
pub total_duration: f64,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct LibraryGetLibraryRequest {
id: i32,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct LibraryGetLibraryResponse {
#[serde(default)]
result: Option<Library>,
#[serde(default)]
error: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct LibraryGetAllLibrariesResponse {
#[serde(default)]
result: Vec<Library>,
#[serde(default)]
error: Option<String>,
}
#[host_fn]
extern "ExtismHost" {
fn library_getlibrary(input: Json<LibraryGetLibraryRequest>) -> Json<LibraryGetLibraryResponse>;
fn library_getalllibraries(input: Json<serde_json::Value>) -> Json<LibraryGetAllLibrariesResponse>;
}
pub fn get_library(id: i32) -> Result<Option<Library>, Error> {
let response = unsafe {
library_getlibrary(Json(LibraryGetLibraryRequest {
id: id,
}))?
};
if let Some(err) = response.0.error {
return Err(Error::msg(err));
}
Ok(response.0.result)
}
pub fn get_all_libraries() -> Result<Vec<Library>, Error> {
let response = unsafe {
library_getalllibraries(Json(serde_json::json!({})))?
};
if let Some(err) = response.0.error {
return Err(Error::msg(err));
}
Ok(response.0.result)
}