nosedrum v0.2.0 Nosedrum.Storage behaviour View Source

Storages contain commands and are used by command invokers to look up commands.

How you start a storage is up to the module itself - what is expected is that storage modules implement the behaviours documented in this module.

The public-facing API of storage modules takes an optional argument, the storage process or other information used to identify the storage such as an ETS table name.

Link to this section Summary

Types

A single command module or mapping of subcommand names to subcommand modules

The "invocation path" of the command

Callbacks

Add a new command under the given path

Return a mapping of command names to command_group/0s

Look up a command group under the specified name

Remove the command under the given path

Link to this section Types

Link to this type

command_group() View Source
command_group() ::
  Module.t()
  | %{optional(:default) => Module.t(), required(String.t()) => Module.t()}

A single command module or mapping of subcommand names to subcommand modules.

In addition to subcommand names, the key :default can be specified by the module. :default is invoked when none of the subcommands in the map match.

Link to this type

command_path() View Source
command_path() :: {String.t()} | {String.t(), String.t() | :default}

The "invocation path" of the command.

The public-facing API of storage modules should use this in order to allow users to identify the command they want to operate on.

Usage

To identify a single command, use a single element tuple, such as {"echo"}. To identify a subcommand, use a pair, such as {"infraction", "search"}. To identify the default subcommand invoked when no matching subcommand is found, specify the group name first, then :default, such as {"tags", :default}.

Link to this section Callbacks

Link to this callback

add_command(path, command, storage) View Source
add_command(
  path :: command_group(),
  command :: Module.t(),
  storage :: reference()
) :: :ok | {:error, String.t()}

Add a new command under the given path.

If the command already exists, no error should be returned.

Link to this callback

all_commands(storage) View Source
all_commands(storage :: reference()) :: %{
  optional(String.t()) => command_group()
}

Return a mapping of command names to command_group/0s.

For top-level commands, the value should be a string, otherwise, a mapping of subcommand names to subcommand modules as described on command_group/0s documentation should be returned.

Link to this callback

lookup_command(name, storage) View Source
lookup_command(name :: String.t(), storage :: reference()) ::
  command_group() | nil

Look up a command group under the specified name.

If the command was not found, nil should be returned.

Link to this callback

remove_command(path, storage) View Source
remove_command(path :: command_path(), storage :: reference()) ::
  :ok | {:error, String.t()}

Remove the command under the given path.

If the command does not exist, no error should be returned.