Shun.Rule (Shun v1.0.1) View Source

Represents what should be done based on the target pattern, which can represent an URI, IPv4, or IPv6 address. It can also be given a String literal, which represents either an IPv4 or IPv6 address, or a range of addresses using CIDR notation.

Since the Rule module only emits results based on target patterns (i.e. Elixir AST), it is best used from macros, such as those found within Shun.Builder, which can implement macros.

Link to this section Summary

Types

Represents a handler function, to be used with handle/2.

t()

Represents a Rule that is built and can be used at compile-time.

Functions

Generates an Accept rule for the given Target.

Generates a Dynamic rule for the given Target.

Generates a Reject rule for the given Target.

Link to this section Types

Specs

Represents a handler function, to be used with handle/2.

Specs

t()

Represents a Rule that is built and can be used at compile-time.

Rules are understood by Shun.Builder.

Link to this section Functions

Specs

accept(Shun.Rule.Target.t()) :: t()

Generates an Accept rule for the given Target.

Expects a target conforming to Shun.Rule.Target.t/0.

Link to this function

handle(pattern, handler)

View Source

Specs

handle(Shun.Rule.Target.t(), handle_fun()) :: t()

Generates a Dynamic rule for the given Target.

Expects a target conforming to Shun.Rule.Target.t/0.

The second argument must refer to a function, which accepts either an URI or an Address.

Example

You can use handle/2 to implement a Rule which will call your function at runtime if the value was matched.

defmodule MyApp.Shun do
  use Shun.Builder
  handle %URI{}, &custom_handle_uri/1

  def custom_handle_uri(uri) do
    cond do
      MyApp.Whitelist.allow_host?(uri.host) -> :accept
      true -> :reject
    end
  end
end

You can also use guards with handle/2:

defmodule MyApp.Shun do
  use Shun.Builder
  handle %URI{host: host} when host == "example.com", &custom_handle_uri/1

  
end

Specs

reject(Shun.Rule.Target.t()) :: t()

Generates a Reject rule for the given Target.

Expects a target conforming to Shun.Rule.Target.t/0.