# This file generated by `dagger_codegen`. Please DO NOT EDIT.
defmodule Dagger.CurrentModule do
  @moduledoc """
  Reflective module API provided to functions at runtime.
  """

  use Dagger.Core.Base, kind: :object, name: "CurrentModule"

  alias Dagger.Core.Client
  alias Dagger.Core.QueryBuilder, as: QB

  @derive Dagger.ID

  defstruct [:query_builder, :client]

  @type t() :: %__MODULE__{}

  @doc """
  A unique identifier for this CurrentModule.
  """
  @spec id(t()) :: {:ok, Dagger.CurrentModuleID.t()} | {:error, term()}
  def id(%__MODULE__{} = current_module) do
    query_builder =
      current_module.query_builder |> QB.select("id")

    Client.execute(current_module.client, query_builder)
  end

  @doc """
  The name of the module being executed in
  """
  @spec name(t()) :: {:ok, String.t()} | {:error, term()}
  def name(%__MODULE__{} = current_module) do
    query_builder =
      current_module.query_builder |> QB.select("name")

    Client.execute(current_module.client, query_builder)
  end

  @doc """
  The directory containing the module's source code loaded into the engine (plus any generated code that may have been created).
  """
  @spec source(t()) :: Dagger.Directory.t()
  def source(%__MODULE__{} = current_module) do
    query_builder =
      current_module.query_builder |> QB.select("source")

    %Dagger.Directory{
      query_builder: query_builder,
      client: current_module.client
    }
  end

  @doc """
  Load a directory from the module's scratch working directory, including any changes that may have been made to it during module function execution.
  """
  @spec workdir(t(), String.t(), [{:exclude, [String.t()]}, {:include, [String.t()]}]) ::
          Dagger.Directory.t()
  def workdir(%__MODULE__{} = current_module, path, optional_args \\ []) do
    query_builder =
      current_module.query_builder
      |> QB.select("workdir")
      |> QB.put_arg("path", path)
      |> QB.maybe_put_arg("exclude", optional_args[:exclude])
      |> QB.maybe_put_arg("include", optional_args[:include])

    %Dagger.Directory{
      query_builder: query_builder,
      client: current_module.client
    }
  end

  @doc """
  Load a file from the module's scratch working directory, including any changes that may have been made to it during module function execution.Load a file from the module's scratch working directory, including any changes that may have been made to it during module function execution.
  """
  @spec workdir_file(t(), String.t()) :: Dagger.File.t()
  def workdir_file(%__MODULE__{} = current_module, path) do
    query_builder =
      current_module.query_builder |> QB.select("workdirFile") |> QB.put_arg("path", path)

    %Dagger.File{
      query_builder: query_builder,
      client: current_module.client
    }
  end
end

defimpl Jason.Encoder, for: Dagger.CurrentModule do
  def encode(current_module, opts) do
    {:ok, id} = Dagger.CurrentModule.id(current_module)
    Jason.Encode.string(id, opts)
  end
end

defimpl Nestru.Decoder, for: Dagger.CurrentModule do
  def decode_fields_hint(_struct, _context, id) do
    {:ok, Dagger.Client.load_current_module_from_id(Dagger.Global.dag(), id)}
  end
end
