Packages
croma
0.3.8
0.13.0
0.12.0
0.11.3
0.11.2
0.11.1
0.11.0
0.10.2
0.10.1
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.2
0.8.1
0.8.0
0.7.3
0.7.2
0.7.1
0.7.0
0.6.8
0.6.7
0.6.6
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.1
0.5.0
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.8
0.3.7
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Elixir macro utilities to make type-based programming easier
Current section
Files
Jump to
Current section
Files
lib/croma/guard.ex
defmodule Croma.Guard do
@moduledoc """
Module to work with guard generation (ses `Croma.Defun.defun/2`).
This module is intended for internal use.
"""
def make(type_expr, v, caller) do
case type_expr do
{:integer , _, _} -> quote do: is_integer(unquote(v))
{:pos_integer , _, _} -> quote do: is_integer(unquote(v)) and unquote(v) > 0
{:neg_integer , _, _} -> quote do: is_integer(unquote(v)) and unquote(v) < 0
{:non_neg_integer, _, _} -> quote do: is_integer(unquote(v)) and unquote(v) >= 0
{:byte , _, _} -> quote do: is_integer(unquote(v)) and (unquote(v) in 0..255)
{:char , _, _} -> quote do: is_integer(unquote(v)) and (unquote(v) in 0..0x10ffff)
{:float , _, _} -> quote do: is_float(unquote(v))
{:number , _, _} -> quote do: is_integer(unquote(v)) or is_float(unquote(v))
{:binary , _, _} -> quote do: is_binary(unquote(v))
{:bitstring , _, _} -> quote do: is_bitstring(unquote(v))
{:module , _, _} -> quote do: is_atom(unquote(v)) or is_tuple(unquote(v))
{:atom , _, _} -> quote do: is_atom(unquote(v))
{:node , _, _} -> quote do: is_atom(unquote(v))
{:fun , _, _} -> quote do: is_function(unquote(v))
{:pid , _, _} -> quote do: is_pid(unquote(v))
{:port , _, _} -> quote do: is_port(unquote(v))
{:reference , _, _} -> quote do: is_reference(unquote(v))
{:char_list , _, _} -> quote do: is_list(unquote(v))
{:list , _, _} -> quote do: is_list(unquote(v))
{:map , _, _} -> quote do: is_map(unquote(v))
{:tuple , _, _} -> quote do: is_tuple(unquote(v))
l when is_list(l) -> quote do: is_list(unquote(v))
{:%{} , _, _} -> quote do: is_map(unquote(v))
{:{} , _, _} -> quote do: is_tuple(unquote(v))
{_, _ } -> quote do: is_tuple(unquote(v)) # tuple with two elements
{{:., _, [alias_, basename]}, _, _} ->
case {Macro.expand(alias_, caller), basename} do
{String , :t} -> quote do: is_binary(unquote(v))
{Dict , :t} -> quote do: is_list(unquote(v)) or is_map(unquote(v))
{Keyword , :t} -> quote do: is_list(unquote(v))
{Croma.Atom , :t} -> quote do: is_atom(unquote(v))
{Croma.Boolean , :t} -> quote do: is_boolean(unquote(v))
{Croma.Float , :t} -> quote do: is_float(unquote(v))
{Croma.Integer , :t} -> quote do: is_integer(unquote(v))
{Croma.String , :t} -> quote do: is_binary(unquote(v))
{Croma.BitString, :t} -> quote do: is_bitstring(unquote(v))
{Croma.Function , :t} -> quote do: is_function(unquote(v))
{Croma.Pid , :t} -> quote do: is_pid(unquote(v))
{Croma.Port , :t} -> quote do: is_port(unquote(v))
{Croma.Reference, :t} -> quote do: is_reference(unquote(v))
{Croma.Tuple , :t} -> quote do: is_tuple(unquote(v))
{Croma.List , :t} -> quote do: is_list(unquote(v))
{Croma.Map , :t} -> quote do: is_map(unquote(v))
_ -> raise "cannot generate guard for the given type: #{Macro.to_string(type_expr)}"
end
_ -> raise "cannot generate guard for the given type: #{Macro.to_string(type_expr)}"
end
end
end