1## This Source Code Form is subject to the terms of the Mozilla Public
2## License, v. 2.0. If a copy of the MPL was not distributed with this
3## file, You can obtain one at https://mozilla.org/MPL/2.0/.
4##
5## Copyright (c) 2007-2021 VMware, Inc. or its affiliates.  All rights reserved.
6
7# Formats returned values e.g. to human-readable text or JSON.
8defmodule RabbitMQ.CLI.FormatterBehaviour do
9  alias RabbitMQ.CLI.Core.Helpers
10
11  @callback format_output(any, map()) :: String.t() | [String.t()]
12  @callback format_stream(Enumerable.t(), map()) :: Enumerable.t()
13
14  @optional_callbacks switches: 0,
15                      aliases: 0
16
17  @callback switches() :: Keyword.t()
18  @callback aliases() :: Keyword.t()
19
20  def switches(formatter) do
21    Helpers.apply_if_exported(formatter, :switches, [], [])
22  end
23
24  def aliases(formatter) do
25    Helpers.apply_if_exported(formatter, :aliases, [], [])
26  end
27
28  def module_name(nil) do
29    nil
30  end
31  def module_name(formatter) do
32    mod = formatter |> String.downcase |> Macro.camelize
33    Module.safe_concat("RabbitMQ.CLI.Formatters", mod)
34  end
35
36  def machine_readable?(nil) do
37    false
38  end
39  def machine_readable?(formatter) do
40    Helpers.apply_if_exported(module_name(formatter), :machine_readable?, [], false)
41  end
42end
43