Diesel behaviour (diesel v0.1.0)

Declarative programming in Elixir

Diesel is a toolkit that helps you build your own DSLs.

Example:

defmodule Latex.Dsl do
  use Diesel.Dsl,
    otp_app: :latex,
    root: :latex,
    packages: [...],
    tags: [
      :document,
      :package,
      :packages,
      :section,
      :subsection,
      ...
    ],
end

defmodule Latex.Pdf do
  @behaviour Diesel.Generator

  @impl true
  def generate(_mod, definition) do
    quote do
      def to_pdf, do: "%PDF-1.4 ..."
    end
  end
end

defmodule Latex do
  use Diesel,
    otp_app: :my_app,
    dsl: Latex.Dsl,
    generators: [
      Latex.Pdf
    ]
end

then we could use it with:

defmodule MyApp.Paper do
  use Latex

  latex do
    document size: "a4" do
      packages [:babel, :graphics]

      section title: "Introduction" do
        subsection title: "Details" do
          ...
        end
      end
    end
  end
end

iex> MyApp.Paper.to_pdf()
"%PDF-1.4 ..."

DSLs built with Diesel are not sealed: they can be easily extended by via packages and code generators. These can be even defined by other apps, via application environment:

config :latex, Latex.Dsl, packages: [ ...]
config :my_app, MyApp.Paper, generators: [...]

Please take a look at the Diesel.Dsl module documentation and also the examples provided in tests.

Summary

Types

@type attrs() :: keyword()
@type element() :: {tag(), attrs(), [element()]}
@type tag() :: atom()

Callbacks

@callback compile(ctx :: map()) :: element() | [element()]
@callback definition() :: element() | [element()]