defmodule Extism.CancelHandle do
  @moduledoc """
  A CancelHandle is a handle generated by a plugin that allows it to be cancelled from another
  thread while running.
  """
  defstruct [
    # The actual NIF Resource. PluginIndex and the context
    handle: nil
  ]

  def wrap_resource(handle) do
    %__MODULE__{
      handle: handle
    }
  end

  @doc """
  Cancel plugin execution
  """
  def cancel(handle) do
    Extism.Native.plugin_cancel(handle.handle)
  end
end

defimpl Inspect, for: Extim.CancelHandle do
  import Inspect.Algebra

  def inspect(dict, opts) do
    concat(["#Extism.CancelHandle<", to_doc(dict.handle, opts), ">"])
  end
end
