🏡 index : quine-global/quintodrome.git

// Code generated by ndpgen. DO NOT EDIT.

package host

import (
	"context"
	"encoding/json"

	extism "github.com/extism/go-sdk"

	"github.com/navidrome/navidrome/plugins/types"
)

// MatcherMatchSongsRequest is the request type for Matcher.MatchSongs.
type MatcherMatchSongsRequest struct {
	Songs []types.SongRef `json:"songs"`
	Opts  MatchOptions    `json:"opts"`
}

// MatcherMatchSongsResponse is the response type for Matcher.MatchSongs.
type MatcherMatchSongsResponse struct {
	Results []*types.Track `json:"results,omitempty"`
	Error   string         `json:"error,omitempty"`
}

// RegisterMatcherHostFunctions registers Matcher service host functions.
// The returned host functions should be added to the plugin's configuration.
func RegisterMatcherHostFunctions(service MatcherService) []extism.HostFunction {
	return []extism.HostFunction{
		newMatcherMatchSongsHostFunction(service),
	}
}

func newMatcherMatchSongsHostFunction(service MatcherService) extism.HostFunction {
	return extism.NewHostFunctionWithStack(
		"matcher_matchsongs",
		func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
			// Read JSON request from plugin memory
			reqBytes, err := p.ReadBytes(stack[0])
			if err != nil {
				matcherWriteError(p, stack, err)
				return
			}
			var req MatcherMatchSongsRequest
			if err := json.Unmarshal(reqBytes, &req); err != nil {
				matcherWriteError(p, stack, err)
				return
			}

			// Call the service method
			results, svcErr := service.MatchSongs(ctx, req.Songs, req.Opts)
			if svcErr != nil {
				matcherWriteError(p, stack, svcErr)
				return
			}

			// Write JSON response to plugin memory
			resp := MatcherMatchSongsResponse{
				Results: results,
			}
			matcherWriteResponse(p, stack, resp)
		},
		[]extism.ValueType{extism.ValueTypePTR},
		[]extism.ValueType{extism.ValueTypePTR},
	)
}

// matcherWriteResponse writes a JSON response to plugin memory.
func matcherWriteResponse(p *extism.CurrentPlugin, stack []uint64, resp any) {
	respBytes, err := json.Marshal(resp)
	if err != nil {
		matcherWriteError(p, stack, err)
		return
	}
	respPtr, err := p.WriteBytes(respBytes)
	if err != nil {
		stack[0] = 0
		return
	}
	stack[0] = respPtr
}

// matcherWriteError writes an error response to plugin memory.
func matcherWriteError(p *extism.CurrentPlugin, stack []uint64, err error) {
	errResp := struct {
		Error string `json:"error"`
	}{Error: err.Error()}
	respBytes, _ := json.Marshal(errResp)
	respPtr, _ := p.WriteBytes(respBytes)
	stack[0] = respPtr
}