// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains client wrappers for the Store host service.
// It is intended for use in Navidrome plugins built with extism-pdk.

use extism_pdk::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Item {
    pub id: String,
    pub name: String,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct StoreSaveRequest {
    item: Item,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct StoreSaveResponse {
    #[serde(default)]
    id: String,
    #[serde(default)]
    error: Option<String>,
}

#[host_fn]
extern "ExtismHost" {
    fn store_save(input: Json<StoreSaveRequest>) -> Json<StoreSaveResponse>;
}

/// Calls the store_save host function.
///
/// # Arguments
/// * `item` - Item parameter.
///
/// # Returns
/// The id value.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn save(item: Item) -> Result<String, Error> {
    let response = unsafe {
        store_save(Json(StoreSaveRequest {
            item: item,
        }))?
    };

    if let Some(err) = response.0.error {
        return Err(Error::msg(err));
    }

    Ok(response.0.id)
}
